def start_demo_exam(request, test_id): demogrp = Group.get_demo_group() test = Test.objects.get(id=int(test_id)) if not test.is_demo: return HttpResponse(status=400) # Is the student writing an exam right now? try: tbw = TestBeingWritten.objects.get(written_by=request.user) except TestBeingWritten.DoesNotExist: pass else: messages.add_message(request, messages.ERROR, u'Egzamin już jest pisany!') # Try to automatically detect an exam and term for this one, a demo one try: exam = Exam.objects.get(test=test, group=demogrp) except Exam.DoesNotExist: exam = Exam(test=test, group=demogrp, name=u'DEMO EGZAMIN', owner=demogrp.teacher) exam.save() # Fetch term for this one try: term = exam.term_set.all()[0] except: term = Term(exam=exam, name=u'DEMO TERMIN', term_time=datetime.now(), is_progressing=True) term.save() # Create a TBW for this student now tbw = TestBeingWritten(term=term, written_by=request.user) tbw.save() tbw.set_test_written(TestWritten(test)) return redirect('/test/')
def setup_ajax(request, exam_id, term_id): """POST variable 'students' should be a JSON-encoded list of student IDs to partake in the test""" if 'students' not in request.POST: raise HttpResponse(status=400) try: trm = Term.objects.get(id=int(term_id)) except: raise Http404 if trm.exam.group.teacher != request.user: return HttpResponse(status=403) # Ok, now we will take those students take these damn exams try: students = set([User.objects.get(id=x) for x in json.loads(request.POST['students'])]) except User.DoesNotExist: return HttpResponse(status=400) applicable_students = set(get_applicable_students(trm, dont_sort=True)) if not students.issubset(applicable_students): return HttpResponse(status=403) # Students can attempt writing exams! for student in students: tbw = TestBeingWritten(term=trm, written_by=student) tbw.save() tbw.set_test_written(TestWritten(trm.exam.test)) trm.is_progressing = True trm.save() # all ready, redirect to supervision return redirect('/teacher/exams/%s/terms/%s/supervise/' % (trm.exam.id, trm.id))