from django.utils import timezone
from django.utils.cache import patch_cache_control
from django.views.decorators.cache import never_cache
-from django.views.decorators.http import require_POST
from unidecode import unidecode
from stage2.forms import AttachmentForm, MarkForm, AssignmentFieldForm
-from stage2.models import Participant, Assignment, Answer, Attachment, Mark, FieldOption
+from stage2.models import Participant, Assignment, Answer, Attachment, Mark
def all_assignments(participant, sent_forms):
assignment = get_object_or_404(Assignment, id=assignment_id)
now = timezone.now()
if assignment.deadline < now:
- raise Http404 # TODO za późno
- all_valid = True
- attachment_forms = []
- field_forms = []
- for i, (label, ext) in enumerate(assignment.file_descriptions, 1):
- answer, created = Answer.objects.get_or_create(participant=participant, assignment=assignment)
- attachment, created = Attachment.objects.get_or_create(answer=answer, file_no=i)
- form = AttachmentForm(
- data=request.POST, files=request.FILES,
- assignment=assignment, file_no=i, label=label, instance=attachment, extensions=ext)
- if form.is_valid():
- form.save()
- else:
- all_valid = False
- attachment_forms.append(form)
- for i, (label, options) in enumerate(assignment.field_descriptions, 1):
- answer = Answer.objects.get(participant=participant, assignment=assignment)
- form = AssignmentFieldForm(data=request.POST, label=label, field_no=i, options=options, answer=answer)
- if form.is_valid():
- form.save()
- else:
- all_valid = False
- field_forms.append(form)
- if all_valid:
+ return HttpResponseForbidden('Not Allowed')
+ attachments_valid, attachment_forms = get_attachment_forms(assignment, participant, request)
+ fields_valid, field_forms = get_field_forms(assignment, participant, request)
+ if attachments_valid and fields_valid:
return HttpResponseRedirect(reverse('stage2_participant', args=(participant_id, key)))
else:
sent_forms = (assignment, field_forms, attachment_forms)
return response
+def get_attachment_forms(assignment, participant, request):
+ all_valid = True
+ attachment_forms = []
+ for i, (label, ext) in enumerate(assignment.file_descriptions, 1):
+ answer, created = Answer.objects.get_or_create(participant=participant, assignment=assignment)
+ attachment, created = Attachment.objects.get_or_create(answer=answer, file_no=i)
+ form = AttachmentForm(
+ data=request.POST, files=request.FILES,
+ assignment=assignment, file_no=i, label=label, instance=attachment, extensions=ext)
+ if form.is_valid():
+ form.save()
+ else:
+ all_valid = False
+ attachment_forms.append(form)
+ return all_valid, attachment_forms
+
+
+def get_field_forms(assignment, participant, request):
+ all_valid = True
+ field_forms = []
+ for i, (label, options) in enumerate(assignment.field_descriptions, 1):
+ answer = Answer.objects.get(participant=participant, assignment=assignment)
+ form = AssignmentFieldForm(data=request.POST, label=label, field_no=i, options=options, answer=answer)
+ if form.is_valid():
+ form.save()
+ else:
+ all_valid = False
+ field_forms.append(form)
+ return all_valid, field_forms
+
+
def attachment_download(attachment):
response = HttpResponse(content_type='application/force-download')
response.write(attachment.file.read())
@login_required
def assignment_list(request):
- assignments = request.user.stage2_assignments.all()
+ expert = request.user
+ assignments = expert.stage2_assignments.all()
if not assignments:
return HttpResponseForbidden('Not allowed')
for assignment in assignments:
- assignment.marked_count = Mark.objects.filter(expert=request.user, answer__assignment=assignment).count()
- assignment.to_mark_count = assignment.available_answers(request.user).count()
- assignment.supervisor = request.user in assignment.supervisors.all()
- assignment.arbiter_count = assignment.answer_set.filter(need_arbiter=True).count()
+ assignment.marked_count = assignment.available_answers(expert, marked=True).count()
+ assignment.to_mark_count = assignment.available_answers(expert).count()
+ assignment.supervisor = expert in assignment.supervisors.all()
+ assignment.arbiter_count = assignment.needing_arbiter().count()
non_empty_assignments = [ass for ass in assignments if ass.marked_count > 0 or ass.to_mark_count > 0]
if len(non_empty_assignments) == 1 and non_empty_assignments[0].to_mark_count > 0:
return render(request, 'stage2/assignment_list.html', {'assignments': assignments})
-def available_answers(assignment, expert, answer_with_errors=None, form_with_errors=None, marked=False):
+def available_answers(assignment, expert, sent_forms=None, marked=False):
if marked:
- answers = Answer.objects.filter(mark__expert=expert, assignment=assignment)
+ answers = assignment.available_answers(expert, marked=True)
+ else:
+ answers = assignment.available_answers(expert).order_by('participant__last_name')
+ answers = answers.prefetch_related('attachment_set')
+ if sent_forms:
+ sent_answer_id, mark_forms = sent_forms
else:
- answers = assignment.available_answers(expert)
- answers = answers.order_by('participant__last_name').prefetch_related('attachment_set')
+ sent_answer_id = mark_forms = None
for answer in answers:
attachments = answer.attachment_set.all()
attachments_by_file_no = {attachment.file_no: attachment for attachment in attachments}
answer.attachments = [
(desc, attachments_by_file_no.get(i))
for (i, (desc, ext)) in enumerate(assignment.file_descriptions, 1)]
- if answer == answer_with_errors:
- answer.form = form_with_errors
+ if answer.id == sent_answer_id:
+ answer.forms = mark_forms
else:
- answer.form = MarkForm(
- answer=answer, instance=answer.mark_set.filter(expert=expert).first(), prefix='ans%s' % answer.id)
+ answer.forms = []
+ for criterion in assignment.markcriterion_set.all():
+ answer.forms.append(MarkForm(
+ answer=answer,
+ criterion=criterion,
+ instance=answer.mark_set.filter(expert=expert, criterion=criterion).first(),
+ prefix='mark%s-%s' % (answer.id, criterion.id)))
return answers
@login_required
-def answer_list(request, assignment_id):
+def answer_list(request, assignment_id, marked=False):
assignment = get_object_or_404(Assignment, id=assignment_id)
- if request.user not in assignment.experts.all():
+ expert = request.user
+ if expert not in assignment.experts.all():
return HttpResponseForbidden('Not allowed')
- answers = available_answers(assignment, request.user)
+ if request.POST:
+ # ugly :/
+ answer_id = None
+ for post_key, value in request.POST.iteritems():
+ if post_key.endswith('answer_id'):
+ answer_id = int(value)
+ answer = get_object_or_404(Answer, id=answer_id)
+
+ if answer not in assignment.available_answers(expert, marked=marked):
+ return HttpResponseForbidden('Not allowed')
+ if answer.assignment.is_active():
+ return HttpResponseForbidden('Not allowed')
+ all_valid, forms = get_mark_forms(answer, request)
+ if all_valid:
+ if marked:
+ return HttpResponseRedirect(reverse('stage2_marked_answers', args=[answer.assignment_id]))
+ else:
+ return HttpResponseRedirect(reverse('stage2_answer_list', args=[answer.assignment_id]))
+ else:
+ sent_forms = answer_id, forms
+ else:
+ sent_forms = None
+ answers = available_answers(assignment, expert, sent_forms=sent_forms, marked=marked)
return render(request, 'stage2/answer_list.html', {
'answers': answers,
'assignment': assignment,
- 'field_counts': assignment.field_counts(answers)
+ 'field_counts': assignment.field_counts(answers) if not marked else None,
+ 'supervisor': expert in assignment.supervisors.all(),
+ 'marked': marked
})
-@login_required
-def marked_answer_list(request, assignment_id):
- assignment = get_object_or_404(Assignment, id=assignment_id)
- if request.user not in assignment.experts.all():
- return HttpResponseForbidden('Not allowed')
- return render(request, 'stage2/answer_list.html', {
- 'answers': available_answers(assignment, request.user, marked=True),
- 'assignment': assignment,
- 'marked': True,
- })
+def get_mark_forms(answer, request):
+ all_valid = True
+ created_marks = []
+ forms = []
+ for criterion in answer.assignment.markcriterion_set.all():
+ mark, created = Mark.objects.get_or_create(
+ answer=answer, criterion=criterion, expert=request.user, defaults={'points': 0})
+ if created:
+ created_marks.append(mark)
+ form = MarkForm(
+ data=request.POST, answer=answer, criterion=criterion, instance=mark,
+ prefix='mark%s-%s' % (answer.id, criterion.id))
+ if form.is_valid():
+ form.save()
+ else:
+ all_valid = False
+ forms.append(form)
+ if not all_valid:
+ for mark in created_marks:
+ mark.delete()
+ return all_valid, forms
@login_required
return attachment_download(attachment)
-@require_POST
-@login_required
-def mark_answer(request, answer_id):
- answer = get_object_or_404(Answer, id=answer_id)
- if request.user not in answer.assignment.experts.all():
- return HttpResponseForbidden('Not allowed')
- if answer.assignment.is_active():
- return HttpResponseForbidden('Not allowed')
- mark, created = Mark.objects.get_or_create(answer=answer, expert=request.user, defaults={'points': 0})
- form = MarkForm(data=request.POST, answer=answer, instance=mark, prefix='ans%s' % answer.id)
- if form.is_valid():
- form.save()
- elif created:
- mark.delete()
-
- return HttpResponseRedirect(reverse(
- 'stage2_answer_list' if created else 'stage2_marked_answers', args=[answer.assignment_id]))
-
-
@login_required
def csv_results(request):
import csv