class Event(UUIDKeyMixin, db.Model): __tablename__ = 'event' name = db.Column(db.String(127), nullable=False) details = db.Column(db.String(255), nullable=False) start = db.Column(db.DateTime, nullable=False) end = db.Column(db.DateTime, nullable=False) owner_id = db.Column(UUIDType, db.ForeignKey('user.id'), default=lambda: get_current_user().id, nullable=False) owner = db.relationship('User')
class Category(db.Model): __tablename__ = 'category' id = db.Column(db.String(20), unique=True, nullable=False, primary_key=True) order = db.Column(db.Integer, nullable=False) title = db.Column(db.String(127), nullable=False) pages = db.relationship("Page")
class Gallery(UUIDKeyMixin, db.Model): __tablename__ = 'gallery' title = db.Column(db.String(63), nullable=False) owner_id = db.Column(UUIDType, db.ForeignKey('user.id'), default=lambda: get_current_user().id, nullable=False) owner = db.relationship('User') media = db.relationship('Media', secondary=gallery_media_relation, lazy=True)
class Media(UUIDKeyMixin, db.Model): __tablename__ = 'media' name = db.Column(db.String(127), nullable=False) mimetype = db.Column(db.String(127), nullable=False) extension = db.Column(db.String(15), nullable=False) owner_id = db.Column(UUIDType, db.ForeignKey('user.id'), default=lambda: get_current_user().id, nullable=False) owner = db.relationship('User') def get_file_name(self, suffix=''): return f'{str(self.id)}{suffix}.{self.extension}'
class Change(db.Model): __tablename__ = 'change' __table_args__ = (db.ForeignKeyConstraint(('category', 'page'), ('page.category', 'page.id')), ) category = db.Column(db.String(20), nullable=False, primary_key=True) page = db.Column(db.String(20), nullable=False, primary_key=True) data = db.Column(db.JSON, nullable=False) created_at = db.Column(db.DateTime, default=db.func.current_timestamp(), primary_key=True) author = db.Column(UUIDType, db.ForeignKey('user.id'), default=lambda: get_current_user().id, nullable=False)
class UUIDKeyMixin: @property def id(self) -> uuid.UUID: return self.__id __id = db.Column(UUIDType(), name='id', primary_key=True, default=uuid.uuid4, unique=True, nullable=False)
class User(UUIDKeyMixin, db.Model): __tablename__ = 'user' first_name = db.Column(db.String(63), nullable=False) last_name = db.Column(db.String(63), nullable=False) email = db.Column(db.String(127), nullable=False, unique=True) password = db.Column(PasswordType, nullable=False) role = db.Column(db.Enum(Role), nullable=False, default='author') @staticmethod def authenticate(username=None, password=None): if not username or not password: raise AuthenticationError('No credentials provided') user: User = User.query.filter_by(email=username).first() if not user: raise AuthenticationError('User not found') if user.password != password: raise AuthenticationError('Password incorrect') return user
from flask_jwt_extended import get_current_user from server.common.database.mixins import UUIDKeyMixin, UUIDType from server.common.database.ref import db gallery_media_relation = db.Table( 'gallery_media', db.Column('media_id', UUIDType, db.ForeignKey('media.id'), primary_key=True), db.Column('gallery_id', UUIDType, db.ForeignKey('gallery.id'), primary_key=True)) class Gallery(UUIDKeyMixin, db.Model): __tablename__ = 'gallery' title = db.Column(db.String(63), nullable=False) owner_id = db.Column(UUIDType, db.ForeignKey('user.id'), default=lambda: get_current_user().id, nullable=False) owner = db.relationship('User') media = db.relationship('Media', secondary=gallery_media_relation, lazy=True)
class TrackUpdateMixin: updated_at = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp())
class TrackCreationMixin: created_at = db.Column(db.DateTime, default=db.func.current_timestamp())