Add basic functionality for updating/deleting problems

This commit is contained in:
James Wang 2016-04-07 18:01:17 -04:00
parent c8efdd3df4
commit 6df67c0e76
No known key found for this signature in database
GPG key ID: 5B80C0B3F263CD5B
6 changed files with 79 additions and 19 deletions

View file

@ -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)
"""
"""

View file

@ -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

View file

@ -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():

View file

@ -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");
});
});
}

View file

@ -420,4 +420,4 @@ var remove_profile_picture = function() {
location.reload(true);
}
});
};
};

View file

@ -12,11 +12,9 @@
}
</style>
<div class="row">
<div class="tabbable">
<ul class="nav nav-pills nav-stacked col-md-3">
<div class="row"> <div class="tabbable"> <ul class="nav nav-pills nav-stacked col-md-3">
<li class="active"><a data-target="#new" data-toggle="tab">New</a></li>
<li ng-repeat="problem in problems"><a data-target="#problem_{{ problem['pid'] }}" data-toggle="tab">{{ problem["title"] }} ({{ problem["value"] }} points)</a></li>
<li ng-repeat="problem in problems"><a data-target="#problem_{{ problem['pid'] }}" data-toggle="tab">{{ problem["name"] }} ({{ problem["value"] }} points)</a></li>
</ul>
<div class="tab-content col-md-9">
<div class="tab-pane active" id="new">
@ -49,9 +47,27 @@
<div class="tab-pane" ng-repeat="problem in problems" id="problem_{{ problem['pid'] }}">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">{{ problem["title"] }}</h4>
<h4 class="panel-title">{{ problem["name"] }}</h4>
</div>
<div class="panel-body">
<div id="{{ problem['pid'] }}_status"/>
<form class="form-horizontal" onsubmit="update_problem(this.id); return false;" id="update_{{ problem['pid'] }}">
<input type="hidden" name="pid" value="{{ problem['pid'] }}"/>
<input class="form-control" name="name" value="{{ problem['name'] }}" required/>
<br>
<textarea class="form-control" name="description" required autocomplete="off">{{ problem["description"] }}</textarea>
<br>
<input class="form-control" name="category" value="{{ problem['category'] }}" required/>
<br>
<input class="form-control" name="hint" value="{{ problem['hint'] }}" required/>
<br>
<input class="form-control" type="number" name="value" value="{{ problem['value'] }}" required/>
<br>
<input class="form-control" name="flag" value="{{ problem['flag'] }}" required/>
<br>
<input type="submit" class="btn btn-success" value="Update"/>
<input onclick="delete_problem(this.closest('form').id); return false;" class="btn btn-danger" value="Delete"/>
</form>
</div>
</div>
</div>