| 3 | 3 |
import mistune
|
| 4 | 4 |
import os
|
| 5 | 5 |
import web
|
| 6 | |
from PIL import Image
|
| 6 |
import PIL.Image
|
| 7 | 7 |
|
| 8 | 8 |
import typing
|
| 9 | 9 |
|
|
| 23 | 23 |
|
| 24 | 24 |
|
| 25 | 25 |
class Image(typing.NamedTuple):
|
| 26 |
id: int
|
| 26 | 27 |
image: str
|
| 27 | 28 |
|
| 28 | 29 |
def thumb(self):
|
|
| 85 | 86 |
return [
|
| 86 | 87 |
cls(
|
| 87 | 88 |
title=d.title,
|
| 88 | |
images=[Image(d.image)],
|
| 89 |
images=[Image(image=d.image, id=0)],
|
| 89 | 90 |
description=d.description,
|
| 90 | 91 |
category=d.cat_name,
|
| 91 | 92 |
id=d.id,
|
|
| 219 | 220 |
|
| 220 | 221 |
def get_photo_by_id(self, id):
|
| 221 | 222 |
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)
|
| 223 | 225 |
except:
|
| 224 | 226 |
return None
|
| 225 | 227 |
|
| 226 | 228 |
def get_photo_by_filename(self, filename):
|
| 227 | 229 |
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)
|
| 229 | 232 |
except:
|
| 230 | 233 |
return None
|
| 231 | 234 |
|
| 232 | 235 |
def get_picture(self, id):
|
| 233 | 236 |
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)
|
| 235 | 239 |
except:
|
| 236 | 240 |
return None
|
| 237 | 241 |
|
|
| 239 | 243 |
if not pp:
|
| 240 | 244 |
pp = self.per_page
|
| 241 | 245 |
return [
|
| 242 | |
Image(l.filename)
|
| 246 |
Image(image=l.filename, id=l.id)
|
| 243 | 247 |
for l in self._db.select(
|
| 244 | 248 |
"photos", offset=offset * pp, limit=pp, order="id DESC"
|
| 245 | 249 |
)
|
| 246 | 250 |
]
|
| 247 | 251 |
|
| 248 | 252 |
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))
|
| 250 | 254 |
|
| 251 | 255 |
def get_all(self, offset=0):
|
| 252 | 256 |
ds = self._db.query(
|
|
| 322 | 326 |
extension = file.filename[-3:]
|
| 323 | 327 |
name = "{0:05}.{1}".format(new_num, extension)
|
| 324 | 328 |
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)
|
| 329 | 334 |
img.save(os.path.join("static", "photos", thumb))
|
| 330 | 335 |
self._db.insert("photos", id=new_id, filename=name, design_id=d_id)
|
| 331 | 336 |
return new_id
|