gdritter repos frony-ritter-designs / db6e924
Fix image uploading Getty Ritter 3 years ago
2 changed file(s) with 26 addition(s) and 14 deletion(s). Collapse all Expand all
163163 page = int(flask.request.args.get("page") or 0)
164164 min, max = db.max_page_ranges()
165165 data = db.get_all(offset=page)
166 print(data)
167166 return 'Edit Mode', Templates.edit_design_list(data)
168167
169168 @app.route("/edit/design", methods=["POST"])
212211
213212 @app.route("/edit/photo/", methods=["POST"])
214213 def do_add_photo():
215 pass
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']
218 photo_id = db.add_photo(file, id)
219 if id == -1:
220 return flask.redirect(f'/edit/view-photo/{photo_id:09}')
221 else:
222 return flask.redirect(f'/edit/design/{id:09}')
216223
217224 # def POST(self):
218225 # input = web.input(file={})
261268 @app.route("/edit/view-photo/<id>/")
262269 @main
263270 def view_photo(id):
264 photo = db.get_photo_by_filename(id)
271 photo = db.get_photo_by_id(id)
265272 if photo:
266273 return f"Viewing photo {id}", Templates.view_photo(photo)
267274 else:
33 import mistune
44 import os
55 import web
6 from PIL import Image
6 import PIL.Image
77
88 import typing
99
2323
2424
2525 class Image(typing.NamedTuple):
26 id: int
2627 image: str
2728
2829 def thumb(self):
8586 return [
8687 cls(
8788 title=d.title,
88 images=[Image(d.image)],
89 images=[Image(image=d.image, id=0)],
8990 description=d.description,
9091 category=d.cat_name,
9192 id=d.id,
219220
220221 def get_photo_by_id(self, id):
221222 try:
222 return self._db.where("photos", id=id)[0].filename
223 photo = self._db.where("photos", id=id)[0]
224 return Image(image=photo.filename, id=photo.id)
223225 except:
224226 return None
225227
226228 def get_photo_by_filename(self, filename):
227229 try:
228 return Image(self._db.where("photos", filename=filename)[0].filename)
230 photo = self._db.where("photos", filename=filename)[0]
231 return Image(image=photo.filename, id=photo.id)
229232 except:
230233 return None
231234
232235 def get_picture(self, id):
233236 try:
234 return Image((self._db.where("photos", design_id=id, limit=1))[0].filename)
237 photo = self._db.where("photos", design_id=id, limit=1)[0]
238 return Image(image=photo.filename, id=photo.id)
235239 except:
236240 return None
237241
239243 if not pp:
240244 pp = self.per_page
241245 return [
242 Image(l.filename)
246 Image(image=l.filename, id=l.id)
243247 for l in self._db.select(
244248 "photos", offset=offset * pp, limit=pp, order="id DESC"
245249 )
246250 ]
247251
248252 def get_all_photos_for_design(self, id):
249 return list(Image(d.filename) for d in self._db.where("photos", design_id=id))
253 return list(Image(image=d.filename, id=d.id) for d in self._db.where("photos", design_id=id))
250254
251255 def get_all(self, offset=0):
252256 ds = self._db.query(
322326 extension = file.filename[-3:]
323327 name = "{0:05}.{1}".format(new_num, extension)
324328 thumb = "{0:05}_thumb.{1}".format(new_num, extension)
325 img = Image.open(file.file)
326 img.thumbnail((400, 400), Image.ANTIALIAS)
327 img.save("/" + os.path.join("srv", "http", "frd", "static", "photos", name))
328 img.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
329
330 img = PIL.Image.open(file.stream)
331 img.thumbnail((400, 400), PIL.Image.ANTIALIAS)
332 img.save(os.path.join(os.getcwd(), "static", "photos", name))
333 img.thumbnail(THUMB_SIZE, PIL.Image.ANTIALIAS)
329334 img.save(os.path.join("static", "photos", thumb))
330335 self._db.insert("photos", id=new_id, filename=name, design_id=d_id)
331336 return new_id