Exemplo n.º 1
0
 def post(self, request, format=None):
     serializer = SubmissionSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response({'error': serializer.errors},
                     status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 2
0
 def put(self, request, pk, format=None):
     submission_item = self.get_object(pk)
     serializer = SubmissionSerializer(submission_item, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response({'error': serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 3
0
    def post(self, request, *args, **kwargs):
        if request.user is None or request.user.is_authenticated is False:
            return Response(res_format('Login required', status=Message.ERROR),
                            status=status.HTTP_200_OK)
        last_submit_time = request.session.get('last_submit_time', None)
        if last_submit_time and (
                datetime.now() -
                datetime.fromtimestamp(last_submit_time)).seconds < 5:
            return Response(res_format(
                "Cannot be resubmitted within five seconds",
                status=Message.ERROR),
                            status=status.HTTP_200_OK)
        request.session['last_submit_time'] = datetime.now().timestamp()

        serializer = SubmissionSerializer(data=request.data)
        if serializer.is_valid():
            submission = serializer.save(str(request.user))
            if submission is not None:
                UserProfile.objects.filter(username=str(request.user)).update(
                    submitted=F('submitted') + 1)
                if submission.status != config.Result.Status.STATUS_PENDING.value:
                    return Response(res_format(submission.id),
                                    status=status.HTTP_200_OK)
                submit_task.delay(submission.id)
                return Response(res_format(submission.id),
                                status=status.HTTP_200_OK)
            return Response(res_format('submit error', status=Message.ERROR),
                            status=status.HTTP_200_OK)
        else:
            return Response(res_format(serializer.errors,
                                       status=Message.ERROR),
                            status=status.HTTP_200_OK)
Exemplo n.º 4
0
    def test_submission_4(self):
        request_data = {
            "remote_oj": "unit_test_oj",
            "remote_id": "unit_test_pid",
            "language": "unit_test_language"
        }

        serializer = SubmissionSerializer(data=request_data)
        self.assertFalse(serializer.is_valid(), serializer.errors)
Exemplo n.º 5
0
    def test_submission_2(self):
        request_data = {
            "remote_oj": "unit_test_oj",
            "remote_id": "unit_test_pid",
            # "contest_id": self.contest.id,
            "code": "#include <cstdio>"
                    "#include <cstring>"
                    "#include <algorithm>"
                    ""
                    "using namespace std;"
                    "int main(int argc, char **argv){"
                    ""
                    "   std::cout << \"Hello World\" << std::endl;"
                    "}",
            "language": "unit_test_language"
        }

        serializer = SubmissionSerializer(data=request_data)
        self.assertTrue(serializer.is_valid(), serializer.errors)
Exemplo n.º 6
0
    def submit(self, request, **kwargs):
        problem = self.get_object()
        code = request.data.get('code', None)
        if code is None:
            return response.Response({}, status=status.HTTP_400_BAD_REQUEST)

        submission = Submission.objects.create(problem=problem, code=code)
        if not settings.TESTING:
            judge.delay(submission.id)
        return response.Response(SubmissionSerializer(submission).data,
                                 status=status.HTTP_202_ACCEPTED)
Exemplo n.º 7
0
    def to_representation(self, value):

        if isinstance(value, Post):
            serializer = PostSerializer(value)
        elif isinstance(value, Comment):
            serializer = CommentSerializer(value)
        elif isinstance(value, Like):
            serializer = LikeSerializer(value)
        elif isinstance(value, Blog):
            serializer = BlogSerializer(value)
        elif isinstance(value, Submission):
            serializer = SubmissionSerializer(value)
        elif isinstance(value, Message):
            serializer = MessageSerializer(value)
        else:
            return value.__str__()

        return serializer.data
Exemplo n.º 8
0
 def get(self, request, pk, format=None):
     submission_item = self.get_object(pk)
     serializer = SubmissionSerializer(submission_item)
     return Response(serializer.data)
Exemplo n.º 9
0
 def get(self, request, format=None):
     submission = Submission.objects.all()
     serializer = SubmissionSerializer(submission, many=True)
     return Response(serializer.data)