fixed api submission

master
James Sigurðarson 2016-08-04 22:13:31 +00:00
parent 4287958bcd
commit 1e98f823b8
4 changed files with 41 additions and 30 deletions

View File

@ -29,21 +29,29 @@ def get_challenges():
return d
def get_solves():
def get_solve_counts():
# TODO: optimize
d = dict()
for k in Challenge.select(Challenge.id):
s = g.redis.hget("solves", k.id)
if s:
d[k.id] = int(s.decode())
else:
d[k.id] = -1
d[k.id] = get_solve_count(k.id)
return d
def get_solve_count(chall_id):
s = g.redis.hget("solves", chall_id)
if s is not None:
return int(s.decode())
else:
return -1
def get_challenge(id):
def get_challenge(id=None, alias=None):
try:
return Challenge.get(Challenge.id == id)
if id is not None:
return Challenge.get(Challenge.id == id)
elif alias is not None:
return Challenge.get(Challenge.alias == alias)
else:
raise ValueError("Invalid argument")
except Challenge.DoesNotExist:
raise ValidationError("Challenge does not exist!")

View File

@ -8,7 +8,7 @@ import config
api = Blueprint("api", __name__, url_prefix="/api")
@api.route("/submit/<int:challenge_id>.json", methods=["POST"])
@api.route("/submit/<challenge_id>.json", methods=["POST"])
@decorators.must_be_allowed_to("solve challenges")
@decorators.must_be_allowed_to("view challenges")
@decorators.competition_running_required
@ -16,14 +16,15 @@ api = Blueprint("api", __name__, url_prefix="/api")
@ratelimit.ratelimit(limit=10, per=120, over_limit=ratelimit.on_over_api_limit)
def submit_api(challenge_id):
try:
chall = challenge.get_challenge(challenge_id)
chall = challenge.get_challenge(alias=challenge_id)
except exceptions.ValidationError as e:
return jsonify(dict(code=1001, message=str(e)))
flag = request.form["flag"]
try:
challenge.submit_flag(chall, g.user, g.team, flag)
return jsonify(dict(code=0, message="Success!"))
solves = challenge.get_solve_count(chall.id)
return jsonify(dict(code=0, message="Success!", solves=solves))
except exceptions.ValidationError as e:
return jsonify(dict(code=1001, message=str(e)))

View File

@ -14,23 +14,22 @@ challenges = Blueprint("challenges", __name__, template_folder="../templates/cha
@decorators.confirmed_email_required
def index():
stages = challenge.get_stages()
first_stage = {a.alias: True for a in challenge.get_stage_challenges(stages[0].id)}
print(first_stage)
challs = challenge.get_challenges()
solved = challenge.get_solved(g.team)
solves = challenge.get_solves()
solves = challenge.get_solve_counts()
categories = challenge.get_categories()
first_stage = {chall.alias: True for chall in challs[stages[0].id]}
return render_template("challenges.html", stages=stages, first_stage=first_stage, challenges=challs, solved=solved, categories=categories, solves=solves)
@challenges.route('/challenges/<int:challenge_id>/solves/')
@challenges.route('/challenges/<challenge_id>/solves/')
@decorators.must_be_allowed_to("view challenge solves")
@decorators.must_be_allowed_to("view challenges")
@decorators.competition_running_required
@decorators.confirmed_email_required
def show_solves(challenge_id):
try:
chall = challenge.get_challenge(challenge_id)
chall = challenge.get_challenge(alias=challenge_id)
except exceptions.ValidationError as e:
flash(str(e))
return redirect(url_for(".index"))
@ -38,7 +37,7 @@ def show_solves(challenge_id):
return render_template("challenge_solves.html", challenge=chall, solves=solves)
@challenges.route('/submit/<int:challenge_id>/', methods=["POST"])
@challenges.route('/submit/<challenge_id>/', methods=["POST"])
@decorators.must_be_allowed_to("solve challenges")
@decorators.must_be_allowed_to("view challenges")
@decorators.competition_running_required

View File

@ -32,14 +32,14 @@
<span class="right">
<span>{{ challenge.author }}</span>
<b>&middot;</b>
<span id="solves{{ challenge.id }}">{{ solves[challenge.id] }}</span> {% if solves[challenge.id] == 1 %}solve{% else %}solves{% endif %}
<span id="solves-{{ challenge.alias }}">{{ solves[challenge.id] }}</span> {% if solves[challenge.id] == 1 %}solve{% else %}solves{% endif %}
<b>&middot;</b>
{{ challenge.category }}
<b>&middot;</b>
{{ challenge.points }} pt
{% if challenge in solved %}
<i id="check{{ challenge.id }}" class="material-icons green-text right check-icon">done</i>
<i id="check-{{ challenge.alias }}" class="material-icons green-text right check-icon">done</i>
{% else %}
<div class="check-pad"></div>
{% endif %}
@ -48,19 +48,19 @@
<div class="collapsible-body">
<p>{{ challenge.description | safe }}
{% if challenge in solved %}
<br /><br /><strong>You've solved this challenge!</strong><br />
<a href="{{ url_for('.show_solves', challenge_id=challenge.id) }}">View solves</a>
<br /><br /><strong>Your team has solved this challenge!</strong><br />
<a href="{{ url_for('.show_solves', challenge_id=challenge.alias) }}">View solves</a>
</p>
{% else %}
<br /><br />
<a href="{{ url_for('.show_solves', challenge_id=challenge.id) }}">View solves</a>
<a href="{{ url_for('.show_solves', challenge_id=challenge.alias) }}">View solves</a>
</p>
<form class="flag-form" action="{{ url_for('.submit', challenge_id=challenge.id) }}" data-challengeid="{{ challenge.id }}" method="POST">
<form class="flag-form" action="{{ url_for('.submit', challenge_id=challenge.alias) }}" data-challenge="{{ challenge.alias }}" method="POST">
<div class="row no-bot">
<div class="col s12 m10">
<div class="input-field">
<input required id="flag{{ challenge.id }}" name="flag" type="text" />
<label for="flag{{ challenge.id }}">Flag</label>
<input required id="flag-{{ challenge.alias }}" name="flag" type="text" />
<label for="flag-{{ challenge.alias }}">Flag</label>
</div>
</div>
<div class="col s12 m2">
@ -141,16 +141,19 @@
$(header_selector).each(function(i, x){ $(x).hasClass("active") || $(x).click(); });
});
$("form").submit(function(e) {
var id = $(this).attr("data-challengeid");
api.makeCall("/submit/" + id + ".json", {flag: $("#flag" + id).val(), _csrf_token: "{{ csrf_token() }}"}, function(data) {
var id = $(this).data("challenge");
var flag = $("#flag-" + id).val();
console.log(id);
console.log(flag);
api.makeCall("/submit/" + id + ".json", {flag: flag, _csrf_token: "{{ csrf_token() }}"}, function(data) {
if(data.code) {
Materialize.toast(data.message, 4000);
}
else {
Materialize.toast("Flag accepted!", 4000);
$("#check" + id).show();
$("#header" + id).click();
$("#solves" + id).html(parseInt($("#solves" + id).html()) + 1);
$("#check-" + id).show();
$("#header-" + id).click();
$("#solves-" + id).html(data.solves);
}
});
return false;