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) password = db.Column(db.String(120), unique=True, nullable=False) image_file = db.Column(db.String(32)) age = db.Column(db.Integer, nullable=False) role_id = db.Column(db.Integer, db.ForeignKey('roles.id')) arm_id = db.Column(db.Integer, db.ForeignKey('arms.id')) subjects = db.relationship('Subjectrating', secondary=user_subjects, lazy='subquery', backref=db.backref('users', lazy=True)) def get_reset_token(self, expires_sec=1800): s = Serializer(current_app.config['SECRET_KEY'], expires_sec) return s.dumps({'user_id': self.id}).decode('utf-8') @staticmethod def verify_token(token): s = Serializer(current_app.config['SECRET_KEY']) try: user_id = s.loads(token)['user_id'] except: return None return User.query.get(user_id) def __init__(self, **kwargs): if kwargs["email"] is not None: # and kwargs["image_file"] is None: self.image_file = hashlib.md5( kwargs["email"].encode('utf-8')).hexdigest() self.username = kwargs["username"] self.email = kwargs["email"] self.age = kwargs["age"] self.role = kwargs["role"] self.arm = kwargs["arm"] self.password = bcrypt.generate_password_hash( kwargs["password"]).decode('utf-8') self.image_file = self.gravatar(self) def change_email(self): self.email = new_email self.image_file = hashlib.md5(self.email.encode('utf-8')).hexdigest() db.session.add(self) return True @staticmethod def gravatar(self, size=100, default='identicon', rating='g'): if request.is_secure: url = 'https://secure.gravatar.com/avatar' else: url = 'http://www.gravatar.com/avatar' hash = self.image_file or hashlib.md5( self.email.encode('utf-8')).hexdigest() return f'{url}/{hash}?s={size}&d={default}&r={rating}' def __repr__(self): return f"user('{self.username}','{self.email}','{self.image_file}')"
class Question(db.Model): id = db.Column(db.Integer, primary_key=True) question = db.Column(db.String(300), nullable=False, unique=False) answer = db.Column(db.String(20), nullable=False, unique=False) Options = db.relationship('Option', backref='option', lazy=True) def __repr__(self): return f"Question ('{self.name}', '{self.answer}')"
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) content = db.Column(db.Text, nullable=False) username = db.Column(db.String(20), db.ForeignKey('user.username'), nullable=False) def __repr__(self): return f"Post('{self.username}',{self.title}', '{self.content}',{self.date_posted}')"
class User(db.Model): 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) isadmin = db.Column(db.Boolean(), nullable=False) image_file = db.Column(db.String(20), nullable=False, default='default.jpg') password = db.Column(db.String(60), nullable=False) posts = db.relationship('Post', backref='author', lazy=True) def __repr__(self): return f"User('{self.username}', '{self.email}', '{self.image_file}','{self.isadmin}')"
class Role(db.Model): __tablename__ = 'roles' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) users = db.relationship('User', backref='role', lazy='dynamic') def __repr__(self): return f"Role '({self.name})'"
class Option(db.Model): id = db.Column(db.Integer, primary_key=True) Option = db.Column(db.String(50), nullable=False) question_id = db.Column(db.Integer, db.ForeignKey('question.id'), nullable=False) def __repr__(self): return f"Option ('{self.name}'"
class Subject(db.Model): id = db.Column(db.Integer, primary_key=True, nullable=False) name = db.Column(db.String(120), unique=True, nullable=False) arm_id = db.Column(db.Integer, db.ForeignKey('arms.id')) subject_rating = db.relationship('Subjectrating', backref='subject_rating', lazy=True) def __repr__(self): return f"Subject ('{self.name}')"
class Arm(db.Model): __tablename__ = 'arms' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(32), nullable=False) # subjects = db.relationship('Subject', backref='subject', lazy = True) arm_subjects = db.relationship('Subject', secondary=arm_subjects, lazy='subquery', backref=db.backref('arm', lazy=True)) users = db.relationship('User', backref='arm', lazy='dynamic') def __repr__(self): return f"Arm ('{self.name}')"