From e4392cf94e56bef5970abcee8dcf71c8ec8a416f Mon Sep 17 00:00:00 2001 From: James Wang Date: Sat, 2 Jan 2016 22:15:23 -0500 Subject: [PATCH] Implement problem updating for admins --- scripts/requirements.txt | 2 +- server/api/admin.py | 19 +++++++- server/api/decorators.py | 2 +- server/api/problem.py | 3 +- web/js/admin/problems.js | 95 ++++++++++++++++++++++++++++++---------- 5 files changed, 94 insertions(+), 27 deletions(-) diff --git a/scripts/requirements.txt b/scripts/requirements.txt index e0f3b60..ec2f247 100644 --- a/scripts/requirements.txt +++ b/scripts/requirements.txt @@ -3,4 +3,4 @@ mysql-python Flask-SQLAlchemy SQLAlchemy gunicorn -requests \ No newline at end of file +requests diff --git a/server/api/admin.py b/server/api/admin.py index 4c734c9..5152a88 100644 --- a/server/api/admin.py +++ b/server/api/admin.py @@ -1,4 +1,19 @@ -from flask import Blueprint -from decorators import api_wrapper +from flask import Blueprint, jsonify +from decorators import admins_only, api_wrapper, login_required +from models import db, Problems, Files blueprint = Blueprint("admin", __name__) + +@blueprint.route("/problem/data", methods=["POST"]) +#@api_wrapper # Disable atm due to json serialization issues: will fix +@admins_only +@login_required +def problem_data(): + problems = Problems.query.add_columns("pid", "name", "category", "description", "hint", "value", "solves", "disabled", "flag").order_by(Problems.value).all() + jason = [] + + for problem in problems: + problem_files = [ str(_file.location) for _file in Files.query.filter_by(pid=int(problem.pid)).all() ] + jason.append({"pid": problem[1], "name": problem[2] ,"category": problem[3], "description": problem[4], "hint": problem[5], "value": problem[6], "solves": problem[7], "disabled": problem[8], "flag": problem[9], "files": problem_files}) + + return jsonify(data=jason) diff --git a/server/api/decorators.py b/server/api/decorators.py index 27565ce..59715a0 100644 --- a/server/api/decorators.py +++ b/server/api/decorators.py @@ -19,7 +19,7 @@ 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.", str(error): traceback.format_exc() } + web_result = { "success": 0, "message": "Something went wrong! Please notify us about this immediately. %s: %s" % (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 index 6ee206a..983e444 100644 --- a/server/api/problem.py +++ b/server/api/problem.py @@ -70,10 +70,11 @@ def problem_delete(): def problem_update(): pid = request.form["pid"] name = request.form["name"] + category = request.form["category"] description = request.form["description"] hint = request.form["hint"] flag = request.form["flag"] - disabled = request.form["disabled"] + disabled = request.form.get("disabled", 0) value = request.form["value"] problem = Problems.query.filter_by(pid=pid).first() diff --git a/web/js/admin/problems.js b/web/js/admin/problems.js index ae53ad4..8f08677 100644 --- a/web/js/admin/problems.js +++ b/web/js/admin/problems.js @@ -1,44 +1,95 @@ function render_problems() { - $.post("/api/problem/data", { + $.post("/api/admin/problem/data", { }, function(data) { data = data["data"]; for (var i = 0; i < data.length; i++) { files = data[i]["files"]; + var checked = ""; + if (data[i]["disabled"]) { + checked = "checked"; + } problem = -`
-
-

` + data[i]["name"] + ` | ` + data[i]["category"] + `` + data[i]["value"] + ` points

-
-
-

` + data[i]["description"] + `

-
- - - - - +`
+
+ +
+
+
+ +
+
+ +
+
-
-
` +
+ +

+
+
+ +
+
+ +
+
+
+
+ + +
+
+
` + +

+ +
` $("#problems").append(problem); } + $("[name=update]").click(function(e) { + var problem = $(this).parents("form:first"); + var pid = $("input[name=pid]", problem).val(); + var name = $("input[name=name]", problem).val(); + var description = $("textarea[name=description]", problem).val(); + var hint = $("input[name=hint]", problem).val(); + var category = $("input[name=category]", problem).val(); + var value = $("input[name=value]", problem).val(); + var flag = $("input[name=flag]", problem).val(); + var disabled = $("input[name=disabled]", problem).prop("checked") ? 1 : 0; + update_problem(pid, name, category, description, hint, flag, disabled, value); + }); }); } -function show_hint(pid) { - $("#hint_" + pid).slideToggle(120, "swing"); +function update_problem (pid, name, category, description, hint, flag, disabled, value) { + $.post("/api/problem/update", { + pid: pid, + name: name, + category: category, + description: description, + hint: hint, + flag: flag, + disabled: disabled, + value: value + }, function(data) { + if (data.success == 1) { + display_message("status_" + pid, "success", data.message, function() {}); + } else { + display_message("status_" + pid, "danger", data.message, function() {}); + } + }) } -$(document).ready( render_problems() ); +$(function() { + render_problems(); +});