-
Notifications
You must be signed in to change notification settings - Fork 11
/
common.py
214 lines (181 loc) · 6.45 KB
/
common.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
209
210
211
212
213
214
# 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.
#
# common.py
# Copyright (C) 2011 Simon Newton
# Common functions
from model import LastUpdateTime, Manufacturer, Pid, Product, ProductCategory, Responder, UploadedResponderInfo
from utils import StringToInt
import datetime
import logging
import memcache_keys
import textwrap
from google.appengine.api import mail
from google.appengine.api import memcache
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
def Encode(s):
"""Encode a string that may contain binary data."""
if type(s) == str:
return s.encode('string-escape')
elif type(s) == unicode:
return s.encode('unicode-escape')
else:
return s
def MaybeEncode(s):
"""Encode a string that may contain binary data if it hasn't already been
encoded."""
if type(s) == str:
if s == s.decode('string-escape'):
return s.encode('string-escape')
else:
return s
elif type(s) == unicode:
if s == s.decode('unicode-escape'):
return s.encode('unicode-escape')
else:
return s
else:
return s
def GetManufacturer(manufacturer_id):
"""Lookup a manufacturer entity by manufacturer id. The manufacturer id can
be a string in decimal or hex (prepend with 0x), or an int
Returns:
The Manufacturer entity object, or None if not found.
"""
if type(manufacturer_id) not in (int, long):
manufacturer_id = StringToInt(manufacturer_id)
query = Manufacturer.all()
query.filter('esta_id = ', manufacturer_id)
for manufacturer in query.fetch(1):
return manufacturer
return None
def LookupModelFromRequest(request):
return LookupModel(request.get('manufacturer'),
request.get('model'))
def LookupModel(manufacturer, model_id):
"""Lookup a model based on the URL params."""
if type(model_id) not in (int, long):
model_id = StringToInt(model_id)
manufacturer = GetManufacturer(manufacturer)
if manufacturer is None or model_id is None:
return None
models = Responder.all()
models.filter('device_model_id = ', model_id)
models.filter('manufacturer = ', manufacturer.key())
model_data = models.fetch(1)
if not model_data:
return None
return model_data[0]
def GetLatestSoftware(responder):
"""Find the latest software version for a responder.
Returns:
A SoftwareVersion object or None.
"""
max_version = None
max_version_info = None
for version in responder.software_version_set:
if max_version is None or version.version_id > max_version:
max_version = version.version_id
max_version_info = version
return max_version_info
def LookupProductCategory(category_id):
"""Lookup a ProductCategory entity by id.
Returns:
The entity object, or None if not found.
"""
query = ProductCategory.all()
query.filter('id = ', category_id)
categories = query.fetch(1)
if categories:
return categories[0]
else:
return None
def MaybeSendEmail(new_responder_count):
"""Send an email there were previously no responders in the moderation queue
Args:
new_responder_count: the number of responders moderation requests we just
added.
"""
if new_responder_count == 0:
return
query = UploadedResponderInfo.all()
query.filter('processed = ', False)
if query.count() == new_responder_count:
message = mail.EmailMessage(
sender='RDM Site <[email protected]>',
subject='Pending Moderation Requests',
to='<[email protected]>',
)
message.body = textwrap.dedent("""\
There are new responders in the moderation queue.
Please visit http://rdm.openlighting.org/admin/moderate_responder_data
""")
message.send()
class BasePageHandler(webapp.RequestHandler):
"""The base class for all page requests.
This adds the index info snippit to the bottom of the page.
"""
ESTA_ID = 0
def GetTemplateData(self):
"""Subclasses override this."""
return {}
def get(self):
output = self.IndexInfo()
page_data = self.GetTemplateData()
if page_data is not None:
output.update(page_data)
self.response.headers['Content-Type'] = 'text/html'
self.response.out.write(template.render(self.TEMPLATE, output))
def ManufacturerPidCount(self):
"""Return the number of manufacturer PIDs."""
manufacturer_pids = memcache.get(memcache_keys.MANUFACTURER_PID_COUNT_KEY)
if manufacturer_pids is None:
manufacturer_pids = 0
for pid in Pid.all():
if pid.manufacturer.esta_id != self.ESTA_ID:
manufacturer_pids += 1
if not memcache.add(memcache_keys.MANUFACTURER_PID_COUNT_KEY,
manufacturer_pids):
logging.error("Memcache set failed.")
return manufacturer_pids
def ProductCount(self):
"""Return the number of product."""
product_count = memcache.get(memcache_keys.PRODUCT_COUNT_KEY)
if product_count is None:
product_count = Responder.all().count() + Product.all().count()
if not memcache.add(memcache_keys.PRODUCT_COUNT_KEY, product_count):
logging.error("Memcache set failed.")
return product_count
def IndexInfo(self):
"""Get the information about the index.
Returns a dict in the form:
{'last_updated': ,
'manufacturer_pid_count': ,
'model_count': ,
}
"""
output = memcache.get(memcache_keys.INDEX_INFO)
if not output:
output = {'last_updated': None}
results = LastUpdateTime.all()
results.order('-update_time')
update_timestamp = results.fetch(1)
if update_timestamp:
output['last_updated'] = datetime.datetime(
*update_timestamp[0].update_time.timetuple()[0:6])
output['manufacturer_pid_count'] = self.ManufacturerPidCount()
output['product_count'] = self.ProductCount()
memcache.set(memcache_keys.INDEX_INFO, output)
return output