-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
270 lines (240 loc) · 7.58 KB
/
models.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
from datetime import datetime, date, time, timedelta
from google.appengine.api import memcache
from google.appengine.ext import db
import tzsearch
import logging
import re
# Identifiers for dining halls
halls = {
'Butler & Wilson Colleges':'butlerwilson',
'Forbes College':'forbes',
'Rockefeller & Mathey Colleges':'rockymathey',
'Whitman College':'whitman'
}
# Ratings for a given protoname.
class Rating(db.Model):
upvotes = db.IntegerProperty()
upvoters = db.ListProperty(str)
downvotes = db.IntegerProperty()
downvoters = db.ListProperty(str)
# Entree data type, keyed by name
class Entree(tzsearch.SearchableModel):
date = db.DateProperty() # What day is this entree being served
allergens = db.StringListProperty()
ingredients = db.StringListProperty()
name = db.StringProperty()
protoname = db.StringProperty()
hexhash = db.StringProperty()
hall = db.StringProperty() # Dining hall
type = db.StringProperty() # breakfast, lunch or dinner
def getDownvotes(self):
r = memcache.get(self.protoname)
if r == None:
r = Rating.get_or_insert(self.protoname)
memcache.set(self.protoname, r)
if r.downvotes == None:
return 0
return r.downvotes
def getUpvotes(self):
r = memcache.get(self.protoname)
if r == None:
r = Rating.get_or_insert(self.protoname)
memcache.set(self.protoname, r)
if r.upvotes == None:
return 0
return r.upvotes
def checkUserVote(self, ip):
r = memcache.get(self.protoname)
if r == None:
r = Rating.get_or_insert(self.protoname)
memcache.set(self.protoname, r)
self.vote = 0
if ip in r.upvoters:
self.vote = 1
elif ip in r.downvoters:
self.vote = -1
def formatted_date(self):
return self.date.strftime('%A, %B %d')
def formatted_url_date(self):
return self.date.strftime('%-m/%d/%Y')
def html_string(self):
html = '<div>'
html = html + '<p><b>%s</b></p>' % self.name
html = html + '<p>%s</p>' % self.allergens
html = html + '<p>%s</p>' % self.ingredients
html = html + '</div>'
return html
# Meal data type
class Meal(db.Model):
date = db.DateProperty() # What day is this meal associated with
entreeIDs = db.ListProperty(int)
hall = db.StringProperty() # Dining hall
type = db.StringProperty() # breakfast, lunch or dinner
def html_string(self):
html = '<div>'
html = html + '<p><b>%s, %s, %s</b></p>' % (self.hall, self.type, self.date)
html = html + '<p>%s</p>' % self.entreeIDs
html = html + '</div>'
return html
# Return entree by id
def getEntreeById(id, ip):
entree = Entree.get_by_id(id)
entree.checkUserVote(ip)
return entree
# Vote on entree by id
def addEntreeVote(id, ip, vote):
# Get entree
entree = getEntreeById(id, ip)
r = memcache.get(entree.protoname)
if r == None:
r = Rating.get_or_insert(self.protoname)
# Remove old votes
if ip in r.upvoters:
r.upvoters.remove(ip)
if ip in r.downvoters:
r.downvoters.remove(ip)
# Apply new vote
if vote == 1:
r.upvoters.append(ip)
elif vote == -1:
r.downvoters.append(ip)
# Update
#del r.vote
r.upvoters.sort()
r.downvoters.sort()
r.upvotes = len(r.upvoters)
r.downvotes = len(r.downvoters)
r.put()
# This overwrites the old version, so the new votes are in the cache.
memcache.set(entree.protoname, r)
# Return all meals and entrees
def getMealsAndEntrees():
meals = Meal.all().run()
entrees = Entree.all().run()
#db.delete(meals)
#db.delete(entrees)
return (meals, entrees)
def getEntreesByHashes(hashes):
q = db.GqlQuery("SELECT * FROM Entree " +
"WHERE hexhash IN :1 ",
hashes)
entrees = []
for entree in q.run():
entrees.append(entree)
return entrees
def getMealByDateHallType(date, hall, mtype):
q = db.GqlQuery("SELECT * FROM Meal " +
"WHERE hall = :1 " +
"AND date = :2 " +
"AND type = :3 ",
hall, date, mtype)
return q.get()
def delOutdatedEntries():
d = date.today()
dMin = d- timedelta(days=1) #days=1
query = Entree.all()
query = query.filter('date <', dMin)
db.delete(query.run())
query = Meal.all()
query.filter('date <', dMin)
db.delete(query.run())
memcache.flush_all()
# Return entrees that match a search query
def searchEntrees(q, ip):
# Sanitize input
# Do nothing for null string
if not q or re.search('\w', q) is None:
return []
dhalls = ['butlerwilson', 'forbes', 'rockymathey', 'whitman']
mtypes = ["breakfast", "lunch", "dinner"]
q = re.sub("[^\w']", ' ', q)
sKey = "search_query" + q
results = memcache.get(sKey)
if results == None:
d = date.today()
dMin = d - timedelta(days=1)
dMax = d + timedelta(days=5)
query = Entree.all().search(q)
query = query.filter('date >=', dMin)
query = query.filter('date <=', dMax)
results = []
for mtype in mtypes:
for dhall in dhalls:
for entree in query.run():
if entree.type == mtype and entree.hall == dhall\
and q.lower() in entree.name.lower():
if entree not in results:
results.append(entree)
memcache.set(sKey, results)
for entree in results:
entree.checkUserVote(ip)
# Sort by date
results.sort(key=lambda r: r.date)
return results
# Return all meals for a hall for a day
def getHallMeals(d, hall):
meals = {}
q = db.GqlQuery("SELECT * FROM Meal " +
"WHERE hall = :1 " +
"AND date = :2",
hall, d)
# Set meal dictionaries
for meal in q.run():
if meal.type in meals:
continue
meals[meal.type] = Entree.get_by_id(meal.entreeIDs)
# Set hall meal array
hallMeals = []
if 'breakfast' in meals:
hallMeals.append({
'type':'breakfast',
'entrees':meals['breakfast']
})
if 'lunch' in meals:
hallMeals.append({
'type':'lunch',
'entrees':meals['lunch']
})
if 'dinner' in meals:
hallMeals.append({
'type':'dinner',
'entrees':meals['dinner']
})
return hallMeals
# Return menus for home page
def getMeals(d, type):
if type not in ['breakfast', 'lunch', 'dinner']:
return []
cKey = str(d)+str(type)
menuArray = memcache.get(cKey)
if menuArray != None:
return menuArray
menus = {}
menulist = []
q = db.GqlQuery("SELECT * FROM Meal " +
"WHERE type = :1 " +
"AND date = :2 " +
"ORDER BY hall ASC",
type, d)
for meal in q.run():
menulist.append(meal)
for meal in menulist:
if meal.hall in menus:
continue
menus[meal.hall] = Entree.get_by_id(meal.entreeIDs)
# Attempt to add data to an array of menus
menuArray = []
try:
menuArray = [
menus['butlerwilson'],
menus['forbes'],
menus['rockymathey'],
menus['whitman']
]
except Exception, e:
menuArray = [
[], [], [], []
]
# This cache entry is valid for 3 hours.
memcache.set(cKey, menuArray)
return menuArray