Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow org admin to download submissions during specified time #367

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dmoj/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ def paged_list_view(view, name):
path('/users/', organization.OrganizationUsers.as_view(), name='organization_users'),
path('/join', organization.JoinOrganization.as_view(), name='join_organization'),
path('/leave', organization.LeaveOrganization.as_view(), name='leave_organization'),
path('/get-submissions-data', organization.GetSubmissionsData.as_view(),
name='get_submissions_data'),
path('/edit', organization.EditOrganization.as_view(), name='edit_organization'),
path('/kick', organization.KickUserWidgetView.as_view(), name='organization_user_kick'),
path('/problems/', organization.ProblemListOrganization.as_view(), name='problem_list_organization'),
Expand Down
11 changes: 11 additions & 0 deletions judge/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,17 @@ class Meta:
})


class OrganizationGetSubmissionsDataForm(Form):
start_time = forms.DateField(widget=forms.DateInput(attrs={'type': 'date', 'required': True}), label=_('From'))
end_time = forms.DateField(widget=forms.DateInput(attrs={'type': 'date', 'required': True}), label=_('To'))

def get_data(self):
start_time = self.cleaned_data['start_time']
end_time = self.cleaned_data['end_time']

return start_time, end_time


class SocialAuthMixin:
def _has_social_auth(self, key):
return (getattr(settings, 'SOCIAL_AUTH_%s_KEY' % key, None) and
Expand Down
44 changes: 41 additions & 3 deletions judge/views/organization.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import csv
from functools import cached_property

from django import forms
Expand All @@ -10,7 +11,7 @@
from django.db.models.expressions import F, Value
from django.db.models.functions import Coalesce
from django.forms import Form, modelformset_factory
from django.http import Http404, HttpResponseRedirect
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.utils import timezone
Expand All @@ -20,9 +21,10 @@
from django.views.generic.detail import SingleObjectMixin, SingleObjectTemplateResponseMixin
from reversion import revisions

from judge.forms import OrganizationForm
from judge.forms import OrganizationForm, OrganizationGetSubmissionsDataForm
from judge.models import BlogPost, Comment, Contest, Language, Organization, OrganizationRequest, \
Problem, Profile
from judge.models.submission import Submission
from judge.tasks import on_new_problem
from judge.utils.infinite_paginator import InfinitePaginationMixin
from judge.utils.ranker import ranker
Expand All @@ -32,8 +34,9 @@
from judge.views.problem import ProblemCreate, ProblemList
from judge.views.submission import SubmissionsListBase


__all__ = ['OrganizationList', 'OrganizationHome', 'OrganizationUsers', 'OrganizationMembershipChange',
'JoinOrganization', 'LeaveOrganization', 'EditOrganization', 'RequestJoinOrganization',
'JoinOrganization', 'LeaveOrganization', 'GetSubmissionsData', 'EditOrganization', 'RequestJoinOrganization',
'OrganizationRequestDetail', 'OrganizationRequestView', 'OrganizationRequestLog',
'KickUserWidgetView']

Expand Down Expand Up @@ -217,6 +220,40 @@ def handle(self, request, org, profile):
profile.organizations.remove(org)


class GetSubmissionsData(LoginRequiredMixin, PublicOrganizationMixin, TitleMixin, SingleObjectMixin, FormView):
form_class = OrganizationGetSubmissionsDataForm
template_name = 'organization/get-submissions-data.html'
title = gettext_lazy('Get submissions data')
success_url = '.'

def form_valid(self, form):
if not self.organization.is_admin(self.request.profile):
raise PermissionDenied()
if form.cleaned_data['start_time'] > form.cleaned_data['end_time']:
form.add_error('start_time', _('Start time must be before end time.'))
return self.form_invalid(form)
return self.download(form)

def download(self, form):
start_time, end_time = form.get_data()
org = self.organization
submissions = Submission.objects.filter(judged_date__gte=start_time,
judged_date__lte=end_time,
problem__organizations=org).order_by('judged_date').only(
'judged_date', 'problem', 'user', 'result')
respone = HttpResponse(content_type='text/csv')
respone['Content-Disposition'] = 'attachment; filename="submissions.csv"'
respone.write(u'\ufeff'.encode('utf8'))

writer = csv.writer(respone)
writer.writerow(['judged_date', 'problem_id', 'user', 'result'])
for submission in submissions:
writer.writerow([submission.judged_date.ctime(), submission.problem.code,
submission.user.user.username, submission.result])

return respone


class OrganizationRequestForm(Form):
reason = forms.CharField(widget=forms.Textarea)

Expand Down Expand Up @@ -485,6 +522,7 @@ def get_context_data(self, **kwargs):
context['title'] = self.object.name
context['can_edit'] = self.can_edit_organization()
context['is_member'] = self.request.profile in self.object
context['form'] = OrganizationGetSubmissionsDataForm()

context['post_comment_counts'] = {
int(page[2:]): count for page, count in
Expand Down
72 changes: 72 additions & 0 deletions templates/organization/get-submissions-data.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{% extends "base.html" %}

{% block media %}
{{ form.media.css }}
<style>
.centered-form {
padding-top: 15px;
max-width: 460px;
width: fit-content;
}

.form-area {
display: block;
}

.form-field {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
margin-top: 5px;
}

.form-field div:first-child {
margin-right: 20px;
}

#submit-bar {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
flex-direction: row-reverse;
}

.aux-download-action {
width: fit-content;
margin: auto;
}

@media (max-width: 460px) {
.main-download-action {
margin-top: 5px;
width: 100%;
}

.aux-download-action {
width: unset;
}
}
</style>
{% endblock %}

{% block body %}
<div class="centered-form">
<form method="post" class="form-area">
{% csrf_token %}
<div class="form-group">
<div class="form-field">
<div>{{ form.start_time.label }}</div>
<div>{{ form.start_time }}</div>
</div>
<div class="form-field">
<div>{{ form.end_time.label }}</div>
<div>{{ form.end_time }}</div>
</div>
</div>
<button>Get</button>
{% if form.has_error('start_time') %}
{{ form.errors['start_time'] }}
{% endif %}
</form>
</div>
{% endblock %}
5 changes: 5 additions & 0 deletions templates/organization/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ <h3>{{ _('Controls') }} <i class="fa fa-question-circle"></i></h3>
<a href="{{ url('admin:judge_organization_change', organization.id) }}">{{ _('Admin organization') }}</a>
</div>
{% endif %}
{% if can_edit %}
<div>
<a href="{{ url('get_submissions_data', organization.slug) }}">{{ _('Download submissions data') }}</a>
</div>
{% endif %}
</div>
</div>

Expand Down
Loading