Begin backend for handling problems
This commit is contained in:
parent
95aa94f965
commit
78617dab06
6 changed files with 79 additions and 5 deletions
0
scripts/setup.sh
Normal file → Executable file
0
scripts/setup.sh
Normal file → Executable file
|
@ -1,5 +1,6 @@
|
|||
import admin
|
||||
import logger
|
||||
import models
|
||||
import problem
|
||||
import user
|
||||
import utils
|
||||
|
|
|
@ -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
|
||||
|
|
68
server/api/problem.py
Normal file
68
server/api/problem.py
Normal file
|
@ -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!" }
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in a new issue