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
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
39 def _floatify_time(current_time
):
40 dow_int
= current_time
.weekday()
41 hour
= float(current_time
.hour
)
42 minute
= current_time
.minute
49 return _DOW_DICT
[dow_int
], hour
51 def _human_readify_time(dow
, float_time
):
54 floor_time
= math
.floor(float_time
)
55 delta
= math
.fabs(float_time
- floor_time
)
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
):
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
):
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
)
77 my_stores
= models
.Store
.query(time
=my_time
, lat
=my_position_lat
,
78 dow
=dow_string
, lon
=my_position_lon
, max_results
=2,
80 for distance
, store
in my_stores
:
81 self
.response
.out
.write(store
.name
)
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
)
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
):
113 path
= os
.path
.join(os
.path
.dirname(__file__
), os
.path
.join('templates', 'add_business.html'))
114 self
.response
.out
.write(template
.render(path
, {}))
117 name
= self
.request
.get('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'))
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'))]]
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')
152 application
= webapp
.WSGIApplication([('/', MainHandler
),
153 ('/add_biz', AddBusiness
),
154 ('/find', FindMyBusiness
) ],
156 wsgiref
.handlers
.CGIHandler().run(application
)
159 if __name__
== '__main__':