-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataDelegate.py
More file actions
154 lines (121 loc) · 4.38 KB
/
dataDelegate.py
File metadata and controls
154 lines (121 loc) · 4.38 KB
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
# a list of functions to do information queries
import entities
from entities import db
from datetime import datetime, timedelta
from HTMLParser import HTMLParser
eastern_delta = timedelta(hours = 5)
def generateCommentsHTML(voteComments):
html = ""
for comment in voteComments:
# make sure voteComment has a comment associated with it
if comment.comment is None:
continue
html += '<div class="comment">'
html += ' <div class="timestamp">'
timePasted = datetime.now() - comment.commentDate
minutes = int(timePasted.total_seconds() / 60)
hours = int(timePasted.total_seconds() / 3600)
timePasted = "Just now"
if minutes == 1:
timePasted = "%s minute ago" % minutes
elif minutes > 1:
timePasted = "%s minutes ago" % minutes
if hours > 0:
minutes = minutes - (hours*60)
hourStr = "hours"
minuteStr = "minutes"
if hours == 1:
hourStr = "hour"
if minutes == 1:
minuteStr = "minute"
timePasted = "%s %s and %s %s ago" % (hours, hourStr, minutes, minuteStr)
html += timePasted
html += ' </div>'
html += comment.comment
html += '</div>'
return html
# wonderful HTML tag stripper from http://stackoverflow.com/a/925630/121654
class MLStripper(HTMLParser):
def __init__(self):
self.reset()
self.fed = []
def handle_data(self, d):
self.fed.append(d)
def get_data(self):
return ''.join(self.fed)
def strip_tags(html):
s = MLStripper()
s.feed(html)
return s.get_data()
# cast a vote, return the name of food_entry type
def castVote(vote, foodID, userID):
if vote == '1':
vote = 1
else:
vote = 0
entry = entities.FoodLocation.get_by_id(foodID)
food_vote = db.GqlQuery("select * from FoodVote where foodLocationID=%s and userID=%s" % (foodID, userID))
food_vote = food_vote.get()
if food_vote == None:
food_vote = entities.FoodVote(userID = userID, foodLocationID = foodID, vote = vote, voteDate = datetime.now())
entry.totalVotes += 1
entry.upvotes += vote
else:
if food_vote.vote == -1: # food_vote exists because a comment was added before voting
entry.totalVotes += 1
entry.upvotes += vote
food_vote.vote = vote
food_vote.voteDate = datetime.now()
else: # vote was changed
if food_vote.vote == 0 and vote == 1: # went from downvote to upvote
entry.upvotes += vote
food_vote.vote = vote
food_vote.voteDate = datetime.now()
if food_vote.vote == 1 and vote == 0: # went from upvote to downvote
entry.upvotes -= 1
food_vote.vote = vote
food_vote.voteDate = datetime.now()
# commit changes
entry.put()
food_vote.put()
return entry.name
def addComment(comment, foodID, userID):
entry = entities.FoodLocation.get_by_id(foodID)
# if user voted, there will be a food_vote entry already created
food_vote = db.GqlQuery("select * from FoodVote where foodLocationID=%s and userID=%s" % (foodID, userID))
food_vote = food_vote.get()
if food_vote == None: # if no entry exists, create one
food_vote = entities.FoodVote(userID = userID, foodLocationID = foodID, comment = comment, commentDate = datetime.now())
else:
food_vote.comment = comment
food_vote.commentDate = datetime.now()
food_vote.put()
# get last entry based on food type
def fetchRatings(food_type="marketplace"):
entry = db.GqlQuery("select * from FoodLocation where name = '%s' ORDER BY created DESC" % food_type);
entry = entry.get() # get() retrieves 1 entry
is_old_entry = False
if entry is None: # No entry exists, create one
entry = entities.FoodLocation(name = food_type, highestRating = 0.0, lowestRating = 0.0)
entry.put()
else: # check if entry is old
entry_date = entry.created - eastern_delta
now = datetime.now() - eastern_delta
delta = now - entry_date
if delta.days > 0 or now.day != entry_date.day:
is_old_entry = True
if is_old_entry: # update rating history, create new entry
highestRating = entry.highestRating
lowestRating = entry.lowestRating
if entry.totalVotes != 0:
rating = entry.upvotes / entry.totalVotes
if rating > highestRating:
highestRating = float(rating)
if rating < lowestRating:
lowestRating = float(rating)
entry = entities.FoodLocation(name = food_type, highestRating = highestRating, lowestRating = lowestRating)
entry.put()
# get votes/comments for the entry
votes = db.GqlQuery("select * from FoodVote where foodLocationID=:1 ORDER BY commentDate DESC", entry.key().id())
votes = votes.run()
return (entry, votes)