gdritter repos frony-ritter-designs / 47eb1e0
Use LinkList for tags as well Getty Ritter 4 years ago
2 changed file(s) with 19 addition(s) and 38 deletion(s). Collapse all Expand all
2828
2929 main = load_template("main")
3030 design_page = load_template("design_page")
31 design_tile = load_template("design_tile")
3231 design_list = load_template("design_list")
3332 text_list = load_template("text_list")
34
35
36 def slugify(string):
37 def process(char):
38 if char in "- \n\t\f":
39 return "-"
40 elif char.isalpha():
41 return char.lower()
42 else:
43 return ""
44
45 return "".join(process(c) for c in string)[:40]
4633
4734
4835 def thumb(img):
5845 "old_templates/",
5946 globals=dict(
6047 markdown=markdown.markdown,
61 slugify=slugify,
48 slugify=storage.slugify,
6249 all_categories=db.all_categories,
6350 thumb=thumb,
6451 five=five,
6653 )
6754
6855
69 def snip(text):
70 if len(text) < 256:
71 return text
72 else:
73 return text[:256] + "..."
74
75
7656 def main(func):
7757 def new_func(*args, **kwargs):
7858 title, content = func(*args, **kwargs)
9171
9272 @app.route("/design/<id>/<slug>/")
9373 @main
94 def get_design(id, slug=None):
74 def get_design(id, _slug=None):
9575 design = db.get_design(int(id))
9676 return "Designs", Templates.design_page(design)
9777
122102 def get_all_designs():
123103 page = int(flask.request.args.get("page") or 0)
124104 data = db.get_all(page)
125 return ("Designs", Templates.design_list(data))
105 return "Designs", Templates.design_list(data)
126106
127107
128108 @app.route("/category/")
137117 def get_category(cat):
138118 page = int(flask.request.args.get("page") or 0)
139119 data = db.get_designs_by_category(cat, page)
140 return (cat.capitalize(), Templates.design_list(data))
120 return cat.capitalize(), Templates.design_list(data)
141121
142122
143123 @app.route("/category/<cat>/tag/<tag>/")
145125 def get_category_with_tag(cat, tag):
146126 page = int(flask.request.args.get("page") or 0)
147127 data = db.get_designs_by_category_and_tag(cat, tag, page)
148 return (cat.capitalize(), Templates.design_list(data))
128 return cat.capitalize(), Templates.design_list(data)
149129
150130
151131 @app.route("/tag/")
152132 @main
153133 def get_all_tags():
154 tags = ((t.tag, t.pretty(), t.count) for t in sorted(db.get_all_tags()))
155 return "All Tags", render.select_tag(tags)
134 tags = db.get_all_tags()
135 return "All Tags", Templates.text_list(tags)
156136
157137
158138 @app.route("/tag/<tag>/")
161141 page = int(flask.request.args.get("page") or 0)
162142 data = db.get_designs_by_tag(tag, page)
163143 pretty_tag = " ".join(w.capitalize() for w in tag.split("_"))
164 return (pretty_tag, Templates.design_list(data))
144 return pretty_tag, Templates.design_list(data)
165145
166146
167147 @app.route("/<name>/")
168148 @main
169149 def get_page(name):
170150 page = db.get_page(name)
171 return (page.title, markdown.markdown(page.text))
151 return page.title, markdown.markdown(page.text)
172152
173153
174154 # class admin:
115115 @classmethod
116116 def from_category(cls, c):
117117 return cls(name=c.nicename, url=f"/category/{c.name}")
118
119 @classmethod
120 def from_tag(cls, t):
121 nicename = " ".join(w.capitalize() for w in t.tag_name.split())
122 return cls(name=f"{nicename} ({t.n})", url=f"/tag/{t.tag_name}")
118123
119124
120125 class LinkList(typing.NamedTuple):
324329 return (0, 1 + (num_designs // self.per_page))
325330
326331 def get_all_tags(self):
327 return [
328 Tag(t.tag_name, t.n)
329 for t in self._db.query(
330 "select t.tag_name, count(*) as n "
331 "from designs d, tags t "
332 "where d.id = t.design_id "
333 "group by t.tag_name"
334 )
335 ]
332 tags = self._db.query(
333 """select t.tag_name, count(*) as n from designs d, tags t
334 where d.id = t.design_id group by t.tag_name"""
335 )
336 return LinkList(elements=sorted([Link.from_tag(t) for t in tags]))
336337
337338 def get_designs_by_tag(self, tag, offset=0):
338339 ds = self._db.query(