class Work_(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(50)) abstract = db.Column(db.Text) time = db.Column(db.String(20)) company = db.Column(db.String(30))
class User(db.Model, UserMixin): __tablename__ = 'user' __table_args__ = ({'schema': 'public'}) id = db.Column('id', db.Integer, autoincrement=True, primary_key=True) __password = db.Column('password', db.String) username = db.Column(db.String(64), unique=True, nullable=False) sex = db.Column(db.Boolean) nickname = db.Column(db.String(10), unique=True) _icon_path = db.Column('icon', db.String(20), unique=True) signature = db.Column(db.String(25)) posts = db.relationship('Post', backref='user') articles = db.relationship('Article', backref='user') def __init__(self, username, password): self.username = username self.__password = generate_password_hash(password) def __repr__(self): return '<User %r>' % self.username def check_password(self, pw): return check_password_hash(self.__password, pw) @property def icon_path(self): if self._icon_path is None: return '/static/no-icon.jpg' else: return os.path.join('/static/uploads/icons', self._icon_path)
class Admin(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20)) password_hash = db.Column(db.String(128)) blog_title = db.Column(db.String(60)) blog_sub_title = db.Column(db.String(100)) name = db.Column(db.String(30)) about = db.Column(db.Text)
class Link(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(60)) url = db.Column(db.String(200), unique=True) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) category_id = db.Column(db.Integer, db.ForeignKey('link_category.id')) category = db.relationship('LinkCategory', back_populates='links')
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(200), nullable=False) author = db.Column(db.String(20), nullable=False) content = db.Column(db.Text, nullable=False) date = db.Column(db.DateTime) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) category = db.relationship("Category", back_populates="posts") comment = db.relationship('Comment', back_populates='post')
class Comment(db.Model): id = db.Column(db.Integer, primary_key=True) body = db.Column(db.Text) author = db.Column(db.String(30)) email = db.Column(db.String(254)) reviewed = db.Column(db.Boolean, default=False) from_admin = db.Column(db.Boolean, default=False) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) post_id = db.Column(db.Integer, db.ForeignKey('post.id')) post = db.relationship('Post', back_populates='comments') replied_id = db.Column(db.Integer, db.ForeignKey('comment.id')) replied = db.relationship('Comment', back_populates='replies', remote_side=[id]) replies = db.relationship('Comment', back_populates='replied', cascade='all')
class Admin(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(30)) name = db.Column(db.String(30)) about = db.Column(db.Text) password = db.Column(db.String(128)) site_title = db.Column(db.String(50)) def set_password(self, password): self.password = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password, password)
class User(db.Model, UserMixin): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True, nullable=False) email = db.Column(db.String(255), unique=True) password = db.Column(db.String(255), default='1') is_staff = db.Column(db.Boolean, nullable=False, default=False) is_active = db.Column(db.Boolean, default=True) author = relationship('Author', back_populates='user', uselist=False) def check_password(self, password): return check_password_hash(self.password, password)
class BlogInfo(db.Model): __tablename__ = 'blog_info' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(64)) signature = db.Column(db.Text) navbar = db.Column(db.String(64)) @staticmethod def insert_blog_info(): blog_mini_info = BlogInfo(title=u"Jazpenn's Blog", signature=u'一个程序猿,除了敲代码,还得找对象!', navbar='inverse') db.session.add(blog_mini_info) db.session.commit()
class Admin(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20)) password_hash = db.Column(db.String(128)) blog_title = db.Column(db.String(60)) blog_sub_title = db.Column(db.String(30)) name = db.Column(db.String(30)) about = db.Column(db.Text) def set_password(self, password): self.password_hash = generate_password_hash(password) def validate_password(self, password): return check_password_hash(self.password_hash, password)
class Article(db.Model): __tablename__ = 'articles' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(64), unique=True) thumbnail = db.Column(db.String(255), default='') content = db.Column(db.Text) summary = db.Column(db.Text) create_time = db.Column(db.DateTime, index=True, default=datetime.utcnow) update_time = db.Column(db.DateTime, index=True, default=datetime.utcnow) num_of_view = db.Column(db.Integer, default=0) articleType_id = db.Column(db.Integer, db.ForeignKey('articleTypes.id')) source_id = db.Column(db.Integer, db.ForeignKey('sources.id')) comments = db.relationship('Comment', backref='article', lazy='dynamic') @staticmethod def generate_fake(count=100): from sqlalchemy.exc import IntegrityError from random import seed, randint import forgery_py seed() articleType_count = ArticleType.query.count() source_count = Source.query.count() for i in range(count): aT = ArticleType.query.offset(randint(0, articleType_count - 1)).first() s = Source.query.offset(randint(0, source_count - 1)).first() a = Article(title=forgery_py.lorem_ipsum.title(randint(3, 5)), content=forgery_py.lorem_ipsum.sentences( randint(15, 35)), summary=forgery_py.lorem_ipsum.sentences(randint(2, 5)), num_of_view=randint(100, 15000), articleType=aT, source=s) db.session.add(a) try: db.session.commit() except IntegrityError: db.session.rollback() @staticmethod def add_view(article, db): article.num_of_view += 1 db.session.add(article) db.session.commit() def __repr__(self): return '<Article %r>' % self.title
class Article(db.Model): __tablename__ = 'articles' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(255)) text = db.Column(db.String()) author_id = db.Column(ForeignKey('authors.id'), nullable=False) created_at = db.Column(db.DateTime, default=datetime.utcnow) updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) author = relationship('Author', back_populates='articles') tags = relationship('Tag', secondary=article_tag_associations_table, back_populates='articles')
class Menu(db.Model): __tablename__ = 'menus' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) types = db.relationship('ArticleType', backref='menu', lazy='dynamic') order = db.Column(db.Integer, default=0, nullable=False) def sort_delete(self): for menu in Menu.query.order_by(Menu.order).offset(self.order).all(): menu.order -= 1 db.session.add(menu) @staticmethod def insert_menus(): menus = [u'Web开发', u'数据库', u'网络技术', u'爱生活,爱自己', u'Linux世界', u'开发语言'] for name in menus: menu = Menu(name=name) db.session.add(menu) db.session.commit() menu.order = menu.id db.session.add(menu) db.session.commit() @staticmethod def return_menus(): menus = [(m.id, m.name) for m in Menu.query.all()] menus.append((-1, u'不选择导航(该分类将单独成一导航)')) return menus def __repr__(self): return '<Menu %r>' % self.name
class Plugin(db.Model): __tablename__ = 'plugins' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(64), unique=True) note = db.Column(db.Text, default='') content = db.Column(db.Text, default='') order = db.Column(db.Integer, default=0) disabled = db.Column(db.Boolean, default=False) @staticmethod def insert_system_plugin(): plugin = Plugin(title=u'博客统计', note=u'系统插件', content='system_plugin', order=1) db.session.add(plugin) db.session.commit() def sort_delete(self): for plugin in Plugin.query.order_by(Plugin.order.asc()).offset( self.order).all(): plugin.order -= 1 db.session.add(plugin) def __repr__(self): return '<Plugin %r>' % self.title
class Text(db.Model): # 作为基类,将不创建表 __abstract__ = True id = db.Column(db.Integer, autoincrement=True, primary_key=True) body = db.Column(db.String(), nullable=True) _datetime = db.Column(db.DateTime, default=datetime.now, nullable=False) @declared_attr def user_id(self): return db.Column('author', db.ForeignKey('user.id'), nullable=False) @property def author(self): author = User.query.get(self.user_id) return author @property def datetime(self): return self._datetime.strftime('%Y-%m-%d %H:%M') @property def username(self): return self.author.username @property def icon_path(self): return self.author.icon_path
class Admin(db.Model, UserMixin): #UserMixin表示通过认证的用户 id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20)) password_hash = db.Column(db.String(128)) blog_title = db.Column(db.String(60)) blog_sub_title = db.Column(db.String(100)) name = db.Column(db.String(30)) about = db.Column(db.Text) #为给定密码生成三劣质 def set_password(self, password): self.password_hash = generate_password_hash(password) #接受散列值和密码查看是否匹配 def validate_password(self, password): return check_password_hash(self.password_hash, password)
class Article(Text): title = db.Column(db.String(50), nullable=False) like = db.Column(db.Integer, default=0, nullable=False) reviews = db.relationship('ArticleReview', backref='article') def __repr__(self): return '<Article %r>' % self.id
class Role(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), unique=True) users = db.relationship('User', back_populates='role') permissions = db.relationship('Permission', secondary=roles_permissions, back_populates='roles') @staticmethod def init_role(): roles_permissions_map = { 'Locked': ['COLLECT'], 'User': ['POST', 'COLLECT', 'COMMENT'], 'Moderator': ['POST', 'COLLECT', 'COMMENT', 'MODERATE'], 'Administrator': ['POST', 'COLLECT', 'COMMENT', 'MODERATE', 'ADMINISTER'] } for role_name in roles_permissions_map: role = Role.query.filter_by(name=role_name).first() if role is None: role = Role(name=role_name) db.session.add(role) role.permissions = [] for permission_name in roles_permissions_map[role_name]: permission = Permission.query.filter_by( name=permission_name).first() if permission is None: permission = Permission(name=permission_name) db.session.add(permission) role.permissions.append(permission) db.session.commit()
class Tag(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), index=True, unique=True) articles = db.relationship('Article', secondary=tagging, back_populates='tags')
class ArticleTypeSetting(db.Model): __tablename__ = 'articleTypeSettings' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) protected = db.Column(db.Boolean, default=False) hide = db.Column(db.Boolean, default=False) types = db.relationship('ArticleType', backref='setting', lazy='dynamic') @staticmethod def insert_system_setting(): system = ArticleTypeSetting(name='system', protected=True, hide=True) db.session.add(system) db.session.commit() @staticmethod def insert_default_settings(): system_setting = ArticleTypeSetting(name='system', protected=True, hide=True) common_setting = ArticleTypeSetting(name='common', protected=False, hide=False) db.session.add(system_setting) db.session.add(common_setting) db.session.commit() @staticmethod def return_setting_hide(): return [(2, u'公开'), (1, u'隐藏')] def __repr__(self): return '<ArticleTypeSetting %r>' % self.name
class User(UserMixin, db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) telephone = db.Column(db.String(11), nullable=False) username = db.Column(db.String(20), nullable=False) password = db.Column(db.String(100), nullable=False) def __init__(self, *args, **kwargs): telephone = kwargs.get('telephone') username = kwargs.get('username') password = kwargs.get('password') self.telephone = telephone self.username = username self.password = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password, password)
class Blog(db.Model): __tablename__ = 'blog' id = db.Column(db.Integer, primary_key=True) blog_title = db.Column(db.String(128), nullable=False) blog_content = db.Column(db.Text) create_time = db.Column(db.DateTime, default=datetime.now) blogger_id = db.Column(db.Integer, db.ForeignKey('user.id')) blogger = db.relationship('User', backref=db.backref('blogs'))
class Question(db.Model): __tablename__ = 'question' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) content = db.Column(db.Text, nullable=False) ques_time = db.Column(db.DateTime, default=datetime.now) blogger_id = db.Column(db.Integer, db.ForeignKey('user.id')) blogger = db.relationship('User', backref=db.backref('questions'))
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(60)) body = db.Column(db.TEXT) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) category = db.relationship('Category', back_populates='posts')
class Book(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) comment = db.Column(db.TEXT) category_id = db.Column(db.Integer, db.ForeignKey('book_category.id')) category = db.relationship('BookCategory', back_populates='books')
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(60)) body = db.Column(db.Text) timestamp = db.Column(db.DateTime, default=datetime.utcnow) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) category = db.relationship('Category', back_populates='posts') comments = db.relationship('Comment', back_populates='post', cascade='all') #设置级联删除
class Admin(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20)) password_hash = db.Column(db.String(128)) blog_title = db.Column(db.String(60)) blog_sub_title = db.Column(db.String(100)) name = db.Column(db.String(30)) about = db.Column(db.Text) @property def password(self): raise AttributeError('read not allowed') @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def validate_password(self, password): return check_password_hash(self.password_hash, password)
class Comment(db.Model): id = db.Column(db.Integer, primary_key=True) person_post = db.Column(db.String(30)) body = db.Column(db.Text) from_admin = db.Column(db.Boolean, default=False) reviewed = db.Column(db.Boolean, default=False) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) email = db.Column(db.String(255)) site = db.Column(db.String(255)) reply_id = db.Column(db.Integer, db.ForeignKey('comment.id')) article_id = db.Column(db.Integer, db.ForeignKey('article.id')) # to support comments to reply comments article = db.relationship('Article', back_populates='comments') replies = db.relationship('Comment', back_populates='replied', cascade='all, delete-orphan') replied = db.relationship('Comment', back_populates='replies', remote_side=[id])
class Category(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), unique=True) articles = db.relationship('Article', back_populates='category') def delete(self): default = Category.query.get(1) articles = self.articles[:] for a in articles: a.category = default db.session.delete(self) db.session.commit()
class Article(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(50)) body = db.Column(db.Text) category = db.relationship('Category', back_populates='articles') timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) comment_open = db.Column(db.Boolean, default=True) count_read = db.Column(db.Integer) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) comments = db.relationship('Comment', back_populates='article', cascade='all, delete-orphan')