forked from heroku/python-getting-started
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
105 lines (79 loc) · 3.37 KB
/
Copy pathviews.py
File metadata and controls
105 lines (79 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from django.contrib.auth.mixins import LoginRequiredMixin
from django.db.models import Prefetch, Q
from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404
from django.views.generic.detail import BaseDetailView
from django.views.generic.list import ListView
from django.views.generic.edit import FormView
import hashcode.models as models
from .forms import SubmissionForm, SubmissionDebugForm
from .core.core import Submission
from .core.visual import Solution
class SubmissionsView(LoginRequiredMixin, ListView):
model = models.Case
def get_queryset(self, *args, **kwargs):
qs = super().get_queryset(*args, **kwargs)
scores = models.Score.objects.filter(user=self.request.user)
qs = qs.prefetch_related(
Prefetch('score_set', queryset=scores, to_attr='scores'))
return qs
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = SubmissionForm()
return context
class SubmissionFormView(LoginRequiredMixin, FormView):
form_class = SubmissionForm
template_name = "hashcode/submit.html"
def form_valid(self, form):
submissions = [
Submission(file, self.request.user)
for file in self.request.FILES.getlist('files')
]
filenames = [submission.key for submission in submissions]
cases = {
case.name: case
for case in models.Case.objects.filter(name__in=filenames)
}
for submission in submissions:
submission.case = cases.get(submission.key)
submission.judge()
return render(
self.request,
'hashcode/submission_score.html',
{'submissions': submissions})
class SubmissionDetailView(LoginRequiredMixin, BaseDetailView):
def get_object(self):
return get_object_or_404(
models.Score.objects,
case_id=self.kwargs['case_id'],
user=self.request.user,
)
def render_to_response(self, context, **kwargs):
if self.request.GET.get('visual'):
input_file = self.object.case.data
output_file = self.object.submission
solution = Solution.from_submission(input_file, output_file)
return render(
self.request,
'hashcode/submission_visual.html',
{'solution': solution})
response = HttpResponse(self.object.submission, content_type='application/json')
response['Content-Disposition'] = f'filename="{self.object.case.name}.out"'
return response
class SubmissionDebugView(LoginRequiredMixin, FormView):
form_class = SubmissionDebugForm
template_name = "hashcode/submit_debug.html"
def form_valid(self, form):
out_file = form.cleaned_data['file']
in_file = form.cleaned_data['case'].data
solution = Solution.from_submission(in_file, out_file)
return render(
self.request,
'hashcode/submission_visual.html',
{'solution': solution})
class CaseDetailView(LoginRequiredMixin, BaseDetailView):
model = models.Case
def render_to_response(self, context, **kwargs):
response = HttpResponse(self.object.data, content_type='application/json')
response['Content-Disposition'] = f'filename="{self.object.name}.in"'
return response