-
Notifications
You must be signed in to change notification settings - Fork 11
/
incoming_handler.py
198 lines (163 loc) · 5.63 KB
/
incoming_handler.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
# 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.
#
# incoming_handler.py
# Copyright (C) 2011 Simon Newton
# PID search / display handlers.
import common
import datetime
import json
import logging
from model import Manufacturer, UploadedResponderInfo
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
class HandleModelData(webapp.RequestHandler):
"""Handle model data uploads.
Requests are in the form:
model_data=<model data as a python dict>
"""
TEMPLATE = 'templates/upload_model_confirm.tmpl'
UPLOAD_TEMPLATE = 'templates/upload_model.tmpl'
def get(self):
self.response.headers['Content-Type'] = 'text/html'
self.response.out.write(template.render(self.UPLOAD_TEMPLATE, {}))
def get_model_data(self):
file_data = self.request.get("model_file")
if file_data:
return file_data
return self.request.get('model_data')
def post(self):
# try a file upload first
model_data = self.get_model_data()
logging.info(model_data)
responders = self.VerifyAndStoreData(model_data)
logging.info('Responders is %s' % responders)
if responders:
common.MaybeSendEmail(len(responders))
self.response.headers['Content-Type'] = 'text/html'
self.response.out.write(
template.render(self.TEMPLATE, {'responders': responders}))
def VerifyAndStoreData(self, data):
"""Check the data look reasonable and if it does, store it.
Returns:
A list of dicts in the form:
[{
'key': key,
}]
"""
if not data:
return []
try:
evaled_data = eval(data, {})
except Exception as e:
logging.info(data)
logging.error(e)
return []
responder_obj_ids = []
for manufacturer_id, responders in evaled_data.iteritems():
try:
manufacturer_id = int(manufacturer_id)
except ValueError:
logging.error('Invalid manufacturer id %s' % manufacturer_id)
continue
# See if we can get the manufacturer name
manufacturer_name = None
manufacturer_query = Manufacturer.all()
manufacturer_query.filter('esta_id = ', manufacturer_id)
results = manufacturer_query.fetch(1)
if results:
manufacturer_name = results[0].name
for responder in responders:
if 'device_model' not in responder:
logging.error('Missing device_model from data')
continue
try:
device_model_id = int(responder['device_model'])
except ValueError:
logging.error('Invalid device model %s' % responder['device_model'])
continue
# ok, that's all the required fields
del responder['device_model']
responder_obj = UploadedResponderInfo(
manufacturer_id=manufacturer_id,
device_model_id=device_model_id,
info=str(responder),
upload_time=datetime.datetime.now()
)
responder_obj.put()
responder_obj_ids.append({
'device_model_id': device_model_id,
'key': str(responder_obj.key()),
'manufacturer': manufacturer_name,
'manufacturer_id': manufacturer_id,
'model_description': responder.get('model_description', ''),
})
return responder_obj_ids
class UpdateModelData(webapp.RequestHandler):
"""Handle updates to model data.
Requests are in the form:
data=<model data as json>
"""
TEMPLATE = 'templates/upload_model_confirm.tmpl'
def get(self):
self.response.headers['Content-Type'] = 'text/json'
self.response.out.write(json.dumps({}))
def post(self):
new_data = self.request.get('data')
email = self.request.get('email')
errors = self.UpdateResponders(new_data, email)
output = {
'ok': errors == [],
'errors': errors,
}
self.response.out.write(json.dumps(output))
def UpdateResponders(self, data, email):
if not data:
return []
try:
evaled_data = eval(data, {})
except Exception as e:
logging.info(data)
logging.error(e)
return ['Bad Data']
logging.info(evaled_data)
for responder_data in evaled_data:
key = responder_data.get('key')
if not key:
continue
uploaded_data = UploadedResponderInfo.get(key)
if not uploaded_data:
logging.error('Invalid key %s' % key)
continue
save = False
url = responder_data.get('url')
if (uploaded_data.link_url is None and url and url != 'http://'):
uploaded_data.link_url = url
save = True
url = responder_data.get('image')
if (uploaded_data.image_url is None and url and url != 'http://'):
uploaded_data.image_url = url
save = True
if uploaded_data.email_or_name is None and email:
uploaded_data.email_or_name = email
save = True
if save:
uploaded_data.put()
return []
incoming_application = webapp.WSGIApplication(
[
('/incoming/model_data', HandleModelData),
('/incoming/update_model_data', UpdateModelData),
],
debug=True)