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'), nullable=False) _tags = db.relationship('Tag', secondary=tags, lazy='joined', backref=db.backref('bookmarks', lazy='dynamic')) @staticmethod def newest(num): return Bookmark.query.order_by(desc(Bookmark.date)).limit(num) @property def tags(self): return ",".join([t.name for t in self._tags]) @tags.setter def tags(self, string): if string: self._tags = [ Tag.get_or_create(name) for name in string.split(',') ] def __repr__(self): return "Bookmark '{}: '{}".format(self.description, self.url)
class Movie(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(225)) release_date = db.Column(db.DateTime, default=datetime.utcnow) actors = db.relationship('Actor', secondary=actors_movies, backref='movie', lazy='select')
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 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 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 Bookmark(db.Model): __tablename__ = 'bookmarks' id = db.Column(db.Integer, primary_key=True) url = db.Column(db.String(2048), unique=True) title = db.Column(db.String(1024)) description = db.Column(db.Text) public = db.Column(db.Boolean, default=False) timestamp = db.Column(db.DateTime) t = db.relationship('Tag', secondary=bookmarks_tags_rel, backref=db.backref('bookmarks', lazy='select')) def __init__(self, url, title, description='', tags=[], public=False): self.url = url self.title = title self.description = description self.tags = tags self.public = public self.timestamp = datetime.datetime.utcnow() @hybrid_property def tags(self): return self.t @tags.setter def tags(self, tagz=[]): tags = [] for tag in tagz: tags.append(Tag.query.filter_by(name=tag).first()) self.t = tags
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 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 Actor(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(40))
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() actors_movies = db.Table( 'actor_movies', db.Column('actor_id', db.Integer, db.ForeignKey('actor.id')), db.Column('movie_id', db.Integer, db.ForeignKey('movie.id'))) class Movie(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(225)) release_date = db.Column(db.DateTime, default=datetime.utcnow) actors = db.relationship('Actor', secondary=actors_movies, backref='movie', lazy='select') class Actor(db.Model): id = db.Column(db.Integer, primary_key=True)
# -*- coding: utf-8 -*- import datetime import random import string from bookmarks import db from sqlalchemy.ext.hybrid import hybrid_property from .util import hash bookmarks_tags_rel = db.Table('bookmarks_tags_rel', db.Column('tag_id', db.Integer, db.ForeignKey('tags.id')), db.Column('bookmark_id', db.Integer, db.ForeignKey('bookmarks.id')) ) class Bookmark(db.Model): __tablename__ = 'bookmarks' id = db.Column(db.Integer, primary_key=True) url = db.Column(db.String(2048), unique=True) title = db.Column(db.String(1024)) description = db.Column(db.Text) public = db.Column(db.Boolean, default=False) timestamp = db.Column(db.DateTime) t = db.relationship('Tag', secondary=bookmarks_tags_rel, backref=db.backref('bookmarks', lazy='select')) def __init__(self, url, title, description='', tags=[], public=False): self.url = url self.title = title
from bookmarks import db from datetime import datetime from sqlalchemy import desc from flask_login import UserMixin from werkzeug.security import check_password_hash, generate_password_hash tags = db.Table( 'bookmark_tag', db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')), db.Column('bookmark_id', db.Integer, db.ForeignKey('bookmark.id'))) 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'), nullable=False) _tags = db.relationship('Tag', secondary=tags, lazy='joined', backref=db.backref('bookmarks', lazy='dynamic')) @staticmethod def newest(num): return Bookmark.query.order_by(desc(Bookmark.date)).limit(num) @property def tags(self): return ",".join([t.name for t in self._tags]) @tags.setter