class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) image_file = db.Column(db.String(20), nullable=False, default='default.jpg') password = db.Column(db.String(60), nullable=False) questions = db.relationship('Question', backref="author", lazy=True) answers = db.relationship('Answer', backref="author", lazy=True) def get_reset_token(self, expires_sec=1800): s = Serializer(current_app.secret_key, expires_sec) return s.dumps({'user_id': self.id}).decode("utf-8") @staticmethod def verify_reset_token(token): s = Serializer(current_app.secret_key) try: user_id = s.loads(token)['user_id'] except: return None return User.query.get(user_id) def __repr__(self): return f"User('{self.username}', '{self.email}', '{self.image_file}')"
class Question_Tag(db.Model): id = db.Column(db.Integer, primary_key=True) question_id = db.Column(db.Integer, db.ForeignKey('question.id'), nullable=False) tag_id = db.Column(db.Integer, db.ForeignKey("tag.id"), nullable=False) def __repr__(self): return f"Question_Tag(qid:{self.question_id}, tid:{self.tag_id})"
class Question(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) message = db.Column(db.Text(), nullable=False) submission_time = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) vote_number = db.Column(db.Integer, nullable=False, default=0) view_number = db.Column(db.Integer, nullable=False, default=0) image = db.Column(db.String(60), nullable=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) voted_by = db.Column(db.String(), nullable=True) def __repr__(self): return f"Question('{self.title}', '{self.submission_time}')"
class Answer(db.Model): id = db.Column(db.Integer, primary_key=True) message = db.Column(db.String(300), nullable=False) vote_number = db.Column(db.Integer, nullable=False, default=0) question_id = db.Column(db.Integer, db.ForeignKey("question.id"), nullable=False) submission_time = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) image = db.Column(db.String(60), nullable=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) voted_by = db.Column(db.String(), nullable=True) def __repr__(self): return f"Answer({self.message}, {self.submission_time}. Question_id={self.question_id})"
class Tag(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(15), nullable=False) def __repr__(self): return f"Tag({self.name})"