class Comment(db.Model): __tablename__ = 'comments' id = db.Column(db.Integer, primary_key=True) content = db.Column(db.Text) created_at = db.Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) updated_at = db.Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) user = relationship('User', backref='comments') article_id = db.Column(db.Integer, db.ForeignKey('articles.id'), nullable=False) article = relationship('Article', backref=db.backref('comments')) replied_comment_id = db.Column(db.Integer, db.ForeignKey('comments.id'), nullable=True) replies = relationship("Comment", backref=db.backref('reply', remote_side=[id]))
class UserSubscription(db.Model): __tablename__ = 'user_subscriptions' following_id = db.Column(db.Integer, db.ForeignKey("users.id")) follower_id = db.Column(db.Integer, db.ForeignKey("users.id")) following = db.relationship("User", foreign_keys=[following_id], backref='follower_subscriptions') follower = db.relationship("User", foreign_keys=[follower_id], backref='following_subscriptions') __mapper_args__ = {'primary_key': [following_id, follower_id]} __table_args__ = (UniqueConstraint('following_id', 'follower_id', name='one_subscription_for_same_user'),)
class Like(db.Model): __tablename__ = 'likes' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) user = relationship('User', backref=db.backref('likes')) article_id = db.Column(db.Integer, db.ForeignKey('articles.id'), nullable=False) article = relationship('Article', backref=db.backref('likes')) __mapper_args__ = {'primary_key': [user_id, article_id]} __table_args__ = (UniqueConstraint('user_id', 'article_id', name='one_like_per_article_and_user'), )
class UserRole(db.Model): __tablename__ = 'users_roles' user_id = db.Column(db.Integer, db.ForeignKey("users.id")) role_id = db.Column(db.Integer, db.ForeignKey("roles.id")) # users = db.relationship("User", foreign_keys=[user_id], backref='roles') user = db.relationship("User", foreign_keys=[user_id], backref='users_roles') role = db.relationship("Role", foreign_keys=[role_id], backref='users_roles') __mapper_args__ = {'primary_key': [user_id, role_id]} __table_args__ = (UniqueConstraint('user_id', 'role_id', name='same_role_for_same_user'), )
class Article(db.Model): __tablename__ = 'articles' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) description = db.Column(db.Text, nullable=False) slug = db.Column(db.Text, unique=True) body = db.Column(db.Text) created_at = db.Column(db.DateTime(), default=datetime.utcnow, index=True) updated_at = db.Column(db.DateTime(), onupdate=datetime.utcnow) publish_on = db.Column(db.DateTime, index=True, default=datetime.utcnow, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) user = relationship('User', backref=db.backref('articles')) ''' liked_by = relationship( 'user', secondary=likesModel, backref='likes', lazy='dynamic') ''' tags = relationship('Tag', secondary=articles_tags, backref='articles') categories = relationship('Category', secondary=articles_categories, backref='articles') # comments = relationship('Comment', backref='article', lazy='dynamic') def add_tag(self, tag): if tag not in self.tags: self.tags.append(tag) return True return False def remove_tag(self, tag): if tag in self.tags: self.tags.remove(tag) return True return False def __repr__(self): return '<Article %r>' % self.body def __str__(self): return '<Article {}>'.format(self.body)
from sqlalchemy import UniqueConstraint from sqlalchemy.orm import relationship from blog_api.factory import db class Like(db.Model): __tablename__ = 'likes' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) user = relationship('User', backref=db.backref('likes')) article_id = db.Column(db.Integer, db.ForeignKey('articles.id'), nullable=False) article = relationship('Article', backref=db.backref('likes')) __mapper_args__ = {'primary_key': [user_id, article_id]} __table_args__ = (UniqueConstraint('user_id', 'article_id', name='one_like_per_article_and_user'), ) likesTable = db.Table('likes', db.Column('user_id', db.Integer, db.ForeignKey('user.id')), db.Column('article_id', db.Integer, db.ForeignKey('articles.id')), keep_existing=True)
from datetime import datetime from slugify import slugify from sqlalchemy import event from blog_api.factory import db class Tag(db.Model): __tablename__ = 'tags' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) slug = db.Column(db.String(), index=True) description = db.Column(db.String()) created_at = db.Column(db.DateTime(), default=datetime.utcnow, index=True) updated_at = db.Column(db.DateTime()) def __repr__(self): return self.name articles_tags = db.Table("articles_tags", db.Column("tag_id", db.Integer, db.ForeignKey("tags.id")), db.Column("article_id", db.Integer, db.ForeignKey("articles.id"))) @event.listens_for(Tag.name, 'set') def receive_set(target, value, oldvalue, initiator): target.slug = slugify(unicode(value))
class SiteSubscription(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer(), db.ForeignKey('users.id'), nullable=False)
from sqlalchemy import UniqueConstraint from blog_api.factory import db class UserSubscription(db.Model): __tablename__ = 'user_subscriptions' following_id = db.Column(db.Integer, db.ForeignKey("users.id")) follower_id = db.Column(db.Integer, db.ForeignKey("users.id")) following = db.relationship("User", foreign_keys=[following_id], backref='follower_subscriptions') follower = db.relationship("User", foreign_keys=[follower_id], backref='following_subscriptions') __mapper_args__ = {'primary_key': [following_id, follower_id]} __table_args__ = (UniqueConstraint('following_id', 'follower_id', name='one_subscription_for_same_user'),) user_subscriptions = db.Table( 'user_subscriptions', db.Column('follower_id', db.Integer, db.ForeignKey('users.id')), db.Column('following_id', db.Integer, db.ForeignKey('users.id')), keep_existing=True ) class SiteSubscription(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer(), db.ForeignKey('users.id'), nullable=False)
name = db.Column(db.String(80), unique=True, nullable=False) description = db.Column(db.String(100), nullable=True) class UserRole(db.Model): __tablename__ = 'users_roles' user_id = db.Column(db.Integer, db.ForeignKey("users.id")) role_id = db.Column(db.Integer, db.ForeignKey("roles.id")) # users = db.relationship("User", foreign_keys=[user_id], backref='roles') user = db.relationship("User", foreign_keys=[user_id], backref='users_roles') role = db.relationship("Role", foreign_keys=[role_id], backref='users_roles') __mapper_args__ = {'primary_key': [user_id, role_id]} __table_args__ = (UniqueConstraint('user_id', 'role_id', name='same_role_for_same_user'), ) users_roles = db.Table('users_roles', db.Column('user_id', db.Integer, db.ForeignKey('users.id')), db.Column('role_id', db.Integer, db.ForeignKey('roles.id')), keep_existing=True)
from slugify import slugify from sqlalchemy import event from blog_api.factory import db class Category(db.Model): __tablename__ = 'categories' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) slug = db.Column(db.String(), index=True) description = db.Column(db.String()) created_at = db.Column(db.DateTime(), default=datetime.utcnow, index=True) updated_at = db.Column(db.DateTime(), onupdate=datetime.utcnow) def __repr__(self): return self.name articles_categories = db.Table( "articles_categories", db.Column("category_id", db.Integer, db.ForeignKey("categories.id")), db.Column("article_id", db.Integer, db.ForeignKey("articles.id"))) @event.listens_for(Category.name, 'set') def receive_set(target, value, oldvalue, initiator): target.slug = slugify(unicode(value))