2016-01-02 20:45:05 +00:00
|
|
|
import hashlib
|
2016-01-02 05:15:29 +00:00
|
|
|
import logger
|
2016-01-02 22:44:32 +00:00
|
|
|
import os
|
2016-01-02 05:15:29 +00:00
|
|
|
|
2015-12-31 02:56:00 +00:00
|
|
|
from flask import Blueprint, session, request
|
|
|
|
from flask import current_app as app
|
2016-01-02 20:45:05 +00:00
|
|
|
from werkzeug import secure_filename
|
2015-12-31 02:56:00 +00:00
|
|
|
|
2016-01-02 20:45:05 +00:00
|
|
|
from models import db, Files, Problems, Solves, Teams
|
2016-01-02 19:30:42 +00:00
|
|
|
from decorators import admins_only, api_wrapper, login_required
|
2015-12-31 02:56:00 +00:00
|
|
|
|
|
|
|
blueprint = Blueprint("problem", __name__)
|
|
|
|
|
|
|
|
@blueprint.route("/add", methods=["POST"])
|
|
|
|
@admins_only
|
|
|
|
@api_wrapper
|
|
|
|
def problem_add():
|
|
|
|
name = request.form["name"]
|
2016-01-02 04:18:40 +00:00
|
|
|
category = request.form["category"]
|
2015-12-31 02:56:00 +00:00
|
|
|
description = request.form["description"]
|
|
|
|
hint = request.form["hint"]
|
|
|
|
flag = request.form["flag"]
|
|
|
|
value = request.form["value"]
|
|
|
|
|
|
|
|
name_exists = Problems.query.filter_by(name=name).first()
|
|
|
|
if name_exists:
|
|
|
|
return { "success":0, "message": "Problem name already taken." }
|
|
|
|
|
2016-01-02 04:18:40 +00:00
|
|
|
problem = Problems(name, category, description, hint, flag, value)
|
2015-12-31 02:56:00 +00:00
|
|
|
db.session.add(problem)
|
2016-01-02 22:44:32 +00:00
|
|
|
db.session.commit()
|
2016-01-02 20:45:05 +00:00
|
|
|
|
2016-01-02 22:44:32 +00:00
|
|
|
files = request.files.getlist("files[]")
|
2016-01-02 20:45:05 +00:00
|
|
|
for _file in files:
|
|
|
|
filename = secure_filename(_file.filename)
|
|
|
|
|
|
|
|
if len(filename) == 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
folder = problem.name.replace(" ", "-")
|
2016-01-02 22:44:32 +00:00
|
|
|
folder_path = os.path.join(os.path.normpath(app.config["UPLOAD_FOLDER"]), folder)
|
|
|
|
if not os.path.exists(folder_path):
|
2016-01-02 20:45:05 +00:00
|
|
|
os.makedirs(folder_path)
|
|
|
|
|
2016-01-02 22:44:32 +00:00
|
|
|
file_path = os.path.join(folder_path, filename)
|
2016-01-02 20:45:05 +00:00
|
|
|
_file.save(file_path)
|
|
|
|
db_file = Files(problem.pid, file_path)
|
|
|
|
db.session.add(db_file)
|
|
|
|
|
2015-12-31 02:56:00 +00:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return { "success": 1, "message": "Success!" }
|
|
|
|
|
|
|
|
@blueprint.route("/delete", methods=["POST"])
|
|
|
|
@admins_only
|
|
|
|
@api_wrapper
|
|
|
|
def problem_delete():
|
|
|
|
pid = request.form["pid"]
|
|
|
|
problem = Problems.query.filter_by(pid=pid).first()
|
|
|
|
if problem:
|
|
|
|
Solves.query.filter_by(pid=pid).delete()
|
|
|
|
Challenges.query.filter_by(pid=pid).delete()
|
|
|
|
db.session.commit()
|
|
|
|
return { "success": 1, "message": "Success!" }
|
|
|
|
return { "success": 0, "message": "Problem does not exist!" }
|
|
|
|
|
|
|
|
@blueprint.route("/update", methods=["POST"])
|
|
|
|
@admins_only
|
|
|
|
@api_wrapper
|
|
|
|
def problem_update():
|
|
|
|
pid = request.form["pid"]
|
|
|
|
name = request.form["name"]
|
|
|
|
description = request.form["description"]
|
|
|
|
hint = request.form["hint"]
|
|
|
|
flag = request.form["flag"]
|
|
|
|
disabled = request.form["disabled"]
|
|
|
|
value = request.form["value"]
|
|
|
|
|
|
|
|
problem = Problems.query.filter_by(pid=pid).first()
|
|
|
|
if problem:
|
|
|
|
problem.name = name
|
|
|
|
problem.description = description
|
|
|
|
problem.hint = hint
|
|
|
|
problem.flag = flag
|
|
|
|
problem.disabled = disabled
|
|
|
|
problem.value = value
|
|
|
|
|
|
|
|
db.session.add(problem)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return { "success": 1, "message": "Success!" }
|
|
|
|
return { "success": 0, "message": "Problem does not exist!" }
|
2016-01-02 05:15:29 +00:00
|
|
|
|
|
|
|
@blueprint.route("/submit", methods=["POST"])
|
|
|
|
@api_wrapper
|
2016-01-02 19:30:42 +00:00
|
|
|
@login_required
|
2016-01-02 05:15:29 +00:00
|
|
|
def problem_submit():
|
|
|
|
pid = request.form["pid"]
|
|
|
|
flag = request.form["flag"]
|
|
|
|
tid = session["tid"]
|
|
|
|
|
|
|
|
problem = Problems.query.filter_by(pid=pid).first()
|
|
|
|
team = Teams.query.filter_by(tid=tid).first()
|
|
|
|
if problem:
|
|
|
|
if flag == problem.flag:
|
|
|
|
solve = Solves(pid, tid)
|
|
|
|
team.score += problem.value
|
|
|
|
problem.solves += 1
|
|
|
|
db.session.add(solve)
|
|
|
|
db.session.add(team)
|
|
|
|
db.session.add(problem)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
logger.log("submissions.log", logger.WARNING, "%s has solved %s by submitting %s" % (team.name, problem.name, flag))
|
|
|
|
return { "success": 1, "message": "Correct!" }
|
|
|
|
|
|
|
|
else:
|
|
|
|
logger.log("submissions.log", logger.WARNING, "%s has incorrectly submitted %s to %s" % (team.name, flag, problem.name))
|
|
|
|
return { "success": 0, "message": "Incorrect." }
|
|
|
|
|
|
|
|
else:
|
|
|
|
return { "success": 0, "message": "Problem does not exist!" }
|