class Token(db.Model): """ токены пользователя, которые выдаются на клиентское приложение, с которого пользователь запрашивает ресурс """ id = db.Column(db.Integer, primary_key=True) user_client_id = db.Column(db.Integer, db.ForeignKey('user_client.id'), nullable=False) access_token = db.Column(db.Text(), nullable=False) refresh_token = db.Column(db.Text(), nullable=False) used = db.Column(db.Boolean, default=False, nullable=False) def __init__(self, user_client_id): self.user_client_id = user_client_id self.access_token = create_access_token(identity=self.id, expires_delta=timedelta(1)) self.refresh_token = create_refresh_token(identity=self.id)
class Article(db.Model): __tablename__ = 'article' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), index = True) content = db.Column(db.Text(length=(2**31)-1)) description = db.Column(db.Text(length=(2**31)-1)) tag_id = db.Column(db.Integer, db.ForeignKey('article_tag.id'), index = True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), index = True) create_time = db.Column(db.DateTime, default=datetime.utcnow, index = True) update_time = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, index = True) avatar_image = db.Column(db.String(64), index = True) like_num = db.Column(db.Integer, default = 0) watch_num = db.Column(db.Integer, default = 0) comment = db.relationship('Comment', cascade = 'delete')
class Teacher(db.Model): __tablename__ = "teachers" id = db.Column(db.Integer, unique=True, nullable=False, primary_key=True) name = db.Column(db.String(), nullable=False) about = db.Column(db.Text(), nullable=False) rating = db.Column(db.Float, nullable=False) price = db.Column(db.Integer, nullable=False) picture = db.Column(db.String(), nullable=False) goals = db.Column(db.JSON) free = db.Column(db.JSON) bookings = db.relationship("Booking")
class ForumPost(db.Model): """ This class corresponds to the database model for a ForumPost object. A high level interface that allows more efficient interactions with the relational database. """ __tablename__ = 'ForumPosts' id = db.Column(db.Integer, primary_key=True) author_username = db.Column(db.String(64), db.ForeignKey('Users.username')) forum_question_id = db.Column(db.Integer, db.ForeignKey('ForumQuestions.id')) content = db.Column(db.Text()) post_time = db.Column(db.DateTime, index=True, default=datetime.utcnow) def __repr__(self): return '<ForumPost {}>'.format(self.id)
class UserPost(db.Model): """ This class corresponds to the database model for a UserPost object. A high level interface that allows more efficient interactions with the relational database. """ __tablename__ = 'UserPosts' id = db.Column(db.Integer, primary_key=True, nullable=False) author_username = db.Column(db.String(64), db.ForeignKey('Users.username'), nullable=False) content = db.Column(db.Text(), nullable=False) post_time = db.Column(db.DateTime, index=True, default=datetime.utcnow, nullable=False) def __repr__(self): return '<UserPost {} by {}>'.format(self.id, self.author_username)
class ForumQuestion(db.Model): """ This class corresponds to the database model for a ForumQuestion object. A high level interface that allows more efficient interactions with the relational database. """ __tablename__ = 'ForumQuestions' id = db.Column(db.Integer, primary_key=True, nullable=False) author_username = db.Column(db.String(64), db.ForeignKey('Users.username'), nullable=False) content = db.Column(db.Text(), nullable=False) post_time = db.Column(db.DateTime, index=True, default=datetime.utcnow, nullable=False) forum_posts = db.relationship("ForumPost", backref='question', lazy='dynamic') def __repr__(self): return '<ForumQuestion {}>'.format(self.id)
class Message(db.Model): """ This class corresponds to the database model for a Message object. A high level interface that allows more efficient interactions with the relational database. Due to time constraints, we were not able to implement messaging. """ __tablename__ = 'Messages' id = db.Column(db.Integer, primary_key=True, nullable=False) sender_username = db.Column(db.String(64), db.ForeignKey('Users.username'), nullable=False) receiver_username = db.Column(db.String(64), db.ForeignKey('Users.username'), nullable=False) content = db.Column(db.Text(), nullable=False) post_time = db.Column(db.DateTime, index=True, default=datetime.utcnow, nullable=False) def __repr__(self): return '<Message ID {} ({} -> {})>'.format(self.id, self.sender_username, self.receiver_username)
class User(UserMixin, db.Model): def __init__(self, **kwargs): super(User, self).__init__(**kwargs) if self.role is None: if self.email == current_app.config.get('ADMIN_EMAIL'): self.role = Role.query.filter_by(name='Admin').first() else: self.role = Role.query.filter_by(name='User').first() __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(64), unique=True, index=True) username = db.Column(db.String(64), unique=True, index=True) password_hash = db.Column(db.String(128)) role_id = db.Column(db.Integer, db.ForeignKey('roles.id')) #Profile info fullname = db.Column(db.String(64)) location = db.Column(db.String(64)) desc = db.Column(db.Text()) member_since = db.Column(db.DateTime(), default=datetime.utcnow) last_visit = db.Column(db.DateTime(), default=datetime.utcnow) #Article posts = db.relationship('Post', backref='author', lazy='dynamic') @property def password(self): raise AttributeError('password is not readable property') @password.setter def password(self, password): self.password_hash = generate_password_hash(password=password) def verify_password(self, password): return check_password_hash(self.password_hash, password) #Check permissions def check_permission(self, permission): if self.role is not None: return (self.role.permissions & permission) == permission return False def check_admin(self): return self.check_permission(Permission.ADMINISTER) # Update vist time def ping(self): self.last_visit = datetime.utcnow() db.session.add(self) db.session.commit() # Generate fake records, Only for test!! @staticmethod def generate_fake(count=100): from sqlalchemy.exc import IntegrityError from random import seed import forgery_py seed() for i in range(count): u = User(email=forgery_py.internet.email_address(), username=forgery_py.internet.user_name(True), password=forgery_py.lorem_ipsum.word(), fullname=forgery_py.name.full_name(), location=forgery_py.address.city(), desc=forgery_py.lorem_ipsum.sentence(), member_since=forgery_py.date.date(True)) db.session.add(u) try: db.session.commit() except IntegrityError: db.session.rollback()