gdritter repos frony-ritter-designs / 687ccb6
Peewee image uploading and deletion Getty Ritter 3 years ago
2 changed file(s) with 41 addition(s) and 8 deletion(s). Collapse all Expand all
3939 photo_upload = load_template("photo_upload")
4040 photo_list = load_template("photo_list")
4141 photo_tile = load_template("photo_tile")
42 photo_delete = load_template("photo_delete")
4243 view_photo = load_template("view_photo")
4344
4445 edit_pages = load_template("edit_pages")
220221 return flask.redirect("/edit/photo")
221222 id = int(flask.request.form["id"])
222223 file = flask.request.files["file"]
223 photo_id = db.add_photo(file, id)
224 photo_id = model.Photo.upload(file, id)
224225 if id == -1:
225 return flask.redirect(f"/edit/view-photo/{photo_id:09}")
226 return flask.redirect(f"/edit/view-photo/{photo_id}")
226227 else:
227 return flask.redirect(f"/edit/design/{id:09}")
228 return flask.redirect(f"/edit/design/{photo_id}")
228229
229230
230231 @app.route("/edit/photo/<id>/")
231232 @main
232233 def modify_photo(id):
233 design_id = flask.requests.args["design_id"]
234 return "Delete Photo", Templates.photo_delete(id, design_id)
234 design_id = flask.request.args.get('design_id', -1)
235 return "Delete Photo", Templates.photo_delete({
236 "id": id,
237 "design_id": design_id
238 })
235239
236240
237241 @app.route("/edit/photo/<id>/", methods=["POST"])
238242 def do_modify_photo(id):
239 design_id = int(flask.request.form["design_id"])
240 db.delete_photo(id)
241 return flask.redirect(f"/edit/design/{design_id}/")
243 model.Photo.get(filename=id).delete_instance()
244 if (d_id := int(flask.request.form.get('design_id', -1))) != -1:
245 return flask.redirect(f"/edit/design/{d_id}/")
246 else:
247 return flask.redirect(f"/edit/design/")
242248
243249
244250 @app.route("/edit/view-photo/")
11 from dataclasses import dataclass
2 import os
23 import markdown
34 import peewee
45 import typing
6 import PIL.Image
57
68 db = peewee.SqliteDatabase("new.db")
79 PER_PAGE = 16
112114 size = klass.select().count()
113115 photos = klass.select().paginate(page, PER_PAGE).order_by(klass.filename.desc())
114116 return photos, 1, (size // PER_PAGE) + 1
117
118 @classmethod
119 def upload(klass, upload, design_id):
120 last_image = klass.select().order_by(klass.filename.desc()).limit(1)
121 if last_image:
122 n = int(last_image[0].filename[:5]) + 1
123 else:
124 n = 0
125 ext = upload.filename[upload.filename.rindex(".")+1:]
126
127 full_name = f"{n:05}.{ext}"
128 thumb_name = f"{n:05}_thumb.{ext}"
129 img = PIL.Image.open(upload.stream)
130 img.thumbnail((400, 400), PIL.Image.ANTIALIAS)
131 img.save(os.path.join(os.getcwd(), "static", "photos", full_name))
132
133 img.thumbnail((100, 100), PIL.Image.ANTIALIAS)
134 img.save(os.path.join(os.getcwd(), "static", "photos", thumb_name))
135
136 klass.create(
137 filename=full_name,
138 design=design_id,
139 )
140
141 return full_name
115142
116143
117144 class Page(Model):