-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
208 lines (178 loc) · 7.73 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import sys
import cgi
import os
import re
import uuid
import logging
sys.path.append('./pymods') # local copy of modules
import simplejson
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.ext.webapp import template
## Data models (TODO separate file)
class Institution(db.Model):
name = db.StringProperty()
unguessable_id = db.StringProperty()
pass
class Building(db.Model):
name = db.StringProperty()
pt = db.GeoPtProperty()
inst = db.ReferenceProperty(Institution)
timestamp_create = db.DateTimeProperty(auto_now_add=True)
timestamp_mod = db.DateTimeProperty(auto_now=True)
pass
class Comment(db.Model):
msg = db.StringProperty()
inst = db.ReferenceProperty(Institution)
timestamp = db.DateTimeProperty(auto_now=True)
user = db.UserProperty(auto_current_user=True)
pass
## Handler classes
class MainPage(webapp.RequestHandler):
def get(self):
if users.get_current_user():
url = users.create_logout_url(self.request.uri)
url_linktext = 'Logout'
usernick = users.get_current_user().nickname()
else:
url = users.create_login_url(self.request.uri)
url_linktext = 'Login'
usernick = 'Anonymous'
template_values = {
'username':usernick,
'url': url,
'url_linktext': url_linktext,
}
path = os.path.join(os.path.dirname(__file__), 'templates/index.tmpl')
self.response.out.write(template.render(path, template_values))
class UploadInst(webapp.RequestHandler):
## Error page (TODO -- put this in a super class and inherit. Yuck.
def error_page(self, error_msg):
error_msg = "There was an error: <b>%s</b>. <br><br>Press <b>back button</b> and fix." % error_msg
template_values = {"error_msg":error_msg}
path = os.path.join(os.path.dirname(__file__), 'templates/error.tmpl')
self.response.out.write(template.render(path, template_values))
pass
## Start an institution, parsing tabular text
def post(self):
if users.get_current_user():
logging.debug(users.get_current_user().nickname())
pass
try:
## store institution
inst = Institution()
inst.name = self.request.get('inst_name').strip()
if inst.name == "":
self.error_page("Empty institution name")
return
inst.unguessable_id = str(uuid.uuid4())
inst.put()
## store first comment
comm = Comment()
comm.msg = "Initial upload. " + self.request.get('comment')
comm.inst = inst.key()
comm.put()
## store buildings, normalizing all non alphanumerics to '_' so ID's work
bldg_table_str = str(self.request.get('bldg_table')).strip()
if bldg_table_str == "":
self.error_page("Empty building field")
return
for line in re.split("\n+", bldg_table_str):
line2 = line.strip()
elems = re.split("[\s,;|]+", line2, 2)
bldg_name = re.sub('\W', '_', elems[2])
bldg = Building(
inst = inst.key(),
pt = db.GeoPt(float(elems[0]), float(elems[1])),
name = bldg_name)
bldg.put()
pass
## view the results (redirect)
self.redirect('/view?unguessable_id=%s' % inst.unguessable_id)
return
except Exception, exc:
error_msg = "There was a problem parsing input data. Python message: \"%s\".<br> Press <b>back button</b> and fix." % exc
template_values = {"error_msg":error_msg}
path = os.path.join(os.path.dirname(__file__), 'templates/error.tmpl')
self.response.out.write(template.render(path, template_values))
pass
class ViewInst(webapp.RequestHandler):
def get(self):
## get all buildings + comments for institution's id
unguessable_id = self.request.get('unguessable_id')
sqlstr = "select * from Institution where unguessable_id = :1"
query = db.GqlQuery(sqlstr, unguessable_id)
if (query.count() != 1) or (not unguessable_id):
logging.error("ViewInst.get: bad number records returned for inst: %s" % query.count())
self.redirect('/')
return
inst = query.fetch(query.count())[0]
## pass to template and write
template_values = {'unguessable_id':inst.unguessable_id,
'inst_name':inst.name,
'comments':inst.comment_set.order('-timestamp'),
'bldg_table':inst.building_set}
path = os.path.join(os.path.dirname(__file__), 'templates/view.tmpl')
self.response.out.write(template.render(path, template_values))
class UpdateInst(webapp.RequestHandler):
## Update positions of institution, using JSON from AJAX
def post(self):
## params
unguessable_id = self.request.get('unguessable_id')
model = simplejson.loads(self.request.get('model'))
comment = self.request.get('comment')
## update buildings
logging.debug('UpdateInst.post() unguessable_id: %s', unguessable_id)
instq = db.GqlQuery("SELECT * FROM Institution WHERE unguessable_id = :1", unguessable_id)
inst = instq.fetch(instq.count())[0]
if inst:
logging.debug("UpdateInst.post: %s %s" % (inst.unguessable_id, inst.name))
for bldg in inst.building_set:
logging.debug("UpdateInst.post():updating building: %s \"%s\"." % \
(bldg.name, model[bldg.name]['latlong'].strip('()')))
a,b = re.split('\s*,\s*', model[bldg.name]['latlong'].strip('()'))
bldg.pt = db.GeoPt(float(a), float(b))
bldg.put()
pass
comm = Comment()
comm.inst = inst.key()
comm.msg = comment
comm.put()
pass
else:
logging.error("UpdateInst.post: Unable to find unguessable_id = %s. Returning." % unguessable_id)
return
pass
class BuildingFeed(webapp.RequestHandler):
## Get KML^H^H JSON feed of building points
def get(self):
## get institution, along with building set
unguessable_id = self.request.get('unguessable_id')
sqlstr = "select * from Institution where unguessable_id = :1"
query = db.GqlQuery(sqlstr, unguessable_id)
if (query.count() != 1) or (not unguessable_id):
logging.error("ViewInst.get: bad number records returned for inst: %s" % query.count())
raise "wtf"
data = [];
inst = query.fetch(query.count())[0]
for bldg in inst.building_set:
#raise(str(dir(bldg.pt)))
data.append({'title':bldg.name, 'lat':bldg.pt.lat, 'lng':bldg.pt.lon})
pass
bldg_table_json = simplejson.dumps(data)
self.response.out.write(bldg_table_json)
pass
######
def main():
logging.getLogger().setLevel(logging.DEBUG)
application = webapp.WSGIApplication([('/', MainPage),
('/upload', UploadInst),
('/view', ViewInst),
('/update', UpdateInst),
('/buildingfeed', BuildingFeed)],
debug=True)
run_wsgi_app(application)
if __name__ == "__main__":
main()