from app import db from config import ADMINS import enum class Format(enum.Enum): jpg = 1 raw = 2 tiff = 3 class Role(enum.Enum): read = 1 write = 2 admin = 3 class User(db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(255)) email = db.Column(db.String(255), index=True, unique=True) username = db.Column(db.String(255), index=True, unique=True) password = db.Column(db.String(255)) portfolios = db.relationship('Portfolio', backref='owner', lazy=True, cascade='save-update, merge, delete, delete-orphan') acl = db.relationship('AccessRight', backref='user', lazy=True) @property def is_authenticated(self): return True @property def is_active(self): return True @property def is_anonymous(self): return False @property def is_admin(self): return self.email in ADMINS def get_id(self): return str(self.id) # python 3 def __repr__(self): return '' % (self.name, self.email, self.username) class Portfolio(db.Model): __tablename__ = 'portfolio' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(255)) user_id = db.Column(db.ForeignKey('user.id', onupdate='CASCADE', ondelete='CASCADE'), nullable=False) albums = db.relationship('Album', backref='portfolio', lazy=True, cascade='save-update, merge, delete, delete-orphan') def __repr__(self): return '' % (self.name, self.owner) class Album(db.Model): __tablename__ = 'album' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(255)) photos = db.relationship('Photo', backref='album', lazy=True, cascade='save-update, merge, delete, delete-orphan') portfolio_id = db.Column(db.ForeignKey('portfolio.id', onupdate='CASCADE', ondelete='CASCADE'), nullable=False) def __repr__(self): return '' % (self.name, self.portfolio) class Photo(db.Model): __tablename__ = 'photo' id = db.Column(db.Integer, primary_key=True, autoincrement=True) file = db.Column(db.String(255)) name = db.Column(db.String(255)) exif = db.Column(db.Text) format = db.Column(db.Enum(Format)) sibling = db.Column(db.ForeignKey('photo.id')) album_id = db.Column(db.ForeignKey('album.id', onupdate='CASCADE', ondelete='CASCADE'), nullable=False) def __repr__(self): return '' % (self.name, self.file, self.format, self.album) class AccessRight(db.Model): __tablename__ = 'accessright' id = db.Column(db.Integer, primary_key = True, autoincrement=True) right = db.Column(db.Enum(Role)) user_id = db.Column(db.ForeignKey('user.id', onupdate='CASCADE', ondelete='CASCADE'), nullable=False) object = db.Column(db.Integer) def __repr__(self): return '' % (self.right, self.user, self.object)