from datetime import datetime from uuid import uuid4 from pathlib import Path from os.path import splitext from peewee import * from PIL import Image from flask_login import login_user from flipbook import config db = SqliteDatabase(f"data/flipbook.sqlite") def rand_id(): return uuid4().hex class Wallet(Model): id = AutoField() address = CharField(null=False, unique=True) register_date = DateTimeField(default=datetime.utcnow) login_date = DateTimeField(null=True) opensea_handle = CharField(null=True) twitter_handle = CharField(null=True) nonce = CharField(default=rand_id()) nonce_date = DateTimeField(default=datetime.utcnow) @property def is_authenticated(self): return True @property def is_active(self): return True @property def is_anonymous(self): return False def generate_nonce(self): return rand_id() def change_nonce(self): self.nonce = rand_id() self.nonce_date = datetime.utcnow() self.save() def login(self): self.change_nonce() self.last_login_date = datetime.utcnow() login_user(self) self.save() class Meta: database = db class Upload(Model): id = AutoField() token_id = IntegerField() title = CharField() text = CharField(null=True) wallet = ForeignKeyField(Wallet) image_name = CharField(null=False) upload_date = DateTimeField(default=datetime.utcnow) def get_thumbnail_name(self): s = splitext(self.image_name) return s[0] + '.thumbnail' + s[1] def save_thumbnail(self): try: image = Image.open(Path(config.UPLOADS_PATH, self.image_name)) image.thumbnail((200,200), Image.ANTIALIAS) image.save(Path(config.UPLOADS_PATH, self.get_thumbnail_name()), format=image.format, quality=90) image.close() return True except Exception as e: print(e) return False class Meta: database = db