Skip to content

Commit 0470bfe

Browse files
committed
Render problem list stats with ajax
1 parent caf599b commit 0470bfe

File tree

2 files changed

+92
-87
lines changed

2 files changed

+92
-87
lines changed

judge/views/submission.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
import os.path
32
from operator import attrgetter
43

@@ -455,15 +454,7 @@ def get_context_data(self, **kwargs):
455454
context["selected_languages"] = self.selected_languages_key
456455
context["all_statuses"] = self.get_searchable_status_codes()
457456
context["selected_statuses"] = self.selected_statuses
458-
459-
if not self.in_hidden_subtasks_contest():
460-
context["results_json"] = mark_safe(json.dumps(self.get_result_data()))
461-
context["results_colors_json"] = mark_safe(
462-
json.dumps(settings.DMOJ_STATS_SUBMISSION_RESULT_COLORS)
463-
)
464-
else:
465-
context["results_json"] = None
466-
457+
context["can_show_result_data"] = not self.in_hidden_subtasks_contest()
467458
context["page_suffix"] = suffix = (
468459
("?" + self.request.GET.urlencode()) if self.request.GET else ""
469460
)
@@ -509,7 +500,15 @@ def get(self, request, *args, **kwargs):
509500
self.include_frozen = True
510501

511502
if "results" in request.GET:
512-
return JsonResponse(self.get_result_data())
503+
response = {}
504+
if not self.in_hidden_subtasks_contest():
505+
response["results_json"] = self.get_result_data()
506+
response[
507+
"results_colors_json"
508+
] = settings.DMOJ_STATS_SUBMISSION_RESULT_COLORS
509+
else:
510+
response["results_json"] = None
511+
return JsonResponse(response)
513512

514513
return super(SubmissionsListBase, self).get(request, *args, **kwargs)
515514

templates/submission/list.html

Lines changed: 82 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
window.show_problem = 0;
2727
{% endif %}
2828
{% endif %}
29-
{% if results_json %}
30-
window.results_json = {{ results_json }};
31-
{% endif %}
3229
</script>
3330

3431
{% compress js %}
@@ -52,6 +49,47 @@
5249

5350
<script src="{{ static('libs/chart.js/Chart.js') }}"></script>
5451
<script type="text/javascript">
52+
var chart = null;
53+
54+
function stats_graph(raw_data) {
55+
var colors = window.results_colors_json;
56+
57+
var ctx = $('#status-graph').find('canvas')[0].getContext('2d');
58+
var font = $('body').css('font-family');
59+
if (chart !== null) {
60+
chart.destroy();
61+
}
62+
chart = new Chart(ctx, {
63+
type: 'pie',
64+
data: {
65+
datasets: [{
66+
data: raw_data.categories.map(function(entry) {
67+
return entry.count;
68+
}),
69+
backgroundColor: raw_data.categories.map(function(entry) {
70+
return colors[entry.code];
71+
}),
72+
}],
73+
labels: raw_data.categories.map(function(entry) {
74+
return entry.name;
75+
}),
76+
},
77+
options: {
78+
animation: false,
79+
scaleFontFamily: font,
80+
tooltips: {
81+
titleFontFamily: font,
82+
bodyFontFamily: font,
83+
},
84+
legend: {
85+
display: false,
86+
},
87+
},
88+
});
89+
90+
$('#total-submission-count').text(raw_data.total);
91+
}
92+
5593
$(function () {
5694
var info_float = $('.info-float');
5795
var container = $('#content-right');
@@ -90,48 +128,16 @@
90128
});
91129

