From 6df67c0e76804b18984e0bf85329ea7325f9cc4c Mon Sep 17 00:00:00 2001 From: James Wang Date: Thu, 7 Apr 2016 18:01:17 -0400 Subject: [PATCH] Add basic functionality for updating/deleting problems --- server/api/admin.py | 7 +++--- server/api/models.py | 6 ++--- server/api/problem.py | 12 ++++------ web/js/admin.js | 45 +++++++++++++++++++++++++++++++++++ web/js/easyctf.js | 2 +- web/pages/admin/problems.html | 26 ++++++++++++++++---- 6 files changed, 79 insertions(+), 19 deletions(-) diff --git a/server/api/admin.py b/server/api/admin.py index 14bc52a..6c172a7 100644 --- a/server/api/admin.py +++ b/server/api/admin.py @@ -16,13 +16,14 @@ def problem_data(): for problem in problems: problems_return.append({ "pid": problem.pid, - "title": problem.title, + "name": problem.name, "category": problem.category, "description": problem.description, "hint": problem.hint, "value": problem.value, "threshold": problem.threshold, - "weightmap": problem.weightmap + "weightmap": problem.weightmap, + "flag": problem.flag }) problems_return.sort(key=lambda prob: prob["value"]) return { "success": 1, "problems": problems_return } @@ -42,4 +43,4 @@ ProblemSubmissionSchema = Schema({ ([str, Length(min=4, max=64)], "The title should be between 4 and 64 characters long."), ), }, extra=True) -""" \ No newline at end of file +""" diff --git a/server/api/models.py b/server/api/models.py index 0a5b34d..98abf5e 100644 --- a/server/api/models.py +++ b/server/api/models.py @@ -133,7 +133,7 @@ class Teams(db.Model): class Problems(db.Model): pid = db.Column(db.String(32), primary_key=True, autoincrement=False) - title = db.Column(db.String(128)) + name = db.Column(db.String(128)) category = db.Column(db.String(128)) flag = db.Column(db.String(128)) description = db.Column(db.Text) @@ -144,9 +144,9 @@ class Problems(db.Model): threshold = db.Column(db.Integer) weightmap = db.Column(db.PickleType) - def __init__(self, pid, title, category, description, flag, value, hint="", autogen=False, bonus=0, threshold=0, weightmap={}): + def __init__(self, pid, name, category, description, flag, value, hint="", autogen=False, bonus=0, threshold=0, weightmap={}): self.pid = pid - self.title = title + self.name = name self.category = category self.description = description self.flag = flag diff --git a/server/api/problem.py b/server/api/problem.py index 338fb7c..d45c8fe 100644 --- a/server/api/problem.py +++ b/server/api/problem.py @@ -26,7 +26,7 @@ def problem_add(): while Problems.query.filter_by(pid=pid).first(): pid = utils.generate_string() - name_exists = Problems.query.filter_by(title=name).first() + name_exists = Problems.query.filter_by(name=name).first() if name_exists: raise WebException("Problem name already taken.") @@ -74,7 +74,6 @@ def problem_update(): description = request.form["description"] hint = request.form["hint"] flag = request.form["flag"] - disabled = request.form.get("disabled", 0) value = request.form["value"] problem = Problems.query.filter_by(pid=pid).first() @@ -84,7 +83,6 @@ def problem_update(): problem.description = description problem.hint = hint problem.flag = flag - problem.disabled = disabled problem.value = value db.session.add(problem) @@ -146,7 +144,7 @@ def insert_problem(data, force=False): else: raise InternalException("Problem already exists.") - insert = Problems(data["pid"], data["title"], data["category"], data["description"], data["value"]) + insert = Problems(data["pid"], data["name"], data["category"], data["description"], data["value"]) if "hint" in data: insert.hint = data["hint"] if "autogen" in data: insert.autogen = data["autogen"] if "bonus" in data: insert.bonus = data["bonus"] @@ -157,10 +155,10 @@ def insert_problem(data, force=False): return True -def get_problem(title=None, pid=None): +def get_problem(name=None, pid=None): match = {} - if title != None: - match.update({ "title": title }) + if name != None: + match.update({ "name": name }) elif pid != None: match.update({ "pid": pid }) with app.app_context(): diff --git a/web/js/admin.js b/web/js/admin.js index 5874aa1..b3640ef 100644 --- a/web/js/admin.js +++ b/web/js/admin.js @@ -24,4 +24,49 @@ var create_problem = function() { $(input).removeAttr("disabled"); }); }); +}; + +var update_problem = function(form_id) { + var input = "#" + form_id + " input"; + var data = $("#" + form_id).serializeObject(); + pid = data["pid"]; + $(input).attr("disabled", "disabled"); + api_call("POST", "/api/problem/update", data, function(result) { + if (result["success"] == 1) { + display_message(pid + "_status", "success", result["message"], function() { + $(input).removeAttr("disabled"); + }); + } else { + display_message(pid + "_status", "danger", result["message"], function() { + $(input).removeAttr("disabled"); + }); + } + }, function(jqXHR, status, error) { + var result = jqXHR["responseText"]; + display_message(pid + "_status", "danger", "Error " + jqXHR["status"] + ": " + result["message"], function() { + $(input).removeAttr("disabled"); + }); + }); +}; + +var delete_problem = function(form_id) { + var input = "#" + form_id + " input"; + var pid = form_id.split("_")[1]; + $(input).attr("disabled", "disabled"); + api_call("POST", "/api/problem/delete", {"pid": pid}, function(result) { + if (result["success"] == 1) { + display_message(pid + "_status", "success", result["message"], function() { + $(input).removeAttr("disabled"); + }); + } else { + display_message(pid + "_status", "danger", result["message"], function() { + $(input).removeAttr("disabled"); + }); + } + }, function(jqXHR, status, error) { + var result = jqXHR["responseText"]; + display_message(pid + "_status", "danger", "Error " + jqXHR["status"] + ": " + result["message"], function() { + $(input).removeAttr("disabled"); + }); + }); } diff --git a/web/js/easyctf.js b/web/js/easyctf.js index de1d7e4..0f290c9 100644 --- a/web/js/easyctf.js +++ b/web/js/easyctf.js @@ -420,4 +420,4 @@ var remove_profile_picture = function() { location.reload(true); } }); -}; \ No newline at end of file +}; diff --git a/web/pages/admin/problems.html b/web/pages/admin/problems.html index 992d77c..ec4d7dc 100644 --- a/web/pages/admin/problems.html +++ b/web/pages/admin/problems.html @@ -12,11 +12,9 @@ } -
-
-