From b4dd54d36aab62fbf36bb5b9bd39007f9c35ffb0 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Sun, 8 Mar 2015 13:39:22 -0400 Subject: [PATCH] Adding score and place to team page, fixing create_app --- CTFd/__init__.py | 2 +- CTFd/admin.py | 4 +++- CTFd/models.py | 15 +++++++++++++++ CTFd/views.py | 4 +++- serve.py | 2 +- templates/admin/team.html | 11 +++++++++++ templates/team.html | 12 ++++++++++++ 7 files changed, 46 insertions(+), 4 deletions(-) diff --git a/CTFd/__init__.py b/CTFd/__init__.py index 3d580300..13edbdd2 100644 --- a/CTFd/__init__.py +++ b/CTFd/__init__.py @@ -7,7 +7,7 @@ import logging import os import sqlalchemy -def create_app(subdomain, username="", password=""): +def create_app(subdomain="", username="", password=""): app = Flask("CTFd", static_folder="../static", template_folder="../templates") with app.app_context(): app.config.from_object('CTFd.config') diff --git a/CTFd/admin.py b/CTFd/admin.py index 225cf2b0..6dd1e84c 100644 --- a/CTFd/admin.py +++ b/CTFd/admin.py @@ -277,9 +277,11 @@ def init_admin(app): user = Teams.query.filter_by(id=teamid).first() solves = Solves.query.filter_by(teamid=teamid).all() addrs = Tracking.query.filter_by(team=teamid).group_by(Tracking.ip).all() + score = user.score() + place = user.place() if request.method == 'GET': - return render_template('admin/team.html', solves=solves, team=user, addrs=addrs) + return render_template('admin/team.html', solves=solves, team=user, addrs=addrs, score=score, place=place) elif request.method == 'POST': name = request.form.get('name', None) password = request.form.get('password', None) diff --git a/CTFd/models.py b/CTFd/models.py index fb0484ae..d192558f 100644 --- a/CTFd/models.py +++ b/CTFd/models.py @@ -109,6 +109,21 @@ class Teams(db.Model): def __repr__(self): return '' % self.name + def score(self): + score = db.func.sum(Challenges.value).label('score') + stuff = db.session.query(Solves.teamid, score).join(Teams).join(Challenges).filter(Teams.banned == None, Teams.id==self.id).group_by(Solves.teamid).one() + print stuff + return stuff[1] if stuff[1] else 0 + + def place(self): + score = db.func.sum(Challenges.value).label('score') + quickest = db.func.max(Solves.date).label('quickest') + teams = db.session.query(Solves.teamid).join(Teams).join(Challenges).filter(Teams.banned == None).group_by(Solves.teamid).order_by(score.desc(), quickest).all() + #http://codegolf.stackexchange.com/a/4712 + i = teams.index((self.id,)) + 1 + k = i%10 + return "%d%s"%(i,"tsnrhtdd"[(i/10%10!=1)*(k<4)*k::4]) + class Solves(db.Model): __table_args__ = (db.UniqueConstraint('chalid', 'teamid'), {}) id = db.Column(db.Integer, primary_key=True) diff --git a/CTFd/views.py b/CTFd/views.py index 3550a0eb..c0b94252 100644 --- a/CTFd/views.py +++ b/CTFd/views.py @@ -106,10 +106,12 @@ def init_views(app): def team(teamid): user = Teams.query.filter_by(id=teamid).first() solves = Solves.query.filter_by(teamid=teamid).all() + score = user.score() + place = user.place() db.session.close() if request.method == 'GET': - return render_template('team.html', solves=solves, team=user) + return render_template('team.html', solves=solves, team=user, score=score, place=place) elif request.method == 'POST': json = {'solves':[]} for x in solves: diff --git a/serve.py b/serve.py index 3d562c24..aabe4ba4 100644 --- a/serve.py +++ b/serve.py @@ -1,3 +1,3 @@ from CTFd import create_app -app = create_app('') +app = create_app() app.run(debug=True, host="0.0.0.0", port=4000) diff --git a/templates/admin/team.html b/templates/admin/team.html index 40f0248b..d1fa4a4c 100644 --- a/templates/admin/team.html +++ b/templates/admin/team.html @@ -5,6 +5,17 @@

{{ team.name }}

+

+ {%if place %} + {{ place }} place + {% endif %} +

+

+ {%if score %} + {{ score }} points + {% endif %} +

+
diff --git a/templates/team.html b/templates/team.html index 81daa63e..6492f5d1 100644 --- a/templates/team.html +++ b/templates/team.html @@ -5,6 +5,18 @@

{{ team.name }}

+

+ {%if place %} + {{ place }} place + {% endif %} +

+

+ {%if score %} + {{ score }} points + {% endif %} +

+ +