diff --git a/server/api/problem.py b/server/api/problem.py index b2a3f75..d55a1f5 100644 --- a/server/api/problem.py +++ b/server/api/problem.py @@ -1,8 +1,10 @@ +import logger + from flask import Blueprint, session, request from flask import current_app as app -from models import db, Problems -from decorators import admins_only, api_wrapper +from models import db, Problems, Solves, Teams +from decorators import admins_only, api_wrapper, login_required blueprint = Blueprint("problem", __name__) @@ -67,3 +69,33 @@ def problem_update(): return { "success": 1, "message": "Success!" } return { "success": 0, "message": "Problem does not exist!" } + +@blueprint.route("/submit", methods=["POST"]) +@api_wrapper +@login_required +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!" } diff --git a/server/app.py b/server/app.py index a262935..e5a2378 100644 --- a/server/app.py +++ b/server/app.py @@ -11,7 +11,7 @@ app.config["SQLALCHEMY_DATABASE_URI"] = config.SQLALCHEMY_DATABASE_URI app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = config.SQLALCHEMY_TRACK_MODIFICATIONS with app.app_context(): - from api.models import db + from api.models import db, Teams, Problems, Solves, Users db.init_app(app) db.create_all() diff --git a/web/pages/register.html b/web/pages/register.html index 5970f8b..14ece2d 100644 --- a/web/pages/register.html +++ b/web/pages/register.html @@ -36,9 +36,9 @@