README update for latest SDK; misc small cleanup
[gae-samples.git] / 24hrsinsf / main.py
blob6b2fc986082882a6f748aabb598e8a8687c78ae6
1 #!/usr/bin/env python
3 # Copyright 2007 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 import wsgiref.handlers
19 import os
20 import logging
21 import math
22 from datetime import datetime
23 from datetime import timedelta
25 from google.appengine.ext import webapp
26 from google.appengine.ext.webapp import template
28 import models
30 _DOW_DICT = {
31 0: 'Monday',
32 1: 'Tuesday',
33 2: 'Wednesday',
34 3: 'Thursday',
35 4: 'Friday',
36 5: 'Saturday',
37 6: 'Sunday'
39 def _floatify_time(current_time):
40 dow_int = current_time.weekday()
41 hour = float(current_time.hour)
42 minute = current_time.minute
43 if minute > 15:
44 hour = hour + 0.5
45 if minute > 45:
46 hour = hour + 0.5
47 if hour == 24.0:
48 hour = 0.0
49 return _DOW_DICT[dow_int], hour
51 def _human_readify_time(dow, float_time):
52 ampm = 'AM'
53 min_string = '00'
54 floor_time = math.floor(float_time)
55 delta = math.fabs(float_time - floor_time)
56 if delta == .5:
57 logging.info(delta)
58 min_string = '30'
59 if float_time > 12.5:
60 ampm = 'PM'
61 float_time = float_time - 12.0
62 return '%s %i:%s %s' % (dow, int(floor_time), min_string, ampm)
64 class MainHandler(webapp.RequestHandler):
66 def get(self):
67 path = os.path.join(os.path.dirname(__file__), os.path.join('templates', 'find_me.html'))
68 self.response.out.write(template.render(path, {}))
70 class FindMyBusiness(webapp.RequestHandler):
71 def get(self):
72 my_position_lat = 37.765914
73 my_position_lon = -122.424817
74 current_time = datetime.now() - timedelta(hours=8)
75 dow_string, my_time = _floatify_time(current_time)
76 logging.info(my_time)
77 my_stores = models.Store.query(time=my_time, lat=my_position_lat,
78 dow=dow_string, lon=my_position_lon, max_results=2,
79 min_params=(2,0))
80 for distance, store in my_stores:
81 self.response.out.write(store.name)
83 def post(self):
84 user_pos_lat = float(self.request.get('lat'))
85 user_pos_lon = float(self.request.get('lon'))
86 time_string = self.request.get('time')
87 day_string = self.request.get('dow')
88 human_address = self.request.get('human_readable')
90 if time_string and day_string:
91 dow = _DOW_DICT[int(day_string)]
92 my_time = float(time_string)
93 else:
94 current_time = datetime.now() - timedelta(hours=8)
95 dow, my_time = _floatify_time(current_time)
97 human_time = _human_readify_time(dow, my_time)
99 my_stores = models.Store.query(time=my_time, lat=user_pos_lat,
100 dow=dow, lon=user_pos_lon,
101 max_results=2, min_params=(2,0))
102 template_values = {'store_information': my_stores,
103 'human_address': human_address,
104 'human_time': human_time,
105 'user_lat': user_pos_lat,
106 'user_lon': user_pos_lon}
108 path = os.path.join(os.path.dirname(__file__), os.path.join('templates', 'display_locations.html'))
109 self.response.out.write(template.render(path, template_values))
111 class AddBusiness(webapp.RequestHandler):
112 def get(self):
113 path = os.path.join(os.path.dirname(__file__), os.path.join('templates', 'add_business.html'))
114 self.response.out.write(template.render(path, {}))
116 def post(self):
117 name = self.request.get('name')
118 logging.info(name)
119 address = self.request.get('address')
120 description = self.request.get('description')
121 lat = float(self.request.get('lat'))
122 lon = float(self.request.get('lon'))
123 hrs_dict = {}
124 hrs_dict['Monday'] = [[int(self.request.get('monday_start')),
125 int(self.request.get('monday_end'))]]
127 hrs_dict['Tuesday'] = [[int(self.request.get('tuesday_start')),
128 int(self.request.get('tuesday_end'))]]
130 hrs_dict['Wednesday'] = [[int(self.request.get('wednesday_start')),
131 int(self.request.get('wednesday_end'))]]
133 hrs_dict['Thursday'] = [[int(self.request.get('thursday_start')),
134 int(self.request.get('thursday_end'))]]
136 hrs_dict['Friday'] = [[int(self.request.get('friday_start')),
137 int(self.request.get('friday_end'))]]
139 hrs_dict['Saturday'] = [[int(self.request.get('saturday_start')),
140 int(self.request.get('saturday_end'))]]
142 hrs_dict['Sunday'] = [[int(self.request.get('sunday_start')),
143 int(self.request.get('sunday_end'))]]
144 categories = []
145 for category in self.request.get('tags').split(','):
146 if len(categories) < 3:
147 categories.append(category)
148 new_store = models.Store.add(name=name, address=address, lat=lat, lon=lon, store_hours=hrs_dict, categories=categories, description=description)
149 self.response.out.write('done')
151 def main():
152 application = webapp.WSGIApplication([('/', MainHandler),
153 ('/add_biz', AddBusiness),
154 ('/find', FindMyBusiness) ],
155 debug=True)
156 wsgiref.handlers.CGIHandler().run(application)
159 if __name__ == '__main__':
160 main()