gdritter repos frony-ritter-designs / 2e60209
Update by-charts as well Getty Ritter 4 years ago
2 changed file(s) with 38 addition(s) and 47 deletion(s). Collapse all Expand all
136136 def get_category(cat):
137137 page = int(flask.request.args.get('page') or 0)
138138 data = db.get_designs_by_category(cat, page)
139 min, max = db.max_page_range_for_category(cat)
140 return (cat.capitalize(),
141 render.by_category_list(
142 cat,
143 (render.design_tile(name, pics, 0, id)
144 for name, pics, desc, cat, id in data),
145 page,
146 page > min,
147 page < (max - 1)))
139 return (cat.capitalize(), Templates.design_list(data))
140 # min, max = db.max_page_range_for_category(cat)
141 # return (cat.capitalize(),
142 # render.by_category_list(
143 # cat,
144 # (render.design_tile(name, pics, 0, id)
145 # for name, pics, desc, cat, id in data),
146 # page,
147 # page > min,
148 # page < (max - 1)))
148149
149150
150151 @app.route('/category/<cat>/tag/<tag>/')
152153 def get_category_with_tag(cat, tag):
153154 page = int(flask.request.args.get('page') or 0)
154155 data = db.get_designs_by_category_and_tag(cat, tag, page)
155
156 rs = Templates.design_list(data)
157 return (cat.capitalize(), rs)
156 return (cat.capitalize(), Templates.design_list(data))
158157
159158
160159 @app.route('/tag/')
4646 return [self]
4747
4848
49 class Rendered(typing.NamedTuple):
50 rendered: str
51
52
5349 class Design(typing.NamedTuple):
5450 title: str
5551 images: typing.List[Image]
6662 def thumbnail(self):
6763 if self.images:
6864 return self.images[0].thumb()
65
66 @classmethod
67 def list(cls, query_results) -> typing.List['Design']:
68 return [
69 cls(
70 title=d.title,
71 images=[Image(d.image)],
72 description=d.description,
73 category=d.cat_name,
74 id=d.id,
75 ) for d in query_results]
6976
7077
7178 class Paginated(typing.NamedTuple):
201208 category=self.get_category_name(d.category),
202209 id=d.id) for d in ds)
203210
204 def get_new_charts(self, pp):
205 ds = list(self._db.select('designs',
206 where='category = 8',
207 limit=pp,
208 order='id DESC'))
209 return ((d.title,
210 self.get_picture(d.id),
211 d.description,
212 self.get_category_name(d.category),
213 d.id) for d in ds)
214
215211 def get_designs_by_category(self, cat, offset=0):
216 cat = self.get_category_id(cat)
217 ds = self._db.where('designs',
218 category=cat,
219 offset=offset * self.per_page,
220 limit=self.per_page,
221 order='id DESC')
222 return ((d.title,
223 self.get_picture(d.id),
224 d.description,
225 cat,
226 d.id) for d in ds)
212 ds = self._db.query(
213 '''select d.title, d.description, d.id, c.name as cat_name,
214 (select filename from photos where photos.design_id = d.id limit 1) as image
215 from designs d, categories c
216 where d.category = c.id
217 and c.name = $cat
218 order by d.id desc
219 limit $per_page offset $offset''',
220 vars={"cat": cat, "per_page": PER_PAGE, "offset": offset},
221 )
222 total = self._db.query(
223 '''select count(*) as c from designs d, categories c
224 where d.category = c.id and c.name = $cat''',
225 vars={"cat": cat})
226 return Paginated.paginate(offset, total[0].c, Design.list(ds))
227227
228228 def get_designs_by_category_and_tag(self, cat, tag, offset=0):
229229 ds = self._db.query(
247247 and tags.tag_name = $tag
248248 and designs.id = tags.design_id''',
249249 vars=dict(tag=tag, cat=cat))
250 contents = [
251 Design(
252 title=d.title,
253 images=[Image(d.image)],
254 description=d.description,
255 category=d.cat_name,
256 id=d.id)
257 for d in ds]
258 return Paginated.paginate(offset, total[0].c, contents)
250 return Paginated.paginate(offset, total[0].c, Design.list(ds))
259251
260252 def new_design(self):
261253 new_id = self._db.query(