class Bookmark(db.Model): id = db.Column(db.Integer, primary_key=True) url = db.Column(db.Text, nullable=False) date = db.Column(db.DateTime, default=datetime.utcnow) description = db.Column(db.String(300)) @staticmethod def latest(num): return Bookmark.query.order_by(desc(Bookmark.date)).limit(num)
class Tag(db.Model): __tablename__ = 'tags' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(32), unique=True) def __init__(self, name): self.name = name def __repr__(self): return self.name
class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) is_admin = db.Column(db.Boolean, default=False) last_login = db.Column(db.DateTime) mail = db.Column(db.String(256)) password_salt = db.Column(db.String(32)) password_hash = db.Column(db.String(256)) def __init__(self, mail, password, is_admin=False): self.is_admin = is_admin self.mail = mail self.password_salt = ''.join(random.choice( string.letters + string.digits) for i in range(16)) self.password = password @hybrid_property def password(self): return self.password_hash @password.setter def password(self, password): self.password_hash = hash(password, self.password_salt) def set_last_login(self): self.last_login = datetime.datetime.utcnow() db.session.add(self) db.session.commit() def get_id(self): return self.id def is_anonymous(self): return False def is_authenticated(self): return True def is_active(self): return True
class Bookmark(db.Model): id = db.Column(db.Integer, primary_key=True) url = db.Column(db.Text, nullable=False) date = db.Column(db.DateTime, default=datetime.utcnow) description = db.Column(db.String(300)) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) # user = db.relationship('User') @staticmethod def newest(num): return Bookmark.query.order_by(desc(Bookmark.date)).limit(num) def __repr__(self): return "<Bookmark {}:{}.".format(self.description, self.url)
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True) email = db.Column(db.String(120), unique=True) bookmarks = db.relationship('Bookmark', backref='user', lazy='select') password_hash = db.Column(db.String(300)) def __repr__(self): return "<User {}>".format(self.username) @property def password(self): raise AttributeError('password: write only-field') @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) @staticmethod def get_by_username(username): return User.query.filter_by(username=username).first()
class Tag(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(25), nullable=False, unique=True, index=True) @staticmethod def get_or_create(name): try: return Tag.query.filter_by(name=name).one() except Exception as e: return Tag(name=name) @staticmethod def all(): return Tag.query.all() def __repr__(self): return self.name
class Actor(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(40))