Exemplo n.º 1
0
    def post(self, request, slug):
        task = get_object_or_404(Task.objects.published(), slug=slug)
        redirect_to_upload = redirect("solutions:upload", slug=slug)

        if task.is_expired:
            messages.error(request, "The deadline for this task has passed.")
            return redirect_to_upload

        uploads = request.FILES.getlist('uploads')

        if not uploads:
            messages.error(request, "You haven't uploaded any files.")
            return redirect_to_upload

        try:
            validate_filenames([f.name for f in uploads])
        except ValidationError as e:
            messages.error(request, e.message)
            return redirect_to_upload

        with transaction.atomic():
            solution = Solution.objects.create(author=request.user, task=task)
            SolutionFile.objects.bulk_create([
                SolutionFile(solution=solution, file=upload)
                for upload in uploads
            ])

        solution_submitted.send(sender=self.__class__, solution=solution)
        messages.success(request,
                         "Your solution has been submitted to the checker.")
        return redirect("solutions:list", slug=slug)
Exemplo n.º 2
0
    def post(self, request, slug):
        task = get_object_or_404(Task.objects.published(), slug=slug)
        failure_response = JsonResponse({"success": False})

        if not request.is_ajax():
            return failure_response

        if task.is_expired:
            messages.error(request, "The deadline for this task has passed.")
            return failure_response

        try:
            json_data = json.loads(request.body)
            uploads = json_data["uploads"]
        # NOTE: JSONDecodeError is only available since Python 3.5
        except (KeyError, ValueError):
            return failure_response

        if not uploads:
            messages.error(request, "You haven't uploaded any files.")
            return failure_response

        try:
            validate_filenames(uploads.keys())
        except ValidationError as e:
            messages.error(request, e.message)
            return failure_response

        files = [SimpleUploadedFile(f, c.encode()) for f, c in uploads.items()]

        with transaction.atomic():
            solution = Solution.objects.create(author=request.user, task=task)
            SolutionFile.objects.bulk_create(
                [SolutionFile(solution=solution, file=f) for f in files])

        solution_submitted.send(sender=self.__class__, solution=solution)
        messages.success(request,
                         "Your solution has been submitted to the checker.")
        return JsonResponse({"success": True})
Exemplo n.º 3
0
 def atomic_submit(self, files, author, task):
     solution = Solution.objects.create(author=author, task=task)
     SolutionFile.objects.bulk_create(
         [SolutionFile(solution=solution, file=file) for file in files])
     return solution