class Account(db.Model): id = db.Column(db.Integer, primary_key=True) nickname = db.Column(db.String(20), nullable=False) password_hash = db.Column(db.String(128)) solved_or_rating = db.Column(db.Integer, nullable=False, default=0) submitted_or_max_rating = db.Column(db.Integer, nullable=False, default=0) update_status = db.Column(db.Integer, default=1, index=True) oj_name = db.Column(db.String(20), nullable=False) last_update_time = db.Column(db.DateTime) # connect to User user_id = db.Column(db.Integer, db.ForeignKey('user.id')) user = db.relationship('User', backref=db.backref('account', lazy='dynamic')) def __init__(self, oj_name, nickname, password_or_oj_id, user): self.oj_name = oj_name self.nickname = nickname self.password = password_or_oj_id self.user = user self.last_update_time = datetime.datetime.min @property def password(self): return security.decrypt(self.password_hash).decode('utf-8') @password.setter def password(self, value): self.password_hash = security.encrypt(value.encode('utf-8')) def __repr__(self): return '<%s Account %s>: %d / %d' % (self.oj_name, self.nickname, self.solved_or_rating, self.submitted_or_max_rating) def set_problem_count(self, v1, v2): self.solved_or_rating = v1 self.submitted_or_max_rating = v2 def get_problem_count(self): if self.oj_name in ['cf', 'bc']: return { 'rating': self.solved_or_rating, 'max_rating': self.submitted_or_max_rating } else: return { 'solved': self.solved_or_rating, 'submitted': self.submitted_or_max_rating } def save(self): db.session.add(self) db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class News(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) url = db.Column(db.String(100), nullable=True) shortcut = db.Column(db.Text) content = db.Column(db.Text) last_update_time = db.Column(db.DateTime) is_top = db.Column(db.SmallInteger, default=0) is_draft = db.Column(db.SmallInteger, default=0) #rank = db.Column(db.Integer, default=0) # connect to User user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete="SET NULL")) user = db.relationship('User', backref=db.backref('news', lazy='dynamic')) # connect to Tag tags = db.relationship('Tag', secondary=new_stags, backref=db.backref('news', lazy='dynamic')) @property def md_shortcut(self): return mdFilter.markdown(self.shortcut) @md_shortcut.setter def md_shortcut(self, data): self.shortcut = data @property def md_content(self): return mdFilter.markdown(self.content) @md_content.setter def md_content(self, data): self.content = data def __init__(self, title, url, is_top, user): self.title = title self.url = url self.user = user self.is_top = is_top self.last_update_time = datetime.datetime.now() @property def serialize(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns} def __repr__(self): return '<News>' def save(self): db.session.add(self) db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class SolutionArticle(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) shortcut = db.Column(db.Text) content = db.Column(db.Text) problem_oj_name = db.Column(db.String(20)) problem_pid = db.Column(db.String(12)) last_update_time = db.Column(db.DateTime) is_top = db.Column(db.SmallInteger, default=0) is_draft = db.Column(db.SmallInteger, default=0) #rank = db.Column(db.Integer, default=0) # connect to User user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete="SET NULL"), nullable=True) user = db.relationship('User', backref=db.backref('solution', lazy='dynamic')) # connect to Tag tags = db.relationship('Tag', secondary=solution_tags,backref=db.backref('solutions', lazy='dynamic')) @property def md_shortcut(self): return mdFilter.markdown(self.shortcut) @md_shortcut.setter def md_shortcut(self, data): self.shortcut = data @property def md_content(self): return mdFilter.markdown(self.shortcut+self.content) @md_content.setter def md_content(self, data): self.content = data def __init__(self, title, user): self.title = title self.user = user self.last_update_time = datetime.datetime.now() def __repr__(self): return '<Article>' def save(self): db.session.add(self) db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class Honor(db.Model): id = db.Column(db.Integer, primary_key=True) contest_name = db.Column(db.String(64), nullable=False) contest_level = db.Column(db.Integer, nullable=False, default=0) acquire_time = db.Column(db.DateTime, nullable=False) team_name = db.Column(db.String(64)) introduce = db.Column(db.Text) # connect to Resource resources = db.relationship('Resource', secondary=honor_resources, backref=db.backref('honors', lazy='dynamic')) # connect to User users = db.relationship('User', secondary=honor_users, backref=db.backref('honors', lazy='dynamic')) def __init__(self): pass def __repr__(self): return '<Honor>@' + self.contest_name @property def md_introduce(self): return mdFilter.markdown(self.introduce) @md_introduce.setter def md_introduce(self, data): self.introduce = data @property def serialize(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns} def save(self): db.session.add(self) db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class Resource(db.Model): id = db.Column(db.Integer, primary_key=True) filename = db.Column(db.String(50), nullable=False) name = db.Column(db.String(50), nullable=False, unique=True, default='UNTITLED') description = db.Column(db.Text) type = db.Column(db.Integer, nullable=False) level = db.Column(db.Integer, nullable=False, default=ResourceLevel.PRIVATE) usage = db.Column(db.Integer, nullable=False, default=ResourceUsage.OTHER_RES) upload_time = db.Column(db.DateTime) # connect to User user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete="SET NULL"), nullable=True) user = db.relationship('User', backref=db.backref('resource', lazy='dynamic')) def __repr__(self): return '<Resource>@' + self.name.encode('utf-8') @property def file_type(self): return unicode(self.filename).split('.')[-1] @property def serialize(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns} def save(self): db.session.add(self) db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class Tag(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(20), nullable=False, unique=True) def __init__(self, name): self.name = name def __repr__(self): return self.name @property def serialize(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns} def save(self): db.session.add(self) db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class Submit(db.Model): id = db.Column(db.Integer, primary_key=True) pro_id = db.Column(db.String(12)) run_id = db.Column(db.String(20)) submit_time = db.Column(db.DateTime, index=True) run_time = db.Column(db.Integer) memory = db.Column(db.Integer) lang = db.Column(db.String(50)) result = db.Column(db.String(100)) code = db.Column(db.Text) update_status = db.Column(db.Integer) oj_name = db.Column(db.String(20), nullable=False) user_name = db.Column(db.String(25), nullable=True) # connect to Account user_id = db.Column(db.Integer, db.ForeignKey('user.id')) user = db.relationship('User', backref=db.backref('submit', lazy='dynamic')) def __init__(self, pro_id, account): self.pro_id = pro_id self.user = account.user self.oj_name = account.oj_name self.user_name = account.user.name self.update_status = 0 def update_info(self, run_id, submit_time, run_time, memory, lang, code, result): self.code = code self.run_id = run_id self.submit_time = submit_time self.run_time = run_time self.memory = memory self.lang = lang self.result = result self.save() def __repr__(self): return u'User:"******" \tProblemID : {1} \tRUNID : {2}'.format( self.user_name, self.pro_id, self.run_id) def save(self): db.session.add(self) db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(25), unique=True, index=True, nullable=False) name = db.Column(db.String(25)) password_hash = db.Column(db.String(128)) stu_id = db.Column(db.String(20)) gender = db.Column(db.Boolean) email = db.Column(db.String(65)) phone = db.Column(db.String(15)) remark = db.Column(db.String(50)) school = db.Column(db.String(20), nullable=False) college = db.Column(db.String(64)) grade = db.Column(db.String(32)) score = db.Column(db.Integer, default=0) current_week_submit = db.Column(db.Integer, default=0) current_week_solved = db.Column(db.Integer, default=0) last_week_submit = db.Column(db.Integer, default=0) last_week_solved = db.Column(db.Integer, default=0) create_time = db.Column(db.DateTime) rights = db.Column(db.Integer) active = db.Column(db.Integer, default=1) situation = db.Column(db.Text) apply_reason = db.Column(db.Text) def __init__(self, username, name, password, school, gender, email): self.username = username self.name = name self.password = password self.school = school self.gender = gender self.email = email self.create_time = datetime.datetime.now() self.rights = 0 def __repr__(self): return '<User %s>' % self.name @property def serialize(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns} @property def is_apply(self): return self.rights & 8 == 8 @property def is_admin(self): return self.rights & 4 == 4 @property def is_coach(self): return self.rights & 2 == 2 @property def is_student(self): return self.rights & 1 == 1 @property def is_training(self): return self.active == 1 @property def password(self): raise AttributeError('password is not a readable attribute') def is_coach_of(self, other): return self.is_coach and (self.school == other.school) and not other.is_admin def update_score(self): score = 0 if self.account: accounts = self.account.all() for account in accounts: if account.oj_name in ['cf', 'bc']: tmp = account.get_problem_count() score += tmp['rating'] * 0.4 + tmp['max_rating'] * 0.1 else: tmp = account.get_problem_count() score += tmp['solved'] * 0.5 self.score = score def update(self): now = datetime.datetime.now() permit_date = now - datetime.timedelta(days=now.weekday()) permit_date = permit_date.replace(hour=0, minute=0, second=0) permit_date1 = permit_date - datetime.timedelta(days=7) last_submitted = self.submit.filter(Submit.submit_time < permit_date, Submit.submit_time > permit_date1) last_solved = db.session.query(Submit.oj_name, Submit.pro_id).distinct().filter( Submit.user_id == self.id, Submit.submit_time < permit_date, Submit.submit_time > permit_date1, or_(Submit.result == 'OK', Submit.result == 'Accepted')) current_submitted = self.submit.filter( Submit.submit_time > permit_date) current_solved = db.session.query(Submit.oj_name, Submit.pro_id).distinct().filter( Submit.user_id == self.id, Submit.submit_time > permit_date, or_(Submit.result == 'OK', Submit.result == 'Accepted')) self.last_week_solved = last_solved.count() self.last_week_submit = last_submitted.count() self.current_week_solved = current_solved.count() self.current_week_submit = current_submitted.count() self.update_score() @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def verify_password(self, password): return check_password_hash(self.password_hash, password) def gravatar(self, size=100, default='identicon', rating='g'): url = 'http://gravatar.duoshuo.com/avatar' hash = hashlib.md5(self.name.encode('utf-8')).hexdigest() return '{url}/{hash}?s={size}&d={default}&r={rating}'.format( url=url, hash=hash, size=size, default=default, rating=rating) def save(self): db.session.add(self) db.session.commit() def delete(self): db.session.delete(self) db.session.commit()