delete searchbox
[edumed.git] / stage2 / views.py
index 98e6dc7..7f3422e 100644 (file)
@@ -16,9 +16,9 @@ def all_assignments(participant):
     for assignment in assignments:
         assignment.answer = assignment.answer_set.filter(participant=participant).first()
         assignment.forms = [
-            (AttachmentForm(assignment=assignment, file_no=i, label=label),
+            (AttachmentForm(assignment=assignment, file_no=i, label=label, extensions=ext),
              assignment.answer.attachment_set.filter(file_no=i).first() if assignment.answer else None)
-            for i, label in enumerate(assignment.file_descriptions, 1)]
+            for i, (label, ext) in enumerate(assignment.file_descriptions, 1)]
     return assignments
 
 
@@ -40,12 +40,12 @@ def upload(request, assignment_id, participant_id, key):
     now = timezone.now()
     if assignment.deadline < now:
         raise Http404  # TODO za późno
-    for i, label in enumerate(assignment.file_descriptions, 1):
+    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)
+            assignment=assignment, file_no=i, label=label, instance=attachment, extensions=ext)
         if form.is_valid():
             form.save()
     return HttpResponseRedirect(reverse('stage2_participant', args=(participant_id, key)))
@@ -75,12 +75,13 @@ def get_file(request, assignment_id, file_no, participant_id, key):
 def assignment_list(request):
     assignments = request.user.stage2_assignments.all()
     if not assignments:
-        return HttpResponseForbidden()
-    if len(assignments) == 1:
-        return HttpResponseRedirect(reverse('stage2_answer_list', args=[assignments.get().id]))
+        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()
+    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 HttpResponseRedirect(reverse('stage2_answer_list', args=[non_empty_assignments[0].id]))
     return render(request, 'stage2/assignment_list.html', {'assignments': assignments})
 
 
@@ -95,7 +96,7 @@ def available_answers(assignment, expert, answer_with_errors=None, form_with_err
         attachments_by_file_no = {attachment.file_no: attachment for attachment in attachments}
         answer.attachments = [
             (desc, attachments_by_file_no.get(i))
-            for (i, desc) in enumerate(assignment.file_descriptions, 1)]
+            for (i, (desc, ext)) in enumerate(assignment.file_descriptions, 1)]
         if answer == answer_with_errors:
             answer.form = form_with_errors
         else:
@@ -108,7 +109,7 @@ def available_answers(assignment, expert, answer_with_errors=None, form_with_err
 def answer_list(request, assignment_id):
     assignment = get_object_or_404(Assignment, id=assignment_id)
     if request.user not in assignment.experts.all():
-        return HttpResponseForbidden()
+        return HttpResponseForbidden('Not allowed')
     return render(request, 'stage2/answer_list.html',
                   {'answers': available_answers(assignment, request.user), 'assignment': assignment})
 
@@ -117,7 +118,7 @@ def answer_list(request, assignment_id):
 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()
+        return HttpResponseForbidden('Not allowed')
     return render(request, 'stage2/answer_list.html', {
         'answers': available_answers(assignment, request.user, marked=True),
         'assignment': assignment,
@@ -136,9 +137,9 @@ def expert_download(request, attachment_id):
 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()
+        return HttpResponseForbidden('Not allowed')
     if answer.assignment.is_active():
-        return HttpResponseForbidden()
+        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():