-
Notifications
You must be signed in to change notification settings - Fork 11
/
product_handler.py
245 lines (198 loc) · 6.55 KB
/
product_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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# 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.
#
# product_handler.py
# Copyright (C) 2012 Simon Newton
# Product search / display handlers
import common
import logging
from model import Controller, Node, ProductTag, Software, Splitter
from utils import StringToInt
from google.appengine.api import images
from google.appengine.ext import webapp
class BrowseProducts(common.BasePageHandler):
"""Show products & pictures.
The sub class products a ProductType() method which tells us what model class
to use.
"""
TEMPLATE = 'templates/browse_products.tmpl'
ROWS = 4
COLUMNS = 4
RESULTS_PER_PAGE = ROWS * COLUMNS
def GetAll(self, page):
"""
Returns:
count, products
"""
query = self.ProductType().all()
query.order('-image_url')
total = query.count()
products = query.fetch(limit=self.RESULTS_PER_PAGE,
offset=page * self.RESULTS_PER_PAGE)
return total, products
def FilterByTag(self, page, tag):
query = ProductTag.all()
query.filter('label = ', tag)
query.filter('product_type = ', self.ProductType().class_name())
tags = query.fetch(1)
if not tags:
return 0, []
query = tags[0].product_set
total = query.count()
tag_relationships = query.fetch(limit=self.RESULTS_PER_PAGE,
offset=(page * self.RESULTS_PER_PAGE))
return total, [r.product for r in tag_relationships]
def FilterByManufacturer(self, page, manufacturer):
manufacturer_id = StringToInt(manufacturer)
manufacturer = common.GetManufacturer(manufacturer_id)
if manufacturer is None:
return 0, []
query = self.ProductType().all()
query.filter('manufacturer = ', manufacturer.key())
query.order('name')
total = query.count()
return total, query.fetch(None)
def GetTemplateData(self):
page = StringToInt(self.request.get('page'), False)
if page is None:
page = 1
# 0 offset
page -= 1
data = {
'page_number': page + 1, # back to 1 offset
'product_type': self.ProductType().class_name().lower(),
}
total = 0
products = []
tag = self.request.get('tag')
manufacturer = self.request.get('manufacturer')
if tag:
data['tag'] = tag
total, products = self.FilterByTag(page, tag)
elif manufacturer:
data['manufacturer'] = manufacturer
total, products = self.FilterByManufacturer(page, manufacturer)
else:
total, products = self.GetAll(page)
data['total'] = total
rows = []
for product, index in zip(products, range(len(products))):
if index % self.COLUMNS == 0:
rows.append([])
output = {
'name': product.name,
'key': product.key(),
}
if product.image_data:
serving_url = product.image_serving_url
if not serving_url:
serving_url = images.get_serving_url(product.image_data.key())
product.image_serving_url = serving_url
product.put()
logging.info('saving %s' % serving_url)
output['image_key'] = serving_url
rows[-1].append(output)
start = page * self.RESULTS_PER_PAGE
data['end'] = start + len(products)
data['product_rows'] = rows
data['start'] = start + 1
if page:
data['previous'] = page
if start + len(products) < total:
data['next'] = page + 2
return data
class BaseSearchHandler(common.BasePageHandler):
"""The base class for product searches."""
def Init(self):
pass
def GetTemplateData(self):
self.Init()
data = self.GetSearchData()
data['products'] = self.GetResults()
data['product_type'] = self.ProductType().class_name().lower()
return data
class DisplayProduct(common.BasePageHandler):
"""Display information about a particular product.
The sub class products a ProductType() method which tells us what model class
to use.
"""
TEMPLATE = 'templates/display_product.tmpl'
def GetTemplateData(self):
product = self.ProductType().get(self.request.get('key'))
if not product:
self.error(404)
return
self.response.headers['Content-Type'] = 'text/plain'
output = {
'name': product.name,
'manufacturer': product.manufacturer.name,
'manufacturer_id': product.manufacturer.esta_id,
}
# link is optional
if product.link:
output['link'] = product.link
# tags
for tag in product.tag_set:
tags = output.setdefault('tags', [])
tags.append(tag.tag.label)
if product.image_data:
serving_url = product.image_serving_url
if not serving_url:
serving_url = images.get_serving_url(product.image_data.key())
product.image_serving_url = serving_url
product.put()
logging.info('saving %s' % serving_url)
output['image_key'] = serving_url
return output
# The classes for each Product type.
# Controllers
class BrowseController(BrowseProducts):
def ProductType(self):
return Controller
class DisplayController(DisplayProduct):
def ProductType(self):
return Controller
# Nodes
class BrowseNodes(BrowseProducts):
def ProductType(self):
return Node
class DisplayNode(DisplayProduct):
def ProductType(self):
return Node
# Software
class BrowseSoftware(BrowseProducts):
def ProductType(self):
return Software
class DisplaySoftware(DisplayProduct):
def ProductType(self):
return Software
# Splitters
class BrowseSplitters(BrowseProducts):
def ProductType(self):
return Splitter
class DisplaySplitters(DisplayProduct):
def ProductType(self):
return Splitter
app = webapp.WSGIApplication(
[
('/controller/browse', BrowseController),
('/controller/display', DisplayController),
('/node/browse', BrowseNodes),
('/node/display', DisplayNode),
('/software/browse', BrowseSoftware),
('/software/display', DisplaySoftware),
('/splitter/browse', BrowseSplitters),
('/splitter/display', DisplaySplitters),
],
debug=True)