Skip to content

Commit

Permalink
Fixed a number of bugs regarding hints. Fixes #130
Browse files Browse the repository at this point in the history
  • Loading branch information
dlareau committed May 10, 2020
1 parent ccf62e0 commit 7d68631
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 58 deletions.
35 changes: 17 additions & 18 deletions huntserver/staff_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,14 +568,7 @@ def staff_hints_text(request):
h.last_modified_time = timezone.now()
h.save()
hints = [h]

elif request.is_ajax():
last_date = datetime.strptime(request.GET.get("last_date"), DT_FORMAT)
last_date = last_date.replace(tzinfo=tz.gettz('UTC'))
hints = Hint.objects.filter(last_modified_time__gt=last_date)

else:
page_num = request.GET.get("page_num")
team_id = request.GET.get("team_id")
hint_status = request.GET.get("hint_status")
puzzle_id = request.GET.get("puzzle_id")
Expand All @@ -596,14 +589,20 @@ def staff_hints_text(request):
elif(hint_status == "unanswered"):
hints = hints.filter(response="")
arg_string = arg_string + ("&hint_status=%s" % hint_status)
hints = hints.select_related('team', 'puzzle').order_by('-pk')
pages = Paginator(hints, 10)
try:
hints = pages.page(page_num)
except PageNotAnInteger:
hints = pages.page(1)
except EmptyPage:
hints = pages.page(pages.num_pages)
if(request.is_ajax()):
last_date = datetime.strptime(request.GET.get("last_date"), DT_FORMAT)
last_date = last_date.replace(tzinfo=tz.gettz('UTC'))
hints = hints.filter(last_modified_time__gt=last_date)
else: # Normal get request
page_num = request.GET.get("page_num")
hints = hints.select_related('team', 'puzzle').order_by('-pk')
pages = Paginator(hints, 10)
try:
hints = pages.page(page_num)
except PageNotAnInteger:
hints = pages.page(1)
except EmptyPage:
hints = pages.page(pages.num_pages)

try:
time_query = Hint.objects.latest('last_modified_time').last_modified_time
Expand All @@ -613,17 +612,17 @@ def staff_hints_text(request):

hint_list = []
for hint in hints:
form = HintResponseForm(initial={"response": hint.response, "hint_id": hint.pk})
hint_list.append(render_to_string('hint_row.html', {'hint': hint, "response_form": form},
hint_list.append(render_to_string('hint_row.html', {'hint': hint, 'staff_side': True},
request=request))

if request.is_ajax() or request.method == 'POST':
context = {'hint_list': hint_list, 'last_date': last_date}
return HttpResponse(json.dumps(context))
else:
form = HintResponseForm()
context = {'page_info': hints, 'hint_list': hint_list, 'arg_string': arg_string,
'last_date': last_date, 'hunt': hunt, 'puzzle_id': puzzle_id, 'team_id': team_id,
'hint_status': hint_status}
'hint_status': hint_status, "response_form": form}
return render(request, 'staff_hints.html', add_apps_to_context(context, request))


Expand Down
20 changes: 20 additions & 0 deletions huntserver/static/huntserver/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,24 @@ select {
.download-btn {
padding: 3px 12px 1px 12px;
margin-top: -2px;
}

.vertical-alignment-helper {
display:table;
height: 100%;
width: 100%;
pointer-events:none;
}
.vertical-align-center {
/* To center vertically */
display: table-cell;
vertical-align: middle;
pointer-events:none;
}
.modal-content {
width:inherit;
max-width:inherit;
height:inherit;
margin: 0 auto;
pointer-events: all;
}
33 changes: 21 additions & 12 deletions huntserver/static/huntserver/staff_hints.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ $(document).ready(function() {
e.preventDefault();
team = $(this).attr('data-team');
value = $(this).attr('data-value');
$.post("/staff/hints/control/",
{action:'update', 'team_pk': team, value: value, csrfmiddlewaretoken: csrf_token},
$.post("/staff/hints/control/",
{action:'update', 'team_pk': team, value: value, csrfmiddlewaretoken: csrf_token},
function( data ) {
var response = JSON.parse(data);
for (var i = 0; i < response.length; i++) {
Expand All @@ -43,10 +43,11 @@ $(document).ready(function() {

var get_posts = function() {
// Hint texts:
data_str = "&last_date=" + last_date;
$.ajax({
type: 'get',
url: "/staff/hints/",
data: {last_date: last_date},
data: $("#filter_form").serialize() + data_str,
success: function (response) {
var response = JSON.parse(response);
messages = response.hint_list;
Expand All @@ -57,7 +58,7 @@ $(document).ready(function() {
if ($('tr[data-id=' + pk + ']').length == 0) {
submission.prependTo("#sub_table");
if($('#sub_table tr').length >= 10){
$('#sub_table tr:last').remove();
$('#sub_table table:last').parent().parent().remove();
}
} else {
$('tr[data-id=' + pk + ']').replaceWith(submission);
Expand Down Expand Up @@ -91,29 +92,37 @@ $(document).ready(function() {

function formListener(e) {
e.preventDefault();
// This sucks, I should be more precise
old_row = $(this).parent().parent().parent().parent().parent().parent().parent();
var hint_id = $(this).find("#id_hint_id").val()
$.ajax({
url : $(this).attr('action') || window.location.pathname,
type: "POST",
data: $(this).serialize(),
success: function (response) {
response = JSON.parse(response);
old_row.replaceWith($(response.hint_list[0]));
$('.sub_form').on('submit', formListener);
$("[data-id=" + hint_id + "]").replaceWith($(response.hint_list[0]))
last_date = response.last_date;
},
error: function (jXHR, textStatus, errorThrown) {
console.log(jXHR);
}
});
$('#formModal').modal('hide');
}

/* open a text box for submitting an email */
$('#formModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var modal = $(this);
var hint_request = button.parent().parent().parent();
var title = hint_request.find(".hint-title");
var response = hint_request.find(".hint-response");
var outer_row = hint_request.parent().parent().parent();
modal.find('.modal-title').html(title.html());
modal.find('.modal-body #id_response').val(response.text());
modal.find('.modal-body #id_hint_id').val(outer_row.data("id"));
})

$(document).delegate('.fix_link', 'click', function() {
$(this).siblings('form').show();
$(this).siblings('p').hide();
$(this).hide();
$('#formModal').modal('show');
return false;
});
});
46 changes: 19 additions & 27 deletions huntserver/templates/hint_row.html
Original file line number Diff line number Diff line change
@@ -1,49 +1,41 @@
{% load crispy_forms_tags %}
<tr data-id='{{hint.pk}}'>
<td>
<table class="table table-bordered hint_table">
<tr>
<td width="70px">Request</td>
<td width="100px">{{ hint.request_time|time:"h:i a" }}</td>
<td>
<b>
Puzzle: <a href="./?team_id={{request.GET.team_id}}&puzzle_id={{hint.puzzle.pk}}&hint_status={{request.GET.hint_status}}">
{{hint.puzzle.puzzle_name}}
</a>
| Team: <a href="./?team_id={{hint.team.pk}}&puzzle_id={{request.GET.puzzle_id}}&hint_status={{request.GET.hint_status}}">
{{hint.team.short_name}}
</a>
</b>
<br>
<div class="hint-title">
<b>
Puzzle: <a href="./?team_id={{request.GET.team_id}}&puzzle_id={{hint.puzzle.pk}}&hint_status={{request.GET.hint_status}}">
{{hint.puzzle.puzzle_name}}
</a>
| Team: <a href="./?team_id={{hint.team.pk}}&puzzle_id={{request.GET.puzzle_id}}&hint_status={{request.GET.hint_status}}">
{{hint.team.short_name}}
</a>
</b>
</div>
{{ hint.request }}
</td>
</tr>
<tr>
<td width="70px">Response</td>
{% if response_form %}
{% if staff_side %}
{% if hint.response %}
<td width="100px">{{ hint.response_time|time:"h:i a" }}</td>
<td>
<div>
<p>{{ hint.response }}</p>
<a href='#' class='fix_link'>Edit Response</a>
<form class='sub_form' action="/staff/hints/" method="post" style="display:none">
{% csrf_token %}
{{ response_form|crispy }}
<input class="btn btn-default" type='submit' value='Submit'/>
</form>
</div>
<div class="hint-response">{{ hint.response }}</div>
<button type="button" class="btn btn-default btn-xs" data-toggle="modal" data-target="#formModal">
Edit response
</button>
</td>
{% else %}
<td width="100px"></td>
<td>
<div>
<form class='sub_form' action="/staff/hints/" method="post">
{% csrf_token %}
{{ response_form|crispy }}
<input class="btn btn-default" type='submit' value='Submit'/>
</form>
</div>
<div class="hint-response"></div>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#formModal">
Respond to hint
</button>
</td>
{% endif %}
{% else %}
Expand Down
24 changes: 23 additions & 1 deletion huntserver/templates/staff_hints.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% extends "staff_base.html" %}
{% block title %}Hint Management{% endblock title %}
{% load crispy_forms_tags %}

{% block includes %}
<script src="{{ STATIC_URL }}js.cookie.js"></script>
Expand Down Expand Up @@ -42,7 +43,7 @@ <h1>Hint Requests</h1>
<button style="float: left; margin-right: 20px;" class="btn btn-default" type="button" data-toggle="collapse" data-target="#hint_counts" aria-expanded="false" aria-controls="collapseExample">
Show/Hide hint counts
</button>
<form method="GET" action="" class="form-inline">
<form id="filter_form" method="GET" action="" class="form-inline">
<select name="team_id" style="width: 200px;" class="form-control">
<option value="">Filter by team</option>
{% for team in hunt.team_set.all|dictsort:"team_name.lower" %}
Expand Down Expand Up @@ -75,6 +76,27 @@ <h1>Hint Requests</h1>
You are viewing filtered hints. <u><a href=".">Click here to remove filtering.</a></u>
</div>
{% endif %}

<div class="modal fade" id="formModal" tabindex="-1" role="dialog" aria-labelledby="formModalLabel">
<div class="vertical-alignment-helper">
<div class="modal-dialog vertical-align-center" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="formModalLabel">Respond to Hint</h4>
</div>
<div class="modal-body">
<form class='sub_form' action="/staff/hints/" method="post">
{% csrf_token %}
{{ response_form|crispy }}
<input class="btn btn-default" type='submit' value='Submit'/>
</form>
</div>
</div>
</div>
</div>
</div>

<table id='hints' class="table table-condensed" style="margin-top: 10px;">
<tbody id='sub_table'>
{% for hint in hint_list %}
Expand Down

0 comments on commit 7d68631

Please sign in to comment.