2015-12-23 06:26:27 +00:00
|
|
|
from flask.ext.sqlalchemy import SQLAlchemy
|
2016-01-16 06:53:35 +00:00
|
|
|
|
2016-01-07 00:23:43 +00:00
|
|
|
import time
|
2016-01-16 06:53:35 +00:00
|
|
|
import traceback
|
2015-12-23 23:23:18 +00:00
|
|
|
import utils
|
2015-12-23 06:26:27 +00:00
|
|
|
|
2015-12-23 19:49:31 +00:00
|
|
|
db = SQLAlchemy()
|
|
|
|
|
2015-12-23 23:06:44 +00:00
|
|
|
class Users(db.Model):
|
2016-01-06 06:15:57 +00:00
|
|
|
uid = db.Column(db.Integer, unique=True, primary_key=True)
|
|
|
|
tid = db.Column(db.Integer)
|
|
|
|
name = db.Column(db.String(64))
|
|
|
|
username = db.Column(db.String(64), unique=True)
|
|
|
|
username_lower = db.Column(db.String(64), unique=True)
|
|
|
|
email = db.Column(db.String(64), unique=True)
|
|
|
|
password = db.Column(db.String(128))
|
2016-01-06 06:35:59 +00:00
|
|
|
admin = db.Column(db.Boolean)
|
2016-01-06 06:15:57 +00:00
|
|
|
utype = db.Column(db.Integer)
|
2016-01-07 00:23:43 +00:00
|
|
|
tid = db.Column(db.Integer)
|
|
|
|
registertime = db.Column(db.Integer)
|
2016-01-16 22:36:30 +00:00
|
|
|
reset_token = db.Column(db.String(64))
|
2015-12-24 00:40:59 +00:00
|
|
|
|
2016-01-06 06:15:57 +00:00
|
|
|
def __init__(self, name, username, email, password, utype=1):
|
|
|
|
self.name = name
|
|
|
|
self.username = username
|
|
|
|
self.username_lower = username.lower()
|
|
|
|
self.email = email.lower()
|
|
|
|
self.password = utils.hash_password(password)
|
|
|
|
self.utype = utype
|
2016-01-06 06:35:59 +00:00
|
|
|
self.admin = False
|
2016-01-07 00:23:43 +00:00
|
|
|
self.registertime = int(time.time())
|
2015-12-24 00:40:59 +00:00
|
|
|
|
|
|
|
class Teams(db.Model):
|
2016-01-06 06:15:57 +00:00
|
|
|
tid = db.Column(db.Integer, primary_key=True)
|
2016-01-12 03:54:26 +00:00
|
|
|
teamname = db.Column(db.String(64), unique=True)
|
|
|
|
teamname_lower = db.Column(db.String(64), unique=True)
|
2016-01-06 06:15:57 +00:00
|
|
|
school = db.Column(db.Text)
|
|
|
|
owner = db.Column(db.Integer)
|
2016-01-16 06:53:35 +00:00
|
|
|
observer = db.Column(db.Boolean)
|
2015-12-24 00:40:59 +00:00
|
|
|
|
2016-01-16 06:53:35 +00:00
|
|
|
def __init__(self, teamname, school, owner, observer):
|
2016-01-12 03:54:26 +00:00
|
|
|
self.teamname = teamname
|
|
|
|
self.teamname_lower = teamname.lower()
|
2016-01-16 06:53:35 +00:00
|
|
|
self.school = school
|
2016-01-12 03:54:26 +00:00
|
|
|
self.owner = owner
|
2016-01-16 06:53:35 +00:00
|
|
|
self.observer = observer
|
2016-01-12 03:54:26 +00:00
|
|
|
|
|
|
|
def get_members(self):
|
|
|
|
members = [ ]
|
|
|
|
for member in Users.query.filter_by(tid=self.tid).all():
|
|
|
|
members.append({
|
2016-01-16 21:12:32 +00:00
|
|
|
"username": member.username,
|
|
|
|
"name": member.name,
|
|
|
|
"captain": member.uid == self.owner
|
2016-01-12 03:54:26 +00:00
|
|
|
})
|
|
|
|
return members
|
2015-12-24 00:40:59 +00:00
|
|
|
|
2016-01-16 06:53:35 +00:00
|
|
|
def points(self):
|
|
|
|
score = db.func.sum(Problems.value).label("score")
|
|
|
|
team = db.session.query(Solves.tid, score).join(Teams).join(Problems).filter(Teams.tid==self.tid).group_by(Solves.tid).first()
|
|
|
|
if team:
|
|
|
|
return team.score
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def place(self):
|
|
|
|
score = db.func.sum(Problems.value).label("score")
|
|
|
|
quickest = db.func.max(Solves.date).label("quickest")
|
|
|
|
teams = db.session.query(Solves.tid).join(Teams).join(Problems).filter().group_by(Solves.tid).order_by(score.desc(), quickest).all()
|
|
|
|
try:
|
|
|
|
i = teams.index((self.tid,)) + 1
|
|
|
|
k = i % 10
|
|
|
|
return (i, "%d%s" % (i, "tsnrhtdd"[(i / 10 % 10 != 1) * (k < 4) * k::4]))
|
|
|
|
except ValueError:
|
|
|
|
return (-1, "--")
|
|
|
|
|
2016-01-17 02:23:34 +00:00
|
|
|
def get_invitation_requests(self, frid=None):
|
|
|
|
if frid is not None:
|
|
|
|
req = db.session.query(TeamInvitations).filter_by(rtype=1, frid=frid, toid=self.tid).first()
|
|
|
|
if req is None:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
user = db.session.query(Users).filter_by(uid=req.frid).first()
|
|
|
|
return { "username": user.username, "name": user.name, "uid": user.uid }
|
|
|
|
result = [ ]
|
|
|
|
requests = db.session.query(TeamInvitations).filter_by(rtype=1, toid=self.tid).all()
|
|
|
|
for req in requests:
|
|
|
|
user = db.session.query(Users).filter_by(uid=req.frid).first()
|
|
|
|
result.append({
|
|
|
|
"username": user.username,
|
|
|
|
"name": user.name,
|
|
|
|
"uid": user.uid
|
|
|
|
})
|
|
|
|
return result
|
|
|
|
|
2016-01-16 21:12:32 +00:00
|
|
|
def get_pending_invitations(self, toid=None):
|
|
|
|
if toid is not None:
|
|
|
|
invitation = db.session.query(TeamInvitations).filter_by(rtype=0, frid=self.tid, toid=toid).first()
|
|
|
|
if invitation is None:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
user = db.session.query(Users).filter_by(uid=invitation.toid).first()
|
|
|
|
return { "username": user.username, "name": user.name, "uid": user.uid }
|
|
|
|
result = [ ]
|
|
|
|
invitations = db.session.query(TeamInvitations).filter_by(rtype=0, frid=self.tid).all()
|
|
|
|
for invitation in invitations:
|
|
|
|
user = db.session.query(Users).filter_by(uid=invitation.toid).first()
|
|
|
|
result.append({
|
|
|
|
"username": user.username,
|
|
|
|
"name": user.name,
|
|
|
|
"uid": user.uid
|
|
|
|
})
|
|
|
|
return result
|
|
|
|
|
2015-12-24 00:40:59 +00:00
|
|
|
class Problems(db.Model):
|
2016-01-06 06:15:57 +00:00
|
|
|
pid = db.Column(db.Integer, primary_key=True)
|
|
|
|
name = db.Column(db.String(128))
|
|
|
|
category = db.Column(db.String(128))
|
|
|
|
description = db.Column(db.Text)
|
|
|
|
hint = db.Column(db.Text)
|
|
|
|
flag = db.Column(db.Text)
|
|
|
|
disabled = db.Column(db.Boolean, default=False)
|
|
|
|
value = db.Column(db.Integer)
|
|
|
|
solves = db.Column(db.Integer, default=0)
|
2015-12-24 00:40:59 +00:00
|
|
|
|
2016-01-06 06:15:57 +00:00
|
|
|
def __init__(self, name, category, description, hint, flag, value):
|
|
|
|
self.name = name
|
|
|
|
self.category = category
|
|
|
|
self.description = description
|
|
|
|
self.hint = hint
|
|
|
|
self.flag = flag
|
|
|
|
self.value = value
|
2015-12-24 00:40:59 +00:00
|
|
|
|
2016-01-02 20:45:05 +00:00
|
|
|
class Files(db.Model):
|
2016-01-06 06:15:57 +00:00
|
|
|
fid = db.Column(db.Integer, primary_key=True)
|
|
|
|
pid = db.Column(db.Integer)
|
|
|
|
location = db.Column(db.Text)
|
2016-01-02 20:45:05 +00:00
|
|
|
|
2016-01-06 06:15:57 +00:00
|
|
|
def __init__(self, pid, location):
|
|
|
|
self.pid = pid
|
|
|
|
self.location = location
|
2016-01-02 20:45:05 +00:00
|
|
|
|
2015-12-24 00:40:59 +00:00
|
|
|
class Solves(db.Model):
|
2016-01-06 06:15:57 +00:00
|
|
|
sid = db.Column(db.Integer, primary_key=True)
|
2016-01-16 06:53:35 +00:00
|
|
|
pid = db.Column(db.Integer, db.ForeignKey("problems.pid"))
|
|
|
|
tid = db.Column(db.Integer, db.ForeignKey("teams.tid"))
|
2016-01-06 06:15:57 +00:00
|
|
|
date = db.Column(db.Integer, default=utils.get_time_since_epoch())
|
2016-01-16 06:53:35 +00:00
|
|
|
team = db.relationship("Teams", foreign_keys="Solves.tid", lazy="joined")
|
|
|
|
prob = db.relationship("Problems", foreign_keys="Solves.pid", lazy="joined")
|
|
|
|
correct = db.Column(db.Boolean)
|
|
|
|
flag = db.Column(db.Text)
|
2015-12-24 00:40:59 +00:00
|
|
|
|
2016-01-16 06:53:35 +00:00
|
|
|
def __init__(self, pid, tid, flag, correct):
|
2016-01-06 06:15:57 +00:00
|
|
|
self.pid = pid
|
|
|
|
self.tid = tid
|
2016-01-16 06:53:35 +00:00
|
|
|
self.flag = flag
|
|
|
|
self.correct = correct
|
2016-01-06 06:15:57 +00:00
|
|
|
|
|
|
|
class LoginTokens(db.Model):
|
|
|
|
sid = db.Column(db.String(64), unique=True, primary_key=True)
|
|
|
|
uid = db.Column(db.Integer)
|
|
|
|
username = db.Column(db.String(32))
|
|
|
|
active = db.Column(db.Boolean)
|
|
|
|
issued = db.Column(db.Integer)
|
|
|
|
expiry = db.Column(db.Integer)
|
|
|
|
ua = db.Column(db.String(128))
|
|
|
|
ip = db.Column(db.String(16))
|
|
|
|
|
2016-01-07 00:23:43 +00:00
|
|
|
def __init__(self, uid, username, expiry=int(time.time()), active=True, ua=None, ip=None):
|
2016-01-06 06:15:57 +00:00
|
|
|
self.sid = utils.generate_string()
|
|
|
|
self.uid = uid
|
|
|
|
self.username = username
|
2016-01-07 00:23:43 +00:00
|
|
|
self.issued = int(time.time())
|
2016-01-06 06:15:57 +00:00
|
|
|
self.expiry = expiry
|
|
|
|
self.active = active
|
|
|
|
self.ua = ua
|
2016-01-16 21:12:32 +00:00
|
|
|
self.ip = ip
|
|
|
|
|
|
|
|
class TeamInvitations(db.Model):
|
|
|
|
rid = db.Column(db.Integer, primary_key=True)
|
|
|
|
rtype = db.Column(db.Integer)
|
|
|
|
frid = db.Column(db.Integer)
|
|
|
|
toid = db.Column(db.Integer)
|
|
|
|
date = db.Column(db.Integer, default=utils.get_time_since_epoch())
|
|
|
|
|
|
|
|
def __init__(self, rtype, frid, toid):
|
|
|
|
self.rtype = rtype
|
|
|
|
self.frid = frid
|
2016-01-16 22:36:30 +00:00
|
|
|
self.toid = toid
|