5 | 5 |
|
6 | 6 |
import typing
|
7 | 7 |
|
| 8 |
PER_PAGE = 16
|
8 | 9 |
|
9 | 10 |
def slugify(string):
|
10 | 11 |
def process(char):
|
|
21 | 22 |
image: str
|
22 | 23 |
|
23 | 24 |
def thumb(self):
|
24 | |
print(self.image)
|
25 | 25 |
return self.image[:-4] + '_thumb' + self.image[-4:]
|
26 | 26 |
|
27 | 27 |
|
|
41 | 41 |
|
42 | 42 |
class PageRef(typing.NamedTuple):
|
43 | 43 |
page: int
|
| 44 |
|
| 45 |
def __iter__(self):
|
| 46 |
return [self]
|
44 | 47 |
|
45 | 48 |
|
46 | 49 |
class Rendered(typing.NamedTuple):
|
|
66 | 69 |
|
67 | 70 |
|
68 | 71 |
class Paginated(typing.NamedTuple):
|
69 | |
next_page: typing.Optional[PageRef]
|
70 | |
prev_page: typing.Optional[PageRef]
|
| 72 |
next_page: typing.Optional[dict]
|
| 73 |
prev_page: typing.Optional[dict]
|
71 | 74 |
last_page: int
|
72 | 75 |
contents: typing.List[Design]
|
| 76 |
|
| 77 |
@classmethod
|
| 78 |
def paginate(
|
| 79 |
cls,
|
| 80 |
offset: int,
|
| 81 |
total: int,
|
| 82 |
contents: typing.List[Design],
|
| 83 |
) -> 'Paginated':
|
| 84 |
last_page = total // PER_PAGE
|
| 85 |
next_page = PageRef(page=offset+1) if offset < last_page else None
|
| 86 |
prev_page = PageRef(page=offset-1) if offset > 0 else None
|
| 87 |
return cls(
|
| 88 |
next_page=next_page,
|
| 89 |
prev_page=prev_page,
|
| 90 |
last_page=last_page,
|
| 91 |
contents=contents,
|
| 92 |
)
|
73 | 93 |
|
74 | 94 |
|
75 | 95 |
class Tag(typing.NamedTuple):
|
|
87 | 107 |
|
88 | 108 |
def __init__(self, per_page=16):
|
89 | 109 |
self._db = web.database(dbn='sqlite', db='frony.db')
|
90 | |
self.per_page = per_page
|
| 110 |
self.per_page = PER_PAGE
|
91 | 111 |
self.categories = {}
|
92 | 112 |
self.photo_num = self.get_max_photo_num() + 1
|
93 | 113 |
|
|
220 | 240 |
cat=cat,
|
221 | 241 |
offset=offset * self.per_page,
|
222 | 242 |
per_page=self.per_page))
|
223 | |
|
224 | |
last_page = 10
|
225 | |
return Paginated(
|
226 | |
next_page=None,
|
227 | |
prev_page=None,
|
228 | |
last_page=last_page,
|
229 | |
contents=[
|
230 | |
Design(
|
231 | |
title=d.title,
|
232 | |
images=[Image(d.image)],
|
233 | |
description=d.description,
|
234 | |
category=d.cat_name,
|
235 | |
id=d.id)
|
236 | |
for d in ds],
|
237 | |
)
|
| 243 |
total = self._db.query(
|
| 244 |
'''select count(*) as c from designs, tags, categories
|
| 245 |
where designs.category = categories.id
|
| 246 |
and categories.name = $cat
|
| 247 |
and tags.tag_name = $tag
|
| 248 |
and designs.id = tags.design_id''',
|
| 249 |
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)
|
238 | 259 |
|
239 | 260 |
def new_design(self):
|
240 | 261 |
new_id = self._db.query(
|