class Role(db.Model): __tablename__ = 'roles' id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.String(50), nullable=False, unique=True, index=True) show_name = db.Column(db.String(100)) default = db.Column(db.Boolean, default=False, index=True) permissions = db.Column(db.Integer) users = db.relationship('User', backref='role', lazy='dynamic') def __init__(self, *args, **kwargs): super(Role, self).__init__(*args, **kwargs) @staticmethod def initial_roles(): roles = { 'Free': (Permission.FREE, True, u'免费用户'), 'Pro': (Permission.PRO, False, u'Pro用户'), 'Ultra': (Permission.ULTRA, False, u'超级用户'), 'Admin': (Permission.ADMIN, False, u'管理员') } for r in roles: role = Role.query.filter_by(name=r).first() if role is None: role = Role(name=r) role.permissions = roles[r][0] role.default = roles[r][1] role.show_name = roles[r][2] db.session.add(role) db.session.commit() def __repr__(self): return '<Role %r>' % self.name
class Category(db.Model): __tablename__ = 'categories' id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.String(255), nullable=False) def __init__(self, *args, **kwargs): super(Category, self).__init__(*args, **kwargs)
class BookUploadRecord(db.Model): __tablename__ = 'book_upload_records' id = db.Column(db.Integer, autoincrement=True, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) book_edition_id = db.Column(db.Integer, db.ForeignKey('book_editions.id')) create_time = db.Column(db.DateTime, default=datetime.now) def __init__(self, *args, **kwargs): super(BookUploadRecord, self).__init__(*args, **kwargs)
class PushSetting(db.Model): __tablename__ = 'push_settings' id = db.Column(db.Integer, autoincrement=True, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) email = db.Column(db.String(255)) create_time = db.Column(db.DateTime, default=datetime.now) is_default = db.Column(db.Integer, default=0) # 1 is default def __init__(self, *args, **kwargs): super(PushSetting, self).__init__(*args, **kwargs)
class UserCheckinRecord(db.Model): __tablename__ = 'user_checkin_records' id = db.Column(db.Integer, autoincrement=True, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) score = db.Column(db.Integer, default=0) total_score = db.Column(db.Integer, default=0) create_time = db.Column(db.DateTime, default=datetime.now) # 下载时间 def __init__(self, *args, **kwargs): super(UserCheckinRecord, self).__init__(*args, **kwargs)
class BookEdition(db.Model): __tablename__ = 'book_editions' id = db.Column(db.Integer, autoincrement=True, primary_key=True) book_id = db.Column(db.Integer, db.ForeignKey('books.id')) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) # 上传者 filename = db.Column(db.String(255), nullable=False) original_filename = db.Column(db.String(255)) size = db.Column(db.Integer) # byte字节 push_count = db.Column(db.Integer, default=0) download_count = db.Column(db.Integer, default=0) checksum = db.Column(db.String(255)) create_time = db.Column(db.DateTime, default=datetime.now) book_edition_comments = db.relationship('BookEditionComment', backref='book_edition', lazy='dynamic') def __init__(self, *args, **kwargs): super(BookEdition, self).__init__(*args, **kwargs)
class BookPushRecord(db.Model): __tablename__ = 'book_push_records' id = db.Column(db.Integer, autoincrement=True, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) book_edition_id = db.Column(db.Integer, db.ForeignKey('book_editions.id')) from_platform = db.Column(db.String(255)) create_time = db.Column(db.DateTime, default=datetime.now) use_time = db.Column(db.Integer, default=0) delivery_status = db.Column(db.Integer, default=1) # 0-失败 1-成功 3-未知 destination_email = db.Column(db.String(255), nullable=False) def __init__(self, *args, **kwargs): super(BookPushRecord, self).__init__(*args, **kwargs)
class UserPoint(db.Model): __tablename__ = 'user_points' id = db.Column(db.Integer, autoincrement=True, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) point = db.Column(db.Integer, default=0) point_source = db.Column(db.String(30)) source_id = db.Column(db.Integer) create_time = db.Column(db.DateTime, default=datetime.now) def __init__(self, *args, **kwargs): super(UserPoint, self).__init__(*args, **kwargs)
class BookEditionComment(db.Model): __tablename__ = 'book_edition_comments' id = db.Column(db.Integer, autoincrement=True, primary_key=True) book_edition_id = db.Column(db.Integer, db.ForeignKey('book_editions.id')) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) # 评价人 at_user_id = db.Column(db.Integer, db.ForeignKey('users.id')) # 评价哪个用户 comment = db.Column(db.Text) create_time = db.Column(db.DateTime, default=datetime.now) def __init__(self, *args, **kwargs): super(BookEditionComment, self).__init__(*args, **kwargs)
class User(UserMixin, db.Model): __tablename__ = 'users' id = db.Column(db.Integer, autoincrement=True, primary_key=True) username = db.Column(db.String(50), nullable=False, unique=True, index=True) _password_hash = db.Column('password', db.String(255), nullable=False, server_default=u'') email = db.Column(db.String(255), nullable=False) avatar = db.Column(db.String(255)) last_login_time = db.Column(db.DateTime, default=datetime.now) last_login_ip = db.Column(db.String(50), default='') login_count = db.Column(db.Integer, nullable=False, default=0) is_delete = db.Column(db.Integer, default=0) # o-OK, 1-deleted role_id = db.Column(db.Integer, db.ForeignKey('roles.id')) create_time = db.Column(db.DateTime, default=datetime.now()) push_settings = db.relationship(PushSetting, backref='user', lazy='dynamic') point_count = db.Column(db.Integer, default=0) # 总积分 def __init__(self, *args, **kwargs): super(User, self).__init__(*args, **kwargs) def __repr__(self): return '<User %r>' % self.username @property def password(self): raise AttributeError('password is not a readable attribute.') @password.setter def password(self, password): self._password_hash = bcrypt.generate_password_hash(password) @property def is_admin(self): return self.can(Permission.ADMIN) @property def is_pro(self): return self.can(Permission.PRO) def verify_password(self, password): return bcrypt.check_password_hash(self._password_hash, password) def can(self, permissions): return self.role is not None and (self.role.permissions | permissions) == permissions @staticmethod def initial_admin(): u = User() u.username = '******' u.password = '******' u.email = 'admin@localhost' u.role = Role.query.filter_by(name='Admin').first() u1 = User() u1.username = '******' u1.password = '******' u1.email = '*****@*****.**' u1.role = Role.query.filter_by(name='Admin').first() db.session.add(u) db.session.add(u1) db.session.commit()
class Book(db.Model): __tablename__ = 'books' id = db.Column(db.Integer, autoincrement=True, primary_key=True) title = db.Column(db.String(255), nullable=False, index=True) # 书名 author = db.Column(db.String(255)) # 作者 logo = db.Column(db.String(255)) # 书的封面 translator = db.Column(db.String(255)) # 译者 publisher = db.Column(db.String(255)) # 出版社 isbn = db.Column(db.String(100)) # ISBN号 douban_id = db.Column(db.Integer, unique=True, index=True) # 豆瓣ID douban_url = db.Column(db.String(255)) douban_rating_score = db.Column(db.Integer, default=0) # 豆瓣得分 douban_rating_people = db.Column(db.Integer, default=0) # 豆瓣评论人数 book_catalog = db.Column(db.Text) # 图书目录 book_intro = db.Column(db.Text) # 内容简介 author_intro = db.Column(db.Text) # 作者简介 create_time = db.Column(db.DateTime, default=datetime.now) is_from = db.Column(db.String(255), default=u'douban') # douban or diy tags = db.relationship('Tag', secondary=books_tags, backref=db.backref('books', lazy='dynamic')) categories = db.relationship('Category', secondary=books_categories, backref=db.backref('books', lazy='dynamic')) book_edtions = db.relationship('BookEdition', backref='book', lazy='dynamic') def __init__(self, *args, **kwargs): super(Book, self).__init__(*args, **kwargs)
# -*- coding:utf-8 -*- from datetime import datetime from bookstore.extensions import db books_tags = db.Table( 'book_tag', db.Column('tag_id', db.Integer, db.ForeignKey('tags.id')), db.Column('book_id', db.Integer, db.ForeignKey('books.id'))) books_categories = db.Table( 'book_category', db.Column('category_id', db.Integer, db.ForeignKey('categories.id')), db.Column('book_id', db.Integer, db.ForeignKey('books.id'))) class Book(db.Model): __tablename__ = 'books' id = db.Column(db.Integer, autoincrement=True, primary_key=True) title = db.Column(db.String(255), nullable=False, index=True) # 书名 author = db.Column(db.String(255)) # 作者 logo = db.Column(db.String(255)) # 书的封面 translator = db.Column(db.String(255)) # 译者 publisher = db.Column(db.String(255)) # 出版社 isbn = db.Column(db.String(100)) # ISBN号 douban_id = db.Column(db.Integer, unique=True, index=True) # 豆瓣ID douban_url = db.Column(db.String(255)) douban_rating_score = db.Column(db.Integer, default=0) # 豆瓣得分 douban_rating_people = db.Column(db.Integer, default=0) # 豆瓣评论人数 book_catalog = db.Column(db.Text) # 图书目录 book_intro = db.Column(db.Text) # 内容简介 author_intro = db.Column(db.Text) # 作者简介