-
Notifications
You must be signed in to change notification settings - Fork 1
/
admin.py
82 lines (61 loc) · 2.06 KB
/
admin.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
from google.cloud import datastore
import config
import datetime
import google.appengine.api.users as users
import jinja2
import logging
import main
import os
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
if not users.is_current_user_admin():
return
client = datastore.Client(config.PROJECT_ID)
query = client.query(kind='Signature')
signatures = list(query.fetch())
count = len(signatures)
known_emails = []
for signature in signatures:
if signature['email'] in known_emails:
logging.info(signature['email'])
known_emails.append(signature['email'])
name_split = signature['full_name'].split(' ')
# Handle initials like "A J Parker"
if (len(name_split[0]) == 1 and len(name_split[1]) == 1):
signature['first_name'] = name_split.pop(0) + ' ' + name_split.pop(0)
else:
signature['first_name'] = name_split.pop(0)
signature['last_name'] = ' '.join(name_split)
template_values = {
'signatures': signatures,
'count': count
}
template = config.JINJA_ENVIRONMENT.get_template('templates/admin.html')
self.response.write(template.render(template_values))
def chunks(l, n):
chunks = []
# For item i in a range that is a length of l,
for i in range(0, len(l), n):
# Create an index range for l of n items:
chunks.append(l[i:i+n])
return chunks
class UpdateEntries(webapp2.RequestHandler):
def get(self):
if not users.is_current_user_admin():
return
# client = datastore.Client(config.PROJECT_ID)
# query = client.query(kind='Signature')
# signatures = list(query.fetch())
# chunk_lists = chunks(signatures, 500)
# for chunk in chunk_lists:
# for signature in chunk:
# signature.update({
# 'primary_district': unicode(main.get_primary_district(signature['neighborhood']))
# })
# client.put_multi(chunk)
# self.response.write('Done.')
app = webapp2.WSGIApplication([
('/adminz', MainPage),
('/adminz/update', UpdateEntries),
], debug=False)