gdritter repos frony-ritter-designs / 58a74d4
switch more stuff to Peewee Getty Ritter 3 years ago
3 changed file(s) with 110 addition(s) and 20 deletion(s). Collapse all Expand all
4646
4747
4848 db = storage.DB()
49 PER_PAGE = 16
4950
5051
5152 def main(func):
100101 @main
101102 def get_all_designs():
102103 page = int(flask.request.args.get("page") or 0)
103 data = db.get_all(page)
104 data = model.Design.get_where(page=page)
104105 return "Designs", Templates.design_list(data)
105106
106107
107108 @app.route("/category/")
108109 @main
109110 def get_all_categories():
110 categories = db.all_categories()
111 return "Category", Templates.text_list(categories)
111 return "Category", Templates.text_list(model.Category.all())
112112
113113
114114 @app.route("/category/<cat>/")
115115 @main
116116 def get_category(cat):
117117 page = int(flask.request.args.get("page") or 0)
118 data = db.get_designs_by_category(cat, page)
118 data = model.Design.get_where(category=cat, page=page)
119119 return cat.capitalize(), Templates.design_list(data)
120120
121121
123123 @main
124124 def get_category_with_tag(cat, tag):
125125 page = int(flask.request.args.get("page") or 0)
126 data = db.get_designs_by_category_and_tag(cat, tag, page)
126 data = model.Design.get_where(tag=tag, category=cat, page=page)
127127 return cat.capitalize(), Templates.design_list(data)
128128
129129
130130 @app.route("/tag/")
131131 @main
132132 def get_all_tags():
133 tags = db.get_all_tags()
134 return "All Tags", Templates.text_list(tags)
133 return "All Tags", Templates.text_list(model.Tag.all())
135134
136135
137136 @app.route("/tag/<tag>/")
138137 @main
139138 def get_tag(tag):
140139 page = int(flask.request.args.get("page") or 0)
141 data = db.get_designs_by_tag(tag, page)
140 data = model.Design.get_where(tag=tag, page=page)
142141 pretty_tag = " ".join(w.capitalize() for w in tag.split("_"))
143142 return pretty_tag, Templates.design_list(data)
144143
154153 @main
155154 def edit_all_designs():
156155 page = int(flask.request.args.get("page") or 0)
157 min, max = db.max_page_ranges()
158 data = db.get_all(offset=page)
156 data = model.Design.get_where(page=page)
159157 return "Edit Mode", Templates.edit_design_list(data)
160158
161159
169167 @main
170168 def edit_design(id):
171169 id = int(id)
172 design = db.get_design(id)
170 # design = db.get_design(id)
171 design = model.Design.get(id=id)
173172 return (
174173 "design",
175174 Templates.edit_design(
176175 {
177176 "id": design.id,
178177 "title": design.title,
179 "tags": " ".join(design.tags(db)),
180 "categories": design.category_list(db),
181 "description": design.description.source,
182 "photos": design.images,
178 "tags": " ".join(t.tag_name for t in design.tags),
179 "categories": design.category_list(),
180 "description": design.description,
181 "photos": ({"image": d} for d in design.photos),
183182 "id_str": f"{design.id:05}",
184183 }
185184 ),
190189 @main
191190 def delete_design(id):
192191 id = int(id)
193 design = db.get_design(id)
192 design = model.Design.get(id=id)
194193 return (
195194 "delete design",
196195 Templates.delete_design({"title": design.title, "id_str": f"{design.id:05}",}),
1 from dataclasses import dataclass
12 import markdown
23 import peewee
34 import typing
45
56 db = peewee.SqliteDatabase("new.db")
7 PER_PAGE = 16
8
9 def slugify(string):
10 def process(char):
11 if char in "- \n\t\f":
12 return "-"
13 elif char.isalpha():
14 return char.lower()
15 else:
16 return ""
17
18 return "".join(process(c) for c in string)[:40]
619
720
821 class Model(peewee.Model):
1427 name = peewee.TextField()
1528 nicename = peewee.TextField()
1629
30 @classmethod
31 def all(klass):
32 elements = ({"url": f"/category/{c.name}", "name": c.nicename} for c in klass.select())
33 return {"elements": elements}
34
1735
1836 class Design(Model):
1937 visible_id = peewee.IntegerField()
2442 def rendered(self):
2543 return markdown.markdown(self.description)
2644
45 def id_str(self):
46 return f'{self.visible_id:05}'
47
48 def slug(self):
49 return slugify(self.title)
50
51 def thumbnail(self):
52 return Photo.get(Photo.design==self).thumb()
53
54 def category_list(self):
55 categories = Category.select()
56 return [
57 {"name": c.name, "selected": c == self.category}
58 for c in categories
59 ]
60
61 @classmethod
62 def get_all(klass, page=0):
63 designs = klass.select().paginate(page, PER_PAGE)
64 return Paginated.paginate(
65 page,
66 klass.select().count(),
67 designs,
68 )
69
70 @classmethod
71 def get_where(klass, *, tag=None, category=None, page=0):
72 query = klass.select(klass)
73 if tag is not None:
74 query = query.join(Tag).where(Tag.tag_name == tag)
75 if category is not None:
76 query = query.switch(klass).join(Category).where(Category.name == category)
77 query = query.group_by(klass).order_by(klass.id)
78 designs = query.paginate(page, PER_PAGE)
79 return Paginated.paginate(
80 page,
81 query.count(),
82 designs,
83 )
84
2785
2886 class Photo(Model):
2987 filename = peewee.TextField()
3088 design = peewee.ForeignKeyField(Design, backref="photos")
89
90 def thumb(self):
91 return self.filename[:-4] + "_thumb" + self.filename[-4:]
3192
3293
3394 class Page(Model):
43104 tag_name = peewee.TextField()
44105 design = peewee.ForeignKeyField(Design, backref="tags")
45106
107 @classmethod
108 def all(klass):
109 elements = ({"url": f"/tag/{t.tag_name}", "name": t.tag_name} for t in klass.select(klass.tag_name).distinct().order_by(klass.tag_name))
110 return {"elements": elements}
111
112
46113
47114 class PageRef(typing.NamedTuple):
48115 page: int
51118 return [self]
52119
53120
54 class Paginated(typing.NamedTuple):
55 next_page: typing.Optional[PageRef]
56 prev_page: typing.Optional[PageRef]
121 @dataclass
122 class Paginated:
123 next_page: typing.Optional[dict]
124 prev_page: typing.Optional[dict]
57125 last_page: int
58126 contents: typing.List[Design]
127
128 @classmethod
129 def paginate(
130 cls, offset: int, total: int, contents: typing.List[Design],
131 ) -> "Paginated":
132 last_page = (total // PER_PAGE) + 1
133 next_page = PageRef(page=offset + 1) if offset < last_page else None
134 prev_page = PageRef(page=offset - 1) if offset > 1 else None
135 return cls(
136 next_page=next_page,
137 prev_page=prev_page,
138 last_page=last_page,
139 contents=contents,
140 )
141
142
143 # @dataclass
144 # class Paginated:
145 # next_page: typing.Optional[dict]
146 # prev_page: typing.Optional[dict]
147 # last_page: int
148 # contents: typing.List[Design]
149
44 {{/contents}}
55 </div>
66 <div class="nav round">
7 <a href="?page=0">Newest</a>
7 <a href="?page=1">Newest</a>
88 {{#prev_page}}
99 <a href="?page={{page}}">Newer</a>
1010 {{/prev_page}}