-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
174 lines (153 loc) · 5.58 KB
/
main.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
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import cgi
import webapp2
import jinja2
import os
from datetime import datetime, date, time
from detectmobile import isMobile
from google.appengine.api import taskqueue
from google.appengine.api import memcache
import menuparser
import loadData
import menuscraper
import models
jinja = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class About(webapp2.RequestHandler):
def get(self):
template = jinja.get_template('templates/about.html')
self.response.out.write(template.render({}))
class Entree(webapp2.RequestHandler):
def get(self):
entreeID = self.request.get('id')
entreeID = int(float(entreeID))
ip = self.request.remote_addr
entree = models.getEntreeById(entreeID, ip)
params = { 'entree':entree }
template = jinja.get_template('templates/pieces/entree.html')
self.response.out.write(template.render(params))
def post(self):
# Get vars
entreeID = self.request.get('id')
entreeID = int(float(entreeID))
ip = self.request.remote_addr
vote = self.request.get('vote')
vote = int(float(vote))
# Add vote
models.addEntreeVote(entreeID, ip, vote)
class Hall(webapp2.RequestHandler):
def get(self):
template = jinja.get_template('templates/pieces/hall.html')
hall = self.request.get('hall')
d = self.request.get('day')
d = datetime.strptime(d, '%m/%d/%Y').date()
params = {}
params['meals'] = models.getHallMeals(d, hall)
self.response.out.write(template.render(params))
class Home(webapp2.RequestHandler):
def get(self):
if (isMobile(self.request)):
#if (True):
template = jinja.get_template('templates/homeMobile.html')
self.response.out.write(template.render({}))
return
template = jinja.get_template('templates/home.html')
self.response.out.write(template.render({}))
class Load(webapp2.RequestHandler):
def get(self):
for i in range(0, 7):
url_to_do = "/load-data"
taskqueue.add(url=url_to_do, params={'offset':i}, method = 'GET')
class LoadData(webapp2.RequestHandler):
def get(self):
offset = self.request.get('offset')
if offset == '':
offset = 0
offset = int(float(offset))
(meals, entrees) = loadData.load(offset)
#(meals, entrees) = models.getMealsAndEntrees()
#menus = models.getHomeMenus()
#self.response.out.write(menus)
for m in meals:
self.response.out.write(m.html_string())
for e in entrees:
self.response.out.write(e.html_string())
class Delete(webapp2.RequestHandler):
def get(self):
taskqueue.add(url="/del-data", method = 'GET')
class DeleteData(webapp2.RequestHandler):
def get(self):
# Uncomment when database is updated, this breaks things right now.
models.delOutdatedEntries()
print "Deleted outdated data"
class Menus(webapp2.RequestHandler):
def get(self):
template = jinja.get_template('templates/pieces/menus.html')
d = self.request.get('day')
d = datetime.strptime(d, '%m/%d/%Y').date()
meal = self.request.get('meal')
params = {}
params['menus'] = models.getMeals(d, meal)
self.response.out.write(template.render(params))
class MobileSearch(webapp2.RequestHandler):
def get(self):
q = self.request.get('q')
ip = self.request.remote_addr
params = {
'entrees':models.searchEntrees(q, ip),
'q':q
}
template = jinja.get_template('templates/mobileSearch.html')
self.response.out.write(template.render(params))
class Search(webapp2.RequestHandler):
def get(self):
q = self.request.get('q')
ip = self.request.remote_addr
params = {
'entrees':models.searchEntrees(q, ip),
'q':q
}
template = jinja.get_template('templates/search.html')
self.response.out.write(template.render(params))
class Timeline(webapp2.RequestHandler):
def get(self):
template = jinja.get_template('templates/timeline.html')
self.response.out.write(template.render({}))
class Tutorial(webapp2.RequestHandler):
def get(self):
template = jinja.get_template('templates/tutorial.html')
self.response.out.write(template.render({}))
app = webapp2.WSGIApplication([
('/about', About),
('/entree', Entree),
('/hall', Hall),
('/load-data', LoadData),
('/queue-load', Load),
('/del-data', DeleteData),
('/queue-del', Delete),
('/menus', Menus),
('/mobile-search', MobileSearch),
('/search', Search),
('/timeline', Timeline),
('/', Home)
], debug=True)
def handle_404(request, response, exception):
template = jinja.get_template('templates/404.html')
response.out.write(template.render({}))
response.set_status(404)
app.error_handlers[404] = handle_404