-
Notifications
You must be signed in to change notification settings - Fork 11
/
contrib.py
145 lines (123 loc) · 4.28 KB
/
contrib.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
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# contrib.py
# Copyright (C) 2012 Simon Newton
# The handlers for the contrib page.
import common
import logging
import datetime
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from model import Responder, UploadedResponderInfo
class BaseContribPageHandler(webapp.RequestHandler):
"""The base handler for contrib requests."""
ALLOWED_USERS = [
]
def get(self):
self.do_request()
def post(self):
self.do_request()
def do_request(self):
user = users.get_current_user()
if not user:
self.redirect(users.create_login_url(self.request.uri))
return
if user.email() not in self.ALLOWED_USERS:
self.error(403)
return
self.HandleRequest()
class ContribPageHandler(BaseContribPageHandler):
"""Contrib menu."""
def HandleRequest(self):
template_data = {
'logout_url': users.create_logout_url("/"),
}
self.response.headers['Content-Type'] = 'text/html'
self.response.out.write(template.render('templates/contrib.tmpl',
template_data))
class AddInfoResponderHandler(BaseContribPageHandler):
"""Displays the UI for adding responder info."""
def HandleRequest(self):
# the responders without data
# we fetch ones so we don't have consistency problems
responders = self.GetMissingResponders()
if self.request.get('update'):
self.SaveChanges(responders)
template_data = {
'logout_url': users.create_logout_url("/"),
'responders': self.BuildResponderList(responders),
}
self.response.headers['Content-Type'] = 'text/html'
self.response.out.write(template.render(
'templates/contrib-add-responder-info.tmpl',
template_data))
def GetMissingResponders(self):
responders = []
for responder in Responder.all():
if responder.link and responder.image_url:
continue
responders.append(responder)
return responders
def BuildResponderList(self, responders):
output = []
for responder in responders:
if responder.link and responder.image_url:
continue
output.append({
'id': responder.device_model_id,
'image': responder.image_url,
'manufacturer': responder.manufacturer.name,
'manufacturer_id': responder.manufacturer.esta_id,
'model': responder.model_description,
'url': responder.link,
})
return output
def GetURLOrNone(self, param):
url = self.request.get(param)
if not url or not url.startswith('http://'):
return None
return url
def SaveChanges(self, responders):
added = 0
for responder in responders:
key = '%d_%d' % (responder.manufacturer.esta_id,
responder.device_model_id)
image = self.GetURLOrNone('%s_image' % key)
url = self.GetURLOrNone('%s_url' % key)
if not (image or url):
continue
responder_obj = UploadedResponderInfo(
manufacturer_id=responder.manufacturer.esta_id,
device_model_id=responder.device_model_id,
upload_time=datetime.datetime.now()
)
if image:
responder_obj.image_url = image
if url:
responder_obj.link_url = url
logging.info('Added %s' % responder.model_description)
responder_obj.put()
added += 1
common.MaybeSendEmail(added)
app = webapp.WSGIApplication(
[
('/contrib', ContribPageHandler),
('/contrib/add_responder_info', AddInfoResponderHandler),
],
debug=True)