-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratings.py
248 lines (232 loc) · 8.43 KB
/
ratings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import os
from google.appengine.ext.webapp import template
import cgi
import logging
import time
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.api import memcache
import adventureModel
import main
import admin
import signup
def getUserVote(adventureStatus, user, iphone):
if not (adventureStatus and (user or iphone)):
logging.warn("getUserVote requires adventureStatus and either user or iphone")
userVote = None
q = None
q2 = None
if user:
q = adventureModel.UserVotes.all().filter('adventureStatus =', adventureStatus).filter('voter =', user)
voter = user.email()
elif iphone:
q = adventureModel.UserVotes.all().filter('adventureStatus =', adventureStatus).filter('iphoneId =', iphone)
voter = iphone
else:
logging.warn("stats: tried to vote but missing user or iphone")
return("You must be logged in to vote.")
memStr = "vote" + str(adventureStatus.key()) + str(voter)
userVote = memcache.get(memStr)
if userVote:
logging.info("getUserVote: got from cache: " + memStr)
else:
votes = q.fetch(1)
for myVote in votes:
userVote = myVote
if userVote:
memcache.add(memStr, userVote, 3600)
logging.info("getUserVote: got from db: " + memStr)
else:
logging.warn("getUserVote: could not find userVote with adventureStatus(%s) user(%s)" % (str(adventureStatus.key()), str(voter)))
return userVote
def addAdventureStat(adventureStatus, plays, vote, user, iphone, comment):
#try to fetch the adventureRating, then increment the stats
rating = None
changed = False
userVote = None
output = "Nothing Recorded"
voteCount = 0
if not adventureStatus:
logging.warn("stats: adventureStatus is required")
return("adventureStatus is required")
q = adventureModel.AdventureRating.all().filter('adventureStatus =', adventureStatus)
ratings = q.fetch(1)
for myRating in ratings:
rating = myRating
if rating:
logging.info("stats: found rating key in the db: " + str(adventureStatus.key()))
else:
logging.info("stats: rating key was not found in the db: " + str(adventureStatus.key()))
return("Rating Key not found in DB")
if plays and plays > 0:
rating.plays = rating.plays + plays
changed = True
if vote and vote >= 0:
#make sure this person hasn't already voted
voter = None
if user:
voter = user.email()
elif iphone:
voter = iphone
else:
logging.warn("stats: tried to vote but missing user or iphone")
return("You must be logged in to vote.")
#delete the memcache vote record
memStr = "vote" + str(adventureStatus.key()) + str(voter)
memcache.delete(memStr)
if user:
userVote = getUserVote(adventureStatus, user, None)
if not userVote:
#see if this user has voted before as an iphone
iphoneUser = signup.getDeviceFromUser(user)
if iphoneUser:
logging.info("stats: checking for vote with: " + iphoneUser)
userVote = getUserVote(adventureStatus, None, iphoneUser)
if userVote:
logging.info("stats: got vote from user -> iphone")
voter = iphoneUser;
if iphone:
userVote = getUserVote(adventureStatus, None, iphone)
if not userVote:
#see if this iphone has voted before as a user
iphoneUser = signup.getUserFromDeviceID(iphone)
if iphoneUser:
userVote = getUserVote(adventureStatus, iphoneUser, None)
if userVote:
logging.info("stats: got vote from iphone -> user")
voter = iphoneUser.email()
#fetch the vote
if userVote:
logging.warn("stats: tried to vote but this user has already voted: " + voter)
output = "Vote Updated. Thank You!"
if userVote.vote != vote:
#if they changed their vote
voteDifference = vote - userVote.vote
logging.info("stats: user is changing their vote. oldVote(%d) newVote(%d) diff(%d)" % (userVote.vote, vote, voteDifference))
userVote.vote = vote
vote = voteDifference
userVote.comment = comment
userVote.put()
changed = True
else:
#now lets create the vote record
output = "Vote Recorded. Thank You!"
voteCount = 1
userVote = adventureModel.UserVotes()
userVote.adventureStatus = rating.adventureStatus
userVote.voter = user
userVote.iphoneId = iphone
userVote.comment = comment
userVote.vote = vote
userVote.put()
changed = True
#now increment the rating
rating.voteCount = rating.voteCount + voteCount
rating.voteSum = rating.voteSum + vote
if changed:
if rating.voteCount > 0:
rating.rating = float(rating.voteSum) / float(rating.voteCount)
rating.put()
logging.info("stats: adventure(%s): plays(%d) votes(%d) voteSum(%d) rating(%f)" % (rating.adventureStatus.editableAdventure.title, rating.plays, rating.voteCount, rating.voteSum, rating.rating))
else:
logging.info("stats: nothing changed")
return(output)
def addAdventurePlay(adventureStatus):
addAdventureStat(adventureStatus, 1, 0, 0, None, None)
def addAdventureVote(adventureStatus, vote, user, comment):
output = addAdventureStat(adventureStatus, 0, vote, user, None, comment)
return output
def addAdventureVoteIphone(adventureStatus, vote, iphone, comment):
output = addAdventureStat(adventureStatus, 0, vote, None, iphone, comment)
return output
class Play(webapp.RequestHandler):
def post(self):
myAdventureKey = self.request.get('myAdventureKey')
myiphone = self.request.get('iphone')
myIP = os.environ['REMOTE_ADDR']
if not myiphone:
logging.warn("Play: iphone is required")
self.response.out("Error: iphone ID is required")
return
if not myIP:
logging.warn("Play: IP is required")
self.response.out("Error: IP is required")
return
if not myAdventureKey:
logging.warn("Play: myAdventureKey is required")
self.response.out("Error: adventure key is required for Play")
return
logging.info("Play: myAdventureKey(%s) myIphone(%s) myIP(%s)" % (myAdventureKey, myiphone, myIP))
# make sure this is a unique play (once every hour)
# unique on IP address + adventure key
alreadyVoted = False
ipKey = "%s-%s" % (myIP, myAdventureKey)
cacheGet = memcache.get(ipKey)
if (cacheGet):
logging.info("Play: already played in the last hour IP (%s)" % ipKey)
alreadyVoted = True
else:
# unique on phone + adventure key
phoneKey = "%s-%s" % (myiphone, myAdventureKey)
cacheGet = memcache.get(phoneKey)
if (cacheGet):
logging.info("Play: already played in the last hour PHONE (%s)" % phoneKey)
alreadyVoted = True
# if it wasn't unique, return success still
if alreadyVoted:
self.response.out.write("SUCCESS")
return
memcache.add(ipKey, 1, 3600)
memcache.add(phoneKey, 1, 3600)
adventure = main.getAdventure(myAdventureKey)
if not adventure:
logging.warn("Vote: adventure key did not exist in db: " + myAdventureKey)
output = "Error: Adventure key did not exist in database."
self.response.out.write(output)
return
#we should be good, lets get the adventureStatus object now
adventureStatus = admin.getAdventureStatus(adventure.adventureStatus)
if not adventureStatus:
logging.warn("Vote: could not get the adventureStatus record: " + myAdventureKey)
return
addAdventurePlay(adventureStatus)
self.response.out.write("SUCCESS")
return
class Vote(webapp.RequestHandler):
def post(self):
myAdventureKey = self.request.get('myAdventureKey')
myVote = int(self.request.get('vote'))
myiphone = self.request.get('iphone')
myComment = self.request.get('comment')
output = None
adventureStatus = None
#they either have to be logged in, or be on the iPhone
if not users.get_current_user():
error = 'Error: You must be logged in to vote.'
output = "You must be logged in to vote."
if not myiphone:
logging.info("Vote: trying to vote but user is not a reader and no iphone was passed in")
self.response.out.write(output)
return
adventure = main.getAdventure(myAdventureKey)
if not adventure:
logging.warn("Vote: adventure key did not exist in db: " + myAdventureKey)
output = "Error: Adventure key did not exist in database."
self.response.out.write(output)
return
#we should be good, lets get the adventureStatus object now
adventureStatus = admin.getAdventureStatus(adventure.adventureStatus)
if not adventureStatus:
logging.warn("Vote: could not get the adventureStatus record: " + myAdventureKey)
return
if myiphone:
output = addAdventureVoteIphone(adventureStatus, myVote, myiphone, myComment)
else:
output = addAdventureVote(adventureStatus, myVote, users.get_current_user(), myComment)
self.response.out.write(output)
return
def get(self):
self.response.out.write("get vote")
return