-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
141 lines (107 loc) · 4.38 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
import os
import json
from google.appengine.ext import ndb
from google.appengine.api.urlfetch import fetch, create_rpc, make_fetch_call
import jinja2
import webapp2
import logging
DEBUG = True
API_KEY = '2be32e012a18e934a254ddcabb8db55455b9349c'
DEVICE_OS = 'BumpFinderApp'
DEVICE_NAME = 'http://bumpfinder.appspot.com/'
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'])
def location_key(lat, lon):
return ndb.Key('Location', "%.4f,%.4f" % (float(lat), float(lon)))
class Location(ndb.Model):
longitude = ndb.FloatProperty()
latitude = ndb.FloatProperty()
class Bump(ndb.Model):
magnitude = ndb.FloatProperty()
created = ndb.DateTimeProperty(auto_now_add=True)
reporter = ndb.StringProperty()
issue_id = ndb.IntegerProperty()
def record_old_bump(bump, email, issue_id):
payload = {
'api_key': API_KEY,
'comment[comment]': "I felt the pothole, too!\n\n[auto-generated via Bump Finder app: http://bumpfinder.appspot.com/]",
'comment[email]': email,
'comment[name]': email.split('@')[0],
'comment[device_os]': DEVICE_OS,
'comment[device_name]': DEVICE_NAME
}
encoded_payload = '&'.join(map(lambda pair: "%s=%s" % pair, payload.items()))
response = fetch(url='http://test.seeclickfix.com/api/issues/%s/comments.json' % issue_id,
payload=encoded_payload,
method="POST")
logging.info(response.content)
bump.issue_id = issue_id
bump.put()
def record_new_bump(bump, lat, lon, email):
geo_json = fetch("http://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s&sensor=true" % (lat, lon))
logging.info(geo_json.content)
geo_data = json.loads(geo_json.content)['results']
if geo_data:
address = geo_data[0]['formatted_address']
else:
address = 'Address Unknown'
payload = {
'api_key': API_KEY,
'issue[summary]': 'Pothole alert!',
'issue[description]': '[auto-generated via Bump Finder app: http://bumpfinder.appspot.com/]',
'issue[reporter_display]': email.split('@')[0],
'issue[reporter_email]': email,
'issue[lat]': lat,
'issue[lng]': lon,
'issue[address]': address,
'issue[device_os]': DEVICE_OS,
'issue[device_name]': DEVICE_NAME,
}
encoded_payload = '&'.join(map(lambda pair: "%s=%s" % pair, payload.items()))
response = fetch(url="http://test.seeclickfix.com/api/issues.json",
payload=encoded_payload,
method="POST")
bump.issue_id = json.loads(response.content)['id']
bump.put()
class ClientHandler(webapp2.RequestHandler):
def get(self):
template_values = {'flash': 'starting up...', 'DEBUG': DEBUG }
template = JINJA_ENVIRONMENT.get_template('app/index.html')
self.response.write(template.render(template_values))
def post(self):
template_values = {
'DEBUG': DEBUG
}
lat = self.request.get('latitude')
lon = self.request.get('longitude')
if lat and lon:
loc_key = location_key(lat, lon)
bump_query = Bump.query(ancestor=loc_key)
old_bumps = bump_query.fetch(100)
bump = Bump(parent=loc_key)
bump.magnitude = float(self.request.get('magnitude'))
speed = self.request.get('speed', 0.0)
if speed:
speed = float(speed)
bump.speed = speed
bump.reporter = self.request.remote_addr
email = self.request.get('email')
bump.email = email
template_values['flash'] = 'BUMP RECORDED'
if old_bumps:
record_old_bump(bump, email, old_bumps[0].issue_id)
template_values['previous_reports'] = len(old_bumps)
else:
record_new_bump(bump, lat, lon, email)
template_values['previous_reports'] = 0
else:
template_values['flash'] = 'no location data-- bump not saved'
if 'json' in self.request.headers['Accept']:
self.response.write(template_values)
else:
template = JINJA_ENVIRONMENT.get_template('client.html')
self.response.write(template.render(template_values))
app = webapp2.WSGIApplication([
('/', ClientHandler)
], debug=True)