def create(request): if SUBMISSION_END < datetime.datetime.now(): raise Http404 context = { 'user': request.user, 'rule': render_document(Node(nid=settings.SUBMISSION_RULE_DOCID).model.current_revision.text.text) } if request.POST.get('submit'): sub = SubmissionForm(request.POST, request.FILES) if sub.is_valid(): submission = sub.save(commit=False) submission.user = request.user submission.save() for f in request.FILES.getlist('slide'): form = SubmissionFileForm().save(commit=False) form.submission = submission form.file = f form.save() return redirect('submission:list') else: pass return render(request, 'submission/create.html', context)
def test_submission_form(self): """ Test submission form. """ problem = Problem.objects.get(pk=1) source_file = open(self._get_source_file(), 'rb') file_dict = {'source_code': SimpleUploadedFile(source_file.name, source_file.read())} form = SubmissionForm({"participant": self.participant.id, "problem": problem.id, "programming_language": 4}, file_dict) self.assertTrue(form.is_valid(), form.errors)
def post(self, request, *args, **kwargs): problem = Problem.objects.get(pk=kwargs.get('pk', None)) form = SubmissionForm(request.POST, request.FILES) if form.is_valid(): submission = Submission(problem=problem, submitter=request.user.student, code=request.POST['code'], number=Submission.objects.filter(problem=problem, submitter=request.user.student).count()+1 ) submission.save() return redirect('submission', problem.pk, submission.pk) else: return redirect('submit', problem.pk)
def upload(request): if request.POST: form = SubmissionForm(request.POST, request.FILES) if form.is_valid(): form.save() return HttpResponseRedirect('/view_personal') else: form = SubmissionForm('/view_personal') args = {} args.update(csrf(request)) args['form'] = form return render_to_response('personal_submission_view.html',args)
def get_context_data(self, **kwargs): context = super(SubmitView, self).get_context_data(**kwargs) context['problem'] = problem = Problem.objects.get(pk=kwargs.get('pk', None)) context['submit_form'] = SubmissionForm() context['submissions'] = subs = Submission.objects.filter(problem=problem, submitter=self.request.user.student) context['can_submit'] = ([x for x in subs if x.status <= 1 or x.status == 2].__len__() == 0) return context
def submit_for_problem(request, id): user = request.user if not user.participant.competition.in_progress(): return HttpResponseNotAllowed("Competition not in progress.") form = SubmissionForm(request.POST, request.FILES) if form.is_valid(): submission = form.save(commit=False) submission.competition = user.participant.competition submission.participant = user.participant submission.problem = get_object_or_404(Problem, pk=id) submission.save() return redirect('/', context_instance=RequestContext(request)) else: return redirect('/', {"form": form}, context_instance=RequestContext(request))
def edit(request, submission_id): if SUBMISSION_END < datetime.datetime.now(): instance = get_object_or_404(Submission, id=submission_id, user=request.user) if instance.status!='E': raise Http404 elif request.user.has_perm('submission.review'): instance = get_object_or_404(Submission, id=submission_id) else: instance = get_object_or_404(Submission, id=submission_id, user=request.user) if request.POST.get('submit'): if request.FILES: submission = SubmissionForm(request.POST, request.FILES, instance=instance) else: submission = SubmissionForm(request.POST, instance=instance) if submission.is_valid(): sub = submission.save() if request.FILES.getlist('slide'): for f in sub.files.all(): f.file.delete() f.delete() for f in request.FILES.getlist('slide'): form = SubmissionFileForm().save(commit=False) form.submission = sub form.file = f form.save() return redirect('submission:list') else: pass context = { 'user': request.user, 'rule': render_document(Node(nid=settings.SUBMISSION_RULE_DOCID).model.current_revision.text.text), 'submission': instance, } return render(request, 'submission/edit.html', context)