-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
118 lines (97 loc) · 3.97 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
import webapp2
import jinja2
import os
import json
import math
import datetime
from google.appengine.ext import db
temp_dir=os.path.join(os.path.dirname(__file__), "")
jinja_env=jinja2.Environment(loader = jinja2.FileSystemLoader(temp_dir), autoescape = True)
class User(db.Model):
email = db.StringProperty()
msg = db.TextProperty()
time = db.DateTimeProperty(auto_now_add = True)
class Main(webapp2.RequestHandler):
def render_front(self, users):
template = jinja_env.get_template("templates/basic.html")
self.response.out.write(template.render(users = users))
def get(self):
users = db.GqlQuery("select * from User order by time desc")
users = list(users)
self.render_front(users)
def post(self):
email = self.request.get("email")
msg = self.request.get("message")
u = User(email = email, msg = msg)
u.put()
self.redirect("/")
class Accident(db.Model):
point = db.GeoPtProperty(required = True)
sev = db.RatingProperty(required = True)
#userid = db.StringProperty()
desc = db.TextProperty()
time = db.DateTimeProperty(auto_now_add = True)
def generate_accident_dict(accident):
d = {}
d["lat"] = accident.point.lat
d["longi"] = accident.point.lon
d["descr"] = accident.desc
d["severity"] = accident.sev
d["time"] = accident.time.strftime("%c")
return d
class AccidentJson(webapp2.RequestHandler):
def get(self):
accidents = db.GqlQuery("select * from Accident order by time desc")
accidents = list(accidents)
l = list(generate_accident_dict(accident) for accident in accidents)
self.response.headers["Content-Type"] = "application/json; charset=utf-8"
self.response.out.write(json.dumps(l))
class ReportAccident(webapp2.RequestHandler):
def get(self):
template = jinja_env.get_template("templates/report_accident.html")
self.response.out.write(template.render())
def post(self):
lat = self.request.get("lat")
lon = self.request.get("longi")
sev = self.request.get("severity")
sev = int(sev)
desc = self.request.get("descr")
geopt = db.GeoPt(lat = float(lat), lon = float(lon))
a = Accident(point = geopt,
sev = sev,
desc = desc)
a.put()
self.redirect("/report/accident")
class AccidentUpdate(webapp2.RequestHandler):
def get(self):
template = jinja_env.get_template("templates/accident_update.html")
self.response.out.write(template.render())
def post(self):
lat = self.request.get("lat")
lat = float(lat)
lon = self.request.get("longi")
lon = float(lon)
dtnow = datetime.datetime.now()
dtold = dtnow - datetime.timedelta(hours = 5)
accidents = db.GqlQuery("select * from Accident where time > :1", dtold)
accidents = list(accidents)
close = []
for accident in accidents:
dist = self.get_distance(lat, lon, accident.point.lat, accident.point.lon)
if dist < 20:
close.append(accident)
l = list(generate_accident_dict(accident) for accident in close)
self.response.headers["Content-Type"] = "application/json; charset=utf-8"
self.response.out.write(json.dumps(l))
def get_distance(self, lat1, lon1, lat2, lon2):
Radius = 6371
dLat = math.radians(lat2 - lat1)
dLon = math.radians(lon2 - lon1)
a = math.sin(dLat / 2)* math.sin(dLat / 2) + math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) * math.sin(dLon / 2) * math.sin(dLon / 2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a));
temp = Radius * c
return temp
app = webapp2.WSGIApplication([('/', Main),
('/accidents\.json', AccidentJson),
('/report/accident', ReportAccident),
('/accidentupdate', AccidentUpdate)])