Fix handling of templates and implement by-cat-and-tag
Getty Ritter
4 years ago
1 | |
#!/usr/bin/python2
|
| 1 |
#!/usr/bin/env python
|
2 | 2 |
|
3 | 3 |
import flask
|
4 | 4 |
import markdown
|
|
16 | 16 |
'''
|
17 | 17 |
Local module to contain mustache templates
|
18 | 18 |
'''
|
19 | |
renderer = pystache.Renderer()
|
| 19 |
renderer = pystache.Renderer(search_dirs='templates')
|
20 | 20 |
|
21 | 21 |
def load_template(name):
|
22 | 22 |
with open(f"templates/{name}.mustache") as f:
|
|
26 | 26 |
main = load_template('main')
|
27 | 27 |
design_page = load_template('design_page')
|
28 | 28 |
design_tile = load_template('design_tile')
|
| 29 |
design_list = load_template('design_list')
|
29 | 30 |
|
30 | 31 |
|
31 | 32 |
def slugify(string):
|
|
148 | 149 |
def get_category_with_tag(cat, tag):
|
149 | 150 |
page = int(flask.request.args.get('page') or 0)
|
150 | 151 |
data = db.get_designs_by_category_and_tag(cat, tag, page)
|
151 | |
min, max = db.max_page_range_for_category(cat)
|
152 | |
return (cat.capitalize(),
|
153 | |
render.by_category_list(
|
154 | |
cat,
|
155 | |
(render.design_tile(name, pics, 0, id)
|
156 | |
for name, pics, desc, cat, id in data),
|
157 | |
page,
|
158 | |
page > min,
|
159 | |
page < (max - 1)))
|
| 152 |
|
| 153 |
rs = Templates.design_list(data)
|
| 154 |
print(rs)
|
| 155 |
return (cat.capitalize(), rs)
|
160 | 156 |
|
161 | 157 |
|
162 | 158 |
@app.route('/tag/')
|
38 | 38 |
else:
|
39 | 39 |
return self.source[:256] + '...'
|
40 | 40 |
|
| 41 |
|
41 | 42 |
class PageRef(typing.NamedTuple):
|
42 | 43 |
page: int
|
43 | 44 |
|
| 45 |
|
44 | 46 |
class Rendered(typing.NamedTuple):
|
45 | 47 |
rendered: str
|
| 48 |
|
46 | 49 |
|
47 | 50 |
class Design(typing.NamedTuple):
|
48 | 51 |
title: str
|
|
61 | 64 |
if self.images:
|
62 | 65 |
return self.images[0].thumb()
|
63 | 66 |
|
| 67 |
|
64 | 68 |
class Paginated(typing.NamedTuple):
|
65 | 69 |
next_page: typing.Optional[PageRef]
|
66 | 70 |
prev_page: typing.Optional[PageRef]
|
67 | 71 |
last_page: int
|
68 | 72 |
contents: typing.List[Design]
|
69 | 73 |
|
| 74 |
|
70 | 75 |
class Tag(typing.NamedTuple):
|
71 | 76 |
tag: str
|
72 | 77 |
count: int
|
73 | 78 |
|
74 | 79 |
def pretty(self):
|
75 | 80 |
return ' '.join(w.capitalize() for w in self.tag.split())
|
| 81 |
|
76 | 82 |
|
77 | 83 |
THUMB_SIZE = (100, 100)
|
78 | 84 |
|
|
215 | 221 |
offset=offset * self.per_page,
|
216 | 222 |
per_page=self.per_page))
|
217 | 223 |
|
218 | |
last_page=0
|
| 224 |
last_page = 10
|
219 | 225 |
return Paginated(
|
220 | 226 |
next_page=None,
|
221 | 227 |
prev_page=None,
|
| 1 |
<div class="all-designs">
|
| 2 |
{{#contents}}
|
| 3 |
{{>design_tile}}
|
| 4 |
{{/contents}}
|
| 5 |
</div>
|
| 6 |
<div class="lrnav"><ul>
|
| 7 |
<a href="?page=0"><li class="navitem">Newest</li></a>
|
| 8 |
{{#prev_page}}
|
| 9 |
<a href="?page={{page}}"><li class="navitem">Newer</li></a>
|
| 10 |
{{/prev_page}}
|
| 11 |
{{^prev_page}}
|
| 12 |
<li class="navitem greyed">Newer</li>
|
| 13 |
{{/prev_page}}
|
| 14 |
|
| 15 |
{{#next_page}}
|
| 16 |
<a href="?page={{page}}"><li class="navitem">Older</li></a>
|
| 17 |
{{/next_page}}
|
| 18 |
{{^next_page}}
|
| 19 |
<li class="navitem greyed">Older</li>
|
| 20 |
{{/next_page}}
|
| 21 |
<a href="?page={{last_page}}"><li class="navitem">Oldest</li></a>
|
| 22 |
<li class="break"></li>
|
| 23 |
</ul></div>
|
1 | 1 |
<a href="/design/{{id_str}}/{{slug}}/">
|
2 | 2 |
<li class="design-tile">
|
3 | 3 |
<div id="img">
|
4 | |
<img src="/static/photos/{{thumbnail}}" alt="$title photo" />
|
| 4 |
<img src="/static/photos/{{thumbnail}}" alt="{{title}} photo" />
|
5 | 5 |
</div>
|
6 | 6 |
<span class="title">{{title}}</span>
|
7 | 7 |
</li>
|
14 | 14 |
<meta http-equiv="Content-Type" contents="application/xhtml+xml; charset=utf-8;" />
|
15 | 15 |
<meta name="description" content="Cross-Stitch Patterns and Other Crafts from Frony Ritter Designs" />
|
16 | 16 |
<link rel="stylesheet" type="text/css" href="/static/standard.css" />
|
17 | |
<title>Frony Ritter Designs – $title</title>
|
| 17 |
<title>Frony Ritter Designs – {{title}}</title>
|
18 | 18 |
</head>
|
19 | 19 |
<body>
|
20 | 20 |
<div id="page">
|