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)
        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.º 3
0
 def test_case_insensitivity(self):
     filenames = [
         "First.java",
         "Second.JAVA",
         "Third.jAvA"
     ]
     try:
         validate_filenames(filenames)
     except ValidationError:
         self.fail("Filename validation should be case insensitive")
Exemplo n.º 4
0
 def submit(self, files, author, task):
     if not files:
         raise SubmissionError("You haven't uploaded any files.")
     try:
         validate_filenames([file.name for file in files])
         solution = self.atomic_submit(files, author, task)
         solution_submitted.send(sender=self.__class__, solution=solution)
     except ValidationError as error:
         raise SubmissionError(str(error))
     except IntegrityError:
         logger.exception('db constraint violation occurred')
         raise SubmissionError('Concurrent submission is not possible.')
Exemplo n.º 5
0
 def test_valid_filenames(self):
     filenames = [
         "HelloWorld.java",
         "HelloWorld.b",
         "HelloWorld.cpp",
         "HelloWorld.h",
         "HelloWorld.py"
     ]
     try:
         validate_filenames(filenames)
     except ValidationError as e:
         self.fail("Filename validation should succeed on the "
                   "given files. ({})".format(e.message))
Exemplo n.º 6
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.º 7
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.º 8
0
 def test_filenames(self):
     filenames = ["HelloWorld.java"]
     with self.assertRaises(ValidationError):
         validate_filenames(filenames)
Exemplo n.º 9
0
 def test_no_filenames(self):
     with self.assertRaises(ValidationError):
         validate_filenames([])
Exemplo n.º 10
0
 def test_case_insensitivity(self):
     filenames = ['First.java', 'Second.JAVA', 'Third.jAvA']
     try:
         validate_filenames(filenames)
     except ValidationError:
         self.fail('Filename validation should be case insensitive')
Exemplo n.º 11
0
 def test_invalid_filenames(self):
     filenames = ['HelloWorld.kt']
     with self.assertRaises(ValidationError):
         validate_filenames(filenames)
Exemplo n.º 12
0
 def test_valid_filenames(self):
     filenames = [
         'HelloWorld.java', 'HelloWorld.b', 'HelloWorld.cpp',
         'HelloWorld.h', 'HelloWorld.py'
     ]
     validate_filenames(filenames)