diff --git a/scripts/setup.sh b/scripts/setup.sh old mode 100644 new mode 100755 diff --git a/server/api/__init__.py b/server/api/__init__.py index cd35ad4..bcc3da4 100644 --- a/server/api/__init__.py +++ b/server/api/__init__.py @@ -1,5 +1,6 @@ import admin import logger import models +import problem import user import utils diff --git a/server/api/decorators.py b/server/api/decorators.py index 2e11a0d..546ff55 100644 --- a/server/api/decorators.py +++ b/server/api/decorators.py @@ -1,6 +1,11 @@ +import json +import traceback + from functools import wraps from flask import session +class WebException(Exception): pass + def login_required(f): @wraps(f) def decorated_function(*args, **kwargs): @@ -33,6 +38,6 @@ def api_wrapper(f): except Exception as error: response = 200 traceback.print_exc() - web_result = { "success": 0, "message": "Something went wrong! Please notify us about this immediately.", error: traceback.format_exc() } + web_result = { "success": 0, "message": "Something went wrong! Please notify us about this immediately.", str(error): traceback.format_exc() } return json.dumps(web_result), response, { "Content-Type": "application/json; charset=utf-8" } return wrapper diff --git a/server/api/problem.py b/server/api/problem.py new file mode 100644 index 0000000..638d6f5 --- /dev/null +++ b/server/api/problem.py @@ -0,0 +1,68 @@ +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 + +blueprint = Blueprint("problem", __name__) + +@blueprint.route("/add", methods=["POST"]) +@admins_only +@api_wrapper +def problem_add(): + name = request.form["name"] + 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." } + + problem = Problems(name, description, hint, flag, value) + db.session.add(problem) + 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!" } diff --git a/server/api/utils.py b/server/api/utils.py index 21a5c91..4b2de57 100644 --- a/server/api/utils.py +++ b/server/api/utils.py @@ -7,8 +7,6 @@ import traceback from functools import wraps from werkzeug.security import generate_password_hash, check_password_hash -class WebException(Exception): pass - def hash_password(s): return generate_password_hash(s) diff --git a/server/app.py b/server/app.py index b4ce5f3..a262935 100644 --- a/server/app.py +++ b/server/app.py @@ -7,17 +7,19 @@ import json app = Flask(__name__) +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 db.init_app(app) db.create_all() app.secret_key = config.SECRET_KEY -app.config["SQLALCHEMY_DATABASE_URI"] = config.SQLALCHEMY_DATABASE_URI -app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = config.SQLALCHEMY_TRACK_MODIFICATIONS app.register_blueprint(api.admin.blueprint, url_prefix="/api/admin") app.register_blueprint(api.user.blueprint, url_prefix="/api/user") +app.register_blueprint(api.problem.blueprint, url_prefix="/api/problem") api.logger.initialize_logs() @app.route("/api")