class Result(db.Model): __tablename__ = 'results' id = db.Column(db.Integer, primary_key=True) topicID = db.Column( db.Integer, db.ForeignKey('topics.id', onupdate='CASCADE', ondelete='CASCADE')) accountID = db.Column( db.Integer, db.ForeignKey('users.id', onupdate='CASCADE', ondelete='CASCADE')) date = db.Column(db.DateTime, nullable=False) percentage = db.Column(db.String(3), nullable=False) def __init__(self, topicID, accountID, percentage): self.topicID = topicID self.accountID = accountID self.date = datetime.utcnow() self.percentage = percentage def serialize(self): return { 'id': self.id, 'userID': self.accountID, 'date': str(self.date), 'percentage': self.percentage, }
class Ban(db.Model): __tablename__ = 'banned' id = db.Column(db.Integer, primary_key=True) userID = db.Column( db.Integer, db.ForeignKey('users.id', onupdate="CASCADE", ondelete="CASCADE")) date = db.Column(db.DateTime, nullable=False) user = db.relationship('User', backref=backref('banned')) def __init__(self, userID): self.userID = userID self.date = datetime.utcnow()
class Help(db.Model): __tablename__ = 'help' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) section = db.Column(db.String(100), nullable=False) slug = db.Column(db.String(200), nullable=False) content = db.Column(db.Text, nullable=False) def __init__(self, title, slug, section, content): self.title = title self.slug = slug self.section = section self.content = content
class Warning(db.Model): __tablename__ = 'warnings' id = db.Column(db.Integer, primary_key=True) userID = db.Column( db.Integer, db.ForeignKey('users.id', onupdate='CASCADE', ondelete='CASCADE')) issue = db.Column(db.Text(100), nullable=False) date = db.Column(db.DateTime, nullable=False) def __init__(self, userID, issue): self.userID = userID self.issue = issue self.date = datetime.utcnow()
class Report(db.Model): __tablename__ = 'reports' id = db.Column(db.Integer, primary_key=True) userID = db.Column( db.Integer, db.ForeignKey('users.id', onupdate='CASCADE', ondelete='CASCADE')) issue = db.Column(db.Text(100), nullable=False) userReportedID = db.Column( db.Integer, db.ForeignKey('users.id', onupdate='CASCADE', ondelete='CASCADE')) date = db.Column(db.DateTime, nullable=False) user = db.relationship('User', foreign_keys=[userID]) userReported = db.relationship('User', foreign_keys=[userReportedID]) def __init__(self, userID, userReportedID, issue): self.userID = userID self.userReportedID = userReportedID self.issue = issue self.date = datetime.utcnow()
class Question(db.Model): __tablename__ = 'questions' id = db.Column(db.Integer, primary_key=True) topicID = db.Column( db.Integer, db.ForeignKey('topics.id', onupdate='CASCADE', ondelete='CASCADE')) question = db.Column(db.String(8000), nullable=False) answer = db.Column(db.String(8000)) def __init__(self, topicID, question, answer): self.topicID = topicID self.question = question self.answer = answer def serialize(self): return { 'id': self.id, 'title': self.question, 'answer': self.answer if self.answer != "" else None }
class Follow(db.Model): __tablename__ = 'follow' id = db.Column(db.Integer, primary_key=True) follower = db.Column( db.Integer, db.ForeignKey('users.id', onupdate='CASCADE', ondelete='CASCADE')) following = db.Column( db.Integer, db.ForeignKey('users.id', onupdate='CASCADE', ondelete='CASCADE')) def __init__(self, follower, following): self.follower = follower self.following = following def serialize(self): return { 'id': self.id, 'follower': self.follower, 'following': self.following, }
class WeeklyGoal(db.Model): __tablename__ = 'weekly_goal' id = db.Column(db.Integer, primary_key=True) userID = db.Column( db.Integer, db.ForeignKey('users.id', onupdate='CASCADE', ondelete='CASCADE')) progress = db.Column(db.Integer, nullable=False) date = db.Column(db.Date, nullable=False) user = db.relationship('User', backref=backref('goal')) def __init__(self, progress): self.userID = current_user.id self.progress = progress self.date = datetime.today() - timedelta( days=datetime.today().isoweekday() % 7) @property def total(self): return self.user.weeklyGoal def completed(self): if self.progress >= self.user.weeklyGoal: return True else: return False def serialize(self): return { 'id': self.id, 'userID': self.userID, 'progress': self.progress, 'date': str(self.date), 'user': self.user.serialize(), }
class Stats(db.Model): __tablename__ = 'stats' id = db.Column(db.Integer, primary_key=True) users = db.Column(db.Integer, nullable=False) subjects = db.Column(db.Integer, nullable=False) topics = db.Column(db.Integer, nullable=False) questions = db.Column(db.Integer, nullable=False) results = db.Column(db.Integer, nullable=False) date = db.Column(db.Date, nullable=False) def __init__(self, users, subjects, topics, questions, results, date): self.users = users self.subjects = subjects self.topics = topics self.questions = questions self.results = results self.date = datetime.utcnow()
class Subject(db.Model): __tablename__ = 'subjects' id = db.Column(db.Integer, primary_key=True) hash = db.Column(db.String(6)) accountID = db.Column( db.Integer, db.ForeignKey('users.id', onupdate="CASCADE", ondelete="CASCADE")) name = db.Column(db.String(100), nullable=False, index=True) slug = db.Column(db.String(200), nullable=False) color = db.Column(db.Integer, nullable=False) date = db.Column(db.DateTime, nullable=False) topic = db.relationship('Topic', backref=backref('subject')) user = db.relationship('User') def __init__(self, accountID, name, color): self.accountID = accountID self.name = name self.color = color self.slug = utils.subjectSlug(name) self.date = datetime.utcnow() @property def shareLink(self): string = 's' + self.hash return url_for('social.share', string=string, _external=True) @property def colors(self): return utils.getColorByNumber(self.color) def serialize(self): return { 'id': self.id, 'name': self.name, 'userID': self.accountID, 'color': self.color, 'date': str(self.date), }
class Topic(db.Model): __tablename__ = 'topics' id = db.Column(db.Integer, primary_key=True) hash = db.Column(db.String(6)) subjectID = db.Column( db.Integer, db.ForeignKey('subjects.id', onupdate='CASCADE', ondelete='CASCADE')) name = db.Column(db.String(150), nullable=False, index=True) slug = db.Column(db.String(200), nullable=False) questions = db.relationship('Question', backref=backref('topic')) date = db.Column(db.DateTime, nullable=False) def __init__(self, subjectID, name): self.subjectID = session['newTopicSubjectID'] = subjectID self.name = name self.slug = utils.topicSlug(name) self.date = datetime.utcnow() @property def shareLink(self): string = 't' + self.hash return url_for('social.share', string=string, _external=True) @property def colors(self): return self.subject.colors @property def user(self): return self.subject.user def serialize(self): return { 'id': self.id, 'name': self.name, 'date': str(self.date), 'subjectID': self.subjectID, }
class OAuth(db.Model): __tablename__ = 'oauth' id = db.Column(db.Integer, primary_key=True) userID = db.Column(db.Integer, db.ForeignKey('users.id', onupdate='CASCADE', ondelete='CASCADE'), nullable=False) socialID = db.Column(db.String(64), nullable=False) provider = db.Column(db.String(1), nullable=False) accessToken = db.Column(db.Text, nullable=False) name = db.Column(db.Text, nullable=False) def __init__(self, userID, socialID, provider, accessToken, name): self.userID = userID self.socialID = socialID self.provider = provider self.accessToken = accessToken self.name = name @hybrid_property def url(self): if self.provider == 'G': return 'https://plus.google.com/' + str(self.socialID) elif self.provider == 'F': return 'https://facebook.com/' + str(self.socialID) def serialize(self): return { 'id': self.id, 'userID': self.userID, 'socialID': self.socialID, 'provider': self.provider, 'accessToken': self.accessToken, 'name': self.name }
class User(UserMixin, db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) hash = db.Column(db.String(6)) name = db.Column(db.String(100), nullable=False, index=True) email = db.Column(db.String(320), nullable=False) password = db.Column(db.String(60)) location = db.Column(db.String(2)) education = db.Column(db.String(255)) profile = db.Column(db.Boolean, nullable=False, default=True) picture = db.Column(db.String(32)) activated = db.Column(db.Boolean, nullable=False, default=False) admin = db.Column(db.Boolean, nullable=False, default=False) joined = db.Column(db.DateTime, nullable=False) weeklyGoal = db.Column(db.Integer, nullable=False, default=8) def __init__(self, name, email, password): self.name = name self.email = email self.password = password self.joined = datetime.utcnow() def setPassword(self, password): self.password = bcrypt.generate_password_hash(password) def verifyPassword(self, password): # If there is no password set - this happens when users create accounts # using their social logins if self.password == None: return False return bcrypt.check_password_hash(self.password, password) @property def profilePicture(self): if self.picture: return { '80': 'https://data.revisify.com/{}/{}_80x80.jpeg'.format( str(self.id), str(self.picture)), '270': 'https://data.revisify.com/{}/{}_270x270.jpeg'.format( str(self.id), str(self.picture)), 'cover': 'https://data.revisify.com/{}/{}_cover.jpeg'.format( str(self.id), str(self.picture)), 'default': False } else: # Return default user icon return { '80': url_for('static', filename='images/userIcon_80x80.png'), '270': url_for('static', filename='images/userIcon_270x270.png'), 'cover': url_for('static', filename='images/userIcon_cover.png'), 'default': True } @property def shareLink(self): string = 'u' + self.hash return url_for('social.share', string=string, _external=True) def serialize(self): return { 'id': self.id, 'name': self.name, 'email': self.email, 'location': self.location, 'education': self.education, 'profile': self.profile, 'picture': self.picture, 'activated': self.activated, 'admin': self.admin, 'joined': str(self.joined), 'weeklyGoal': self.weeklyGoal } @staticmethod def generate(count): from sqlalchemy.exc import IntegrityError from random import seed import forgery_py as fake from hashids import Hashids hashid = Hashids(salt=app.config['SECRET_KEY'], min_length=6) seed() for i in range(count): u = User(name=fake.name.full_name(), email=fake.internet.email_address(), password=None, location=randomLocation(), education=randomSchool(), profile=True, activated=True, admin=False) db.session.add(u) db.session.flush() u.hash = hashid.encode(u.id) try: db.session.commit() except IntegrityError: db.session.rollback()