-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathmain.py
145 lines (121 loc) · 4.92 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
import os, datetime, csv, StringIO
from google.appengine.ext import webapp, db
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import login_required
from google.appengine.api import users, taskqueue
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from google.appengine.dist import use_library
use_library('django', '0.96')
from django.utils import simplejson as json
class PostalCode(db.Model):
# See format at http://download.geonames.org/export/zip/
# key = country_code/postal_code
place_name = db.StringProperty()
admin_name1 = db.StringProperty()
latitude = db.StringProperty()
longitude = db.StringProperty()
accuracy = db.IntegerProperty()
history = db.TextProperty()
created = db.DateTimeProperty(auto_now_add=True)
author = db.UserProperty()
class Event(db.Model):
user = db.UserProperty()
time = db.DateTimeProperty(auto_now_add=True)
text = db.StringProperty()
class Download(db.Model):
text = db.TextProperty()
class HomePage(webapp.RequestHandler):
def get(self):
events = Event.all().order('-time').fetch(100)
self.response.out.write(template.render('template/index.html', locals()))
class FeedPage(webapp.RequestHandler):
def get(self):
events = Event.all().order('-time').fetch(100)
self.response.headers['Content-Type'] = 'text/xml'
self.response.out.write(template.render('template/atom.xml', locals()))
class CodePage(webapp.RequestHandler):
def get(self, code):
data = PostalCode.get_by_key_name(code)
user = users.get_current_user()
if not data: data = PostalCode(
key_name = code,
place_name = '',
admin_name1 = '',
latitude = '22',
longitude = '78',
accuracy = -1
)
history = json.loads(data.history or '[]')
self.response.out.write(template.render('template/postcode.html', locals()))
def post(self, code):
user = users.get_current_user()
old_data = PostalCode.get_by_key_name(code)
if old_data:
history = json.loads(old_data.history or '[]')
history.append({
'u': old_data.author and old_data.author.nickname() or 'None',
'p': old_data.place_name,
'a': old_data.admin_name1,
'x': old_data.latitude,
'y': old_data.longitude,
'z': old_data.accuracy
})
else:
history = []
data = PostalCode(
key_name = code,
place_name = self.request.get('place_name'),
admin_name1 = self.request.get('admin_name1'),
latitude = self.request.get('latitude'),
longitude = self.request.get('longitude'),
accuracy = 3,
author = user,
created = datetime.datetime.now(),
history = json.dumps(history)
)
data.put()
Event(user=user,text=code).put()
self.redirect('/' + code)
class BuildDownload(webapp.RequestHandler):
def get(self):
count = int(self.request.get('count') or 0)
last_key = self.request.get('last')
q = PostalCode.all().order('__key__')
if last_key: q = q.filter('__key__ > ', db.Key(last_key))
text = StringIO.StringIO()
out = csv.writer(text)
codes = q.fetch(500)
if not codes: return
for p in codes:
country, postcode = p.key().name().split('/')
out.writerow([country, postcode, p.place_name.encode('utf-8'),
p.admin_name1.encode('utf-8'), p.latitude, p.longitude,
p.accuracy])
Download(key_name = str(count), text=text.getvalue()).put()
taskqueue.add(url=self.request.path, method='GET', params={
'count': str(count+1),
'last': codes[-1].key(),
})
self.response.out.write('Download file #' + str(count) + ' built.')
class DownloadPage(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
for data in Download.all().order('__key__'):
self.response.out.write(data.text)
class LoginPage(webapp.RequestHandler):
def get(self):
self.redirect(users.create_login_url(self.request.get('continue', '/')))
class LogoutPage(webapp.RequestHandler):
def get(self):
self.redirect(users.create_logout_url(self.request.get('continue', '/')))
application = webapp.WSGIApplication([
('/', HomePage),
('/([A-Z][A-Z]/\d+)', CodePage),
('/feed', FeedPage),
('/_download', BuildDownload),
('/download', DownloadPage),
('/#', LoginPage),
('/logout', LogoutPage),
], debug=(os.name=='nt'))
if __name__ == '__main__':
webapp.util.run_wsgi_app(application)