gdritter repos frony-ritter-designs / 36b140c
auto-format Getty Ritter 3 years ago
4 changed file(s) with 85 addition(s) and 56 deletion(s). Collapse all Expand all
11 import model
22 import sqlite3
33
4
45 def main():
5 conn = sqlite3.connect('frony.db')
6 conn = sqlite3.connect("frony.db")
67 c = conn.cursor()
78
89 CATEGORIES = {}
9 for (c_id, name, nicename) in c.execute('SELECT * FROM categories'):
10 CATEGORIES[c_id] = model.Category.create(
11 name=name, nicename=nicename)
10 for (c_id, name, nicename) in c.execute("SELECT * FROM categories"):
11 CATEGORIES[c_id] = model.Category.create(name=name, nicename=nicename)
1212
1313 DESIGNS = {}
14 for row in c.execute('SELECT * FROM designs'):
14 for row in c.execute("SELECT * FROM designs"):
1515 (id, title, description, category) = row
1616 DESIGNS[id] = model.Design.create(
1717 visible_id=id,
1818 title=title,
1919 description=description,
20 category=CATEGORIES[category])
20 category=CATEGORIES[category],
21 )
2122
22 for (_, filename, d_id) in c.execute('SELECT * FROM photos'):
23 for (_, filename, d_id) in c.execute("SELECT * FROM photos"):
2324 if d_id in DESIGNS:
2425 model.Photo.create(filename=filename, design=DESIGNS[d_id])
2526
26 for (_, name, d_id) in c.execute('SELECT * FROM tags'):
27 for (_, name, d_id) in c.execute("SELECT * FROM tags"):
2728 if d_id in DESIGNS:
2829 model.Tag.create(tag_name=name, design=DESIGNS[d_id])
2930
30 for (_, name, text, title) in c.execute('SELECT * FROM pages'):
31 for (_, name, text, title) in c.execute("SELECT * FROM pages"):
3132 model.Page.create(name=name, text=text, title=title)
3233
33 if __name__ == '__main__':
34
35 if __name__ == "__main__":
3436 from model import *
37
3538 db.create_tables([Category, Design, Photo, Page, Tag])
3639 main()
7676 def get_design(id, _slug=None):
7777 design = model.Design.get(visible_id=id)
7878 return "Designs", Templates.design_page(design)
79
7980
8081 @app.route("/api/design/<int:id>/")
8182 def api_get_design(id: int):
163164 page = int(flask.request.args.get("page") or 0)
164165 min, max = db.max_page_ranges()
165166 data = db.get_all(offset=page)
166 return 'Edit Mode', Templates.edit_design_list(data)
167 return "Edit Mode", Templates.edit_design_list(data)
168
167169
168170 @app.route("/edit/design", methods=["POST"])
169171 def post_new_design():
170172 id = db.new_design()
171173 return flask.redirect(f"/edit/design/{id:05}/")
172174
175
173176 @app.route("/edit/design/<id>/")
174177 @main
175178 def edit_design(id):
176179 id = int(id)
177180 design = db.get_design(id)
178 return 'design', Templates.edit_design({
179 "id": design.id,
180 "title": design.title,
181 "tags": ' '.join(design.tags(db)),
182 "categories": design.category_list(db),
183 "description": design.description.source,
184 "photos": design.images,
185 "id_str": f"{design.id:05}",
186 })
181 return (
182 "design",
183 Templates.edit_design(
184 {
185 "id": design.id,
186 "title": design.title,
187 "tags": " ".join(design.tags(db)),
188 "categories": design.category_list(db),
189 "description": design.description.source,
190 "photos": design.images,
191 "id_str": f"{design.id:05}",
192 }
193 ),
194 )
195
187196
188197 @app.route("/edit/delete/<id>/")
189198 @main
190199 def delete_design(id):
191200 id = int(id)
192201 design = db.get_design(id)
193 return 'delete design', Templates.delete_design({
194 "title": design.title,
195 "id_str": f"{design.id:05}",
196 })
202 return (
203 "delete design",
204 Templates.delete_design({"title": design.title, "id_str": f"{design.id:05}",}),
205 )
206
197207
198208 @app.route("/edit/delete/<id>/", methods=["POST"])
199209 def do_delete_design(id):
200210 db.delete_design(int(id))
201211 return flask.redirect(f"/edit/design/")
202212
213
203214 @app.route("/edit/photo/")
204215 @main
205216 def add_photo():
206 if 'id' in flask.request.args:
207 id = flask.request.args.get('id')
217 if "id" in flask.request.args:
218 id = flask.request.args.get("id")
208219 return f"Add photo for design {id}", Templates.photo_upload({"id": id})
209220 else:
210221 return "Add generic photo", Templates.photo_upload({"id": -1})
211222
223
212224 @app.route("/edit/photo/", methods=["POST"])
213225 def do_add_photo():
214 if 'file' not in flask.request.files or 'id' not in flask.request.form:
215 return flask.redirect('/edit/photo')
216 id = int(flask.request.form['id'])
217 file = flask.request.files['file']
226 if "file" not in flask.request.files or "id" not in flask.request.form:
227 return flask.redirect("/edit/photo")
228 id = int(flask.request.form["id"])
229 file = flask.request.files["file"]
218230 photo_id = db.add_photo(file, id)
219231 if id == -1:
220 return flask.redirect(f'/edit/view-photo/{photo_id:09}')
232 return flask.redirect(f"/edit/view-photo/{photo_id:09}")
221233 else:
222 return flask.redirect(f'/edit/design/{id:09}')
234 return flask.redirect(f"/edit/design/{id:09}")
235
223236
224237 @app.route("/edit/photo/<id>/")
225238 @main
226239 def modify_photo(id):
227 design_id = flask.requests.args['design_id']
228 return 'Delete Photo', Templates.photo_delete(id, design_id)
240 design_id = flask.requests.args["design_id"]
241 return "Delete Photo", Templates.photo_delete(id, design_id)
242
229243
230244 @app.route("/edit/photo/<id>/", methods=["POST"])
231245 def do_modify_photo(id):
232 design_id = int(flask.request.form['design_id'])
246 design_id = int(flask.request.form["design_id"])
233247 db.delete_photo(id)
234 return flask.redirect(f'/edit/design/{design_id}/')
248 return flask.redirect(f"/edit/design/{design_id}/")
249
235250
236251 @app.route("/edit/view-photo/")
237252 @main
238253 def view_all_photos():
239 page = int(flask.request.args.get('page', 0))
254 page = int(flask.request.args.get("page", 0))
240255 p_min, p_max = db.max_photo_page_ranges()
241256 data = db.get_all_photos(offset=page)
242 return 'Photos', Templates.photo_list({
243 'photos': (
244 {"photo": Templates.photo_tile(p)} for p in data),
245 'page': page,
246 'show_left': page > p_min,
247 'show_right': page < p_max,
248 })
257 return (
258 "Photos",
259 Templates.photo_list(
260 {
261 "photos": ({"photo": Templates.photo_tile(p)} for p in data),
262 "page": page,
263 "show_left": page > p_min,
264 "show_right": page < p_max,
265 }
266 ),
267 )
268
249269
250270 @app.route("/edit/view-photo/<id>/")
251271 @main
256276 else:
257277 raise Exception(f"Unable to find photo {id}")
258278
279
259280 @app.route("/edit/pages/")
260281 @main
261282 def edit_page_list():
262 return 'Edit Page List', Templates.edit_pages({
263 "pages": db.get_page_list(),
264 })
283 return "Edit Page List", Templates.edit_pages({"pages": db.get_page_list(),})
284
265285
266286 @app.route("/edit/pages/<slug>/")
267287 @main
268288 def edit_page(slug):
269289 page = db.get_page(slug)
270 return f"Edit Page {slug}", Templates.edit_page({
271 "name": slug,
272 "title": page.title,
273 "text": page.text,
274 })
290 return (
291 f"Edit Page {slug}",
292 Templates.edit_page({"name": slug, "title": page.title, "text": page.text,}),
293 )
275294
276295
277296 @app.route("/edit/pages/<slug>/", methods=["POST"])
278297 def do_edit_page(slug):
279 text = flask.request.form.get('text', '')
280 title = flask.request.form.get('title', slug.capitalize())
298 text = flask.request.form.get("text", "")
299 title = flask.request.form.get("title", slug.capitalize())
281300 db.set_page(slug, title, text)
282 return flask.redirect(f'/edit/pages/{slug}')
301 return flask.redirect(f"/edit/pages/{slug}")
283302
284303
285304 # urls = (
4646 def to_slate(self):
4747 return mistune.create_markdown(renderer=mistune.AstRenderer())(self.source)
4848
49
4950 @dataclass
5051 class PageRef:
5152 page: int
5253
5354 def __iter__(self):
5455 return [self]
56
5557
5658 @dataclass
5759 class Design:
250252 ]
251253
252254 def get_all_photos_for_design(self, id):
253 return list(Image(image=d.filename, id=d.id) for d in self._db.where("photos", design_id=id))
255 return list(
256 Image(image=d.filename, id=d.id)
257 for d in self._db.where("photos", design_id=id)
258 )
254259
255260 def get_all(self, offset=0):
256261 ds = self._db.query(
11 from invoke import task
2
23
34 @task
45 def js(c):
56 c.run("yarn run build")
7
68
79 @task(js)
810 def run(c, port=8080, host="127.0.0.1"):