92130
// Draw the statistics graph.
93-
{% if results_json %}
131+
{% if can_show_result_data %}
94132
$(function () {
95-
var chart = null;
96-
function stats_graph(raw_data) {
97-
var colors = {{ results_colors_json }};
98-
99-
var ctx = $('#status-graph').find('canvas')[0].getContext('2d');
100-
var font = $('body').css('font-family');
101-
if (chart !== null) {
102-
chart.destroy();
103-
}
104-
chart = new Chart(ctx, {
105-
type: 'pie',
106-
data: {
107-
datasets: [{
108-
data: raw_data.categories.map(function(entry) {
109-
return entry.count;
110-
}),
111-
backgroundColor: raw_data.categories.map(function(entry) {
112-
return colors[entry.code];
113-
}),
114-
}],
115-
labels: raw_data.categories.map(function(entry) {
116-
return entry.name;
117-
}),
118-
},
119-
options: {
120-
animation: false,
121-
scaleFontFamily: font,
122-
tooltips: {
123-
titleFontFamily: font,
124-
bodyFontFamily: font,
125-
},
126-
legend: {
127-
display: false,
128-
},
129-
},
130-
});
131-
132-
$('#total-submission-count').text(raw_data.total);
133-
}
134-
stats_graph(window.results_json);
133+
let currentUrl = window.location.href;
134+
let separator = currentUrl.includes('?') ? '&' : '?';
135+
let newUrl = currentUrl + separator + 'results=1';
136+
$.get(newUrl, function(data) {
137+
window.results_json = data.results_json;
138+
window.results_colors_json = data.results_colors_json;
139+
stats_graph(window.results_json);
140+
});
135141
});
136142
{% endif %}
137143
</script>
@@ -320,37 +326,37 @@ <h2>{{content_title}}</h2>
320326
{% endblock %}
321327

322328
{% block right_sidebar %}
323-
{% if results_json %}
324-
<div class="right-sidebar">
325-
<div class="submission">
326-
<div class="sidebox">
327-
<h3 class="colored-text"><i class="fa fa-search"></i>{{ _('Filter submissions') }}</h3>
328-
<div class="sidebox-content">
329-
<form id="filter-form" name="form" action="" method="get">
330-
<div class="filter-form-group">
331-
<label class="bold-text margin-label" for="status"><i class="non-italics" >{{ _('Status') }}</i></label>
332-
<select id="status" name="status" multiple>
333-
{% for id, name in all_statuses %}
334-
<option {% if id in selected_statuses %}selected{% endif %}
335-
value="{{ id }}">{{ name }}</option>
336-
{% endfor %}
337-
</select>
338-
</div>
339-
<div class="filter-form-group">
340-
<label class="bold-text margin-label" for="language"><i class="non-italics">{{ _('Language') }}</i></label>
341-
<select id="language" name="language" multiple>
342-
{% for code, name in all_languages %}
343-
<option {% if code in selected_languages %}selected{% endif %}
344-
value="{{ code }}">{{ name }}</option>
345-
{% endfor %}
346-
</select>
347-
</div>
348-
<div class="form-submit-group" >
349-
<a id="go" onclick="form.submit()" class="button small">{{ _('Go') }}</a>
350-
</div>
351-
</form>
352-
</div>
329+
<div class="right-sidebar">
330+
<div class="submission">
331+
<div class="sidebox">
332+
<h3 class="colored-text"><i class="fa fa-search"></i>{{ _('Filter submissions') }}</h3>
333+
<div class="sidebox-content">
334+
<form id="filter-form" name="form" action="" method="get">
335+
<div class="filter-form-group">
336+
<label class="bold-text margin-label" for="status"><i class="non-italics" >{{ _('Status') }}</i></label>
337+
<select id="status" name="status" multiple>
338+
{% for id, name in all_statuses %}
339+
<option {% if id in selected_statuses %}selected{% endif %}
340+
value="{{ id }}">{{ name }}</option>
341+
{% endfor %}
342+
</select>
343+
</div>
344+
<div class="filter-form-group">
345+
<label class="bold-text margin-label" for="language"><i class="non-italics">{{ _('Language') }}</i></label>
346+
<select id="language" name="language" multiple>
347+
{% for code, name in all_languages %}
348+
<option {% if code in selected_languages %}selected{% endif %}
349+
value="{{ code }}">{{ name }}</option>
350+
{% endfor %}
351+
</select>
352+
</div>
353+
<div class="form-submit-group" >
354+
<a id="go" onclick="form.submit()" class="button small">{{ _('Go') }}</a>
355+
</div>
356+
</form>
353357
</div>
358+
</div>
359+
{% if can_show_result_data %}
354360
<div class="sidebox">
355361
<div id="statistics-table">
356362
<h3 class="colored-text"><i class="fa fa-pie-chart"></i>{{ _('Statistics') }}</h3>
@@ -364,9 +370,9 @@ <h3 class="colored-text"><i class="fa fa-pie-chart"></i>{{ _('Statistics') }}</h
364370
</div>
365371
</div>
366372
</div>
367-
</div>
373+
{% endif %}
368374
</div>
369-
{% endif %}
375+
</div>
370376
{% endblock %}
371377

372378
{% block left_sidebar %}

0 commit comments

Comments
 (0)