gdritter repos frony-ritter-designs / 3a731f9
Start trying to do new things with pagination Getty Ritter 4 years ago
2 changed file(s) with 38 addition(s) and 17 deletion(s). Collapse all Expand all
3939 return ''.join(process(c) for c in string)[:40]
4040
4141
42 def thumb(url):
43 if url:
44 return url[:-4] + '_thumb' + url[-4:]
42 def thumb(img):
43 return img.thumb()
4544
4645
4746 def five(n):
2121 image: str
2222
2323 def thumb(self):
24 print(self.image)
2425 return self.image[:-4] + '_thumb' + self.image[-4:]
2526
2627
3738 else:
3839 return self.source[:256] + '...'
3940
41 class PageRef(typing.NamedTuple):
42 page: int
43
44 class Rendered(typing.NamedTuple):
45 rendered: str
4046
4147 class Design(typing.NamedTuple):
4248 title: str
5561 if self.images:
5662 return self.images[0].thumb()
5763
64 class Paginated(typing.NamedTuple):
65 next_page: typing.Optional[PageRef]
66 prev_page: typing.Optional[PageRef]
67 last_page: int
68 contents: typing.List[Design]
69
5870 class Tag(typing.NamedTuple):
5971 tag: str
6072 count: int
6880 class DB:
6981
7082 def __init__(self, per_page=16):
71 self._db = web.database(dbn='sqlite',
72 db='frony.db')
83 self._db = web.database(dbn='sqlite', db='frony.db')
7384 self.per_page = per_page
7485 self.categories = {}
7586 self.photo_num = self.get_max_photo_num() + 1
189200 d.id) for d in ds)
190201
191202 def get_designs_by_category_and_tag(self, cat, tag, offset=0):
192 cid = self.get_category_id(cat)
193203 ds = self._db.query(
194 '''select * from designs, tags
195 where tags.tag_name = $tag
196 and category=$cat
197 and designs.id = tags.design_id
198 order by designs.id desc
204 '''select d.title, d.description, d.id, c.name as cat_name,
205 (select filename from photos where photos.design_id = d.id limit 1) as image
206 from designs d, tags t, categories c
207 where t.tag_name = $tag
208 and d.category=c.id
209 and c.name = $cat
210 and d.id = t.design_id
211 order by d.id desc
199212 limit $per_page offset $offset''',
200213 vars=dict(tag=tag,
201 cat=cid,
214 cat=cat,
202215 offset=offset * self.per_page,
203216 per_page=self.per_page))
204217
205 return ((d.title,
206 self.get_picture(d.design_id),
207 d.description,
208 cat,
209 d.design_id) for d in ds)
218 last_page=0
219 return Paginated(
220 next_page=None,
221 prev_page=None,
222 last_page=last_page,
223 contents=[
224 Design(
225 title=d.title,
226 images=[Image(d.image)],
227 description=d.description,
228 category=d.cat_name,
229 id=d.id)
230 for d in ds],
231 )
210232
211233 def new_design(self):
212234 new_id = self._db.query(