diff options
-rw-r--r-- | api/tests.py | 2 | ||||
-rw-r--r-- | api/urls.py | 5 | ||||
-rw-r--r-- | api/views.py | 63 | ||||
-rw-r--r-- | online_test/settings.py | 4 |
4 files changed, 66 insertions, 8 deletions
diff --git a/api/tests.py b/api/tests.py index feffcc0..4ef6fa4 100644 --- a/api/tests.py +++ b/api/tests.py @@ -1,5 +1,5 @@ from django.test import TestCase -from django.core.urlresolvers import reverse +from django.urls import reverse from django.contrib.auth.models import User from rest_framework.test import APIClient from rest_framework import status diff --git a/api/urls.py b/api/urls.py index e81a275..3edf945 100644 --- a/api/urls.py +++ b/api/urls.py @@ -23,6 +23,11 @@ urlpatterns = [ views.AnswerValidator.as_view(), name='validators'), url(r'validate/(?P<uid>[0-9]+)/$', views.AnswerValidator.as_view(), name='validator'), + url(r'course/(?P<pk>[0-9]+)/$', + views.ICourse.as_view(), name='get_course'), + url(r'quit/(?P<answerpaper_id>\d+)/$', views.QuitQuiz.as_view(), + name="quit_quiz"), + url(r'login/$', views.login, name='login') ] urlpatterns = format_suffix_patterns(urlpatterns) diff --git a/api/views.py b/api/views.py index b81e895..5bbc077 100644 --- a/api/views.py +++ b/api/views.py @@ -9,7 +9,12 @@ from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status from rest_framework import permissions +from rest_framework.authtoken.models import Token +from rest_framework.decorators import ( + api_view, authentication_classes, permission_classes +) from django.http import Http404 +from django.contrib.auth import authenticate from yaksh.code_server import get_result as get_result_from_code_server from yaksh.settings import SERVER_POOL_PORT, SERVER_HOST_NAME import json @@ -50,6 +55,7 @@ class StartQuiz(APIView): raise Http404 def get(self, request, course_id, quiz_id, format=None): + context = {} user = request.user quiz = self.get_quiz(quiz_id, user) questionpaper = quiz.questionpaper_set.first() @@ -58,7 +64,9 @@ class StartQuiz(APIView): questionpaper, user, course_id) if last_attempt and last_attempt.is_attempt_inprogress(): serializer = AnswerPaperSerializer(last_attempt) - return Response(serializer.data) + context["time_left"] = last_attempt.time_left() + context["answerpaper"] = serializer.data + return Response(context) can_attempt, msg = questionpaper.can_attempt_now(user, course_id) if not can_attempt: @@ -71,7 +79,9 @@ class StartQuiz(APIView): answerpaper = questionpaper.make_answerpaper(user, ip, attempt_number, course_id) serializer = AnswerPaperSerializer(answerpaper) - return Response(serializer.data, status=status.HTTP_201_CREATED) + context["time_left"] = answerpaper.time_left() + context["answerpaper"] = serializer.data + return Response(context, status=status.HTTP_201_CREATED) class QuestionDetail(APIView): @@ -175,7 +185,7 @@ class AnswerValidator(APIView): def post(self, request, answerpaper_id, question_id, format=None): try: - user_answer = request.data['answer'] + user_answer = str(request.data['answer']) except KeyError: return Response(status=status.HTTP_400_BAD_REQUEST) user = request.user @@ -196,8 +206,8 @@ class AnswerValidator(APIView): if result.get('success'): answer.correct = True answer.marks = question.points - answer.error = json.dumps(result.get('error')) - answer.save() + answer.error = json.dumps(result.get('error')) + answer.save() answerpaper.update_marks(state='inprogress') return Response(result) @@ -208,11 +218,11 @@ class AnswerValidator(APIView): # update result if result['status'] == 'done': final_result = json.loads(result.get('result')) - answer.error = json.dumps(result.get('error')) + answer.error = json.dumps(final_result.get('error')) if final_result.get('success'): answer.correct = True answer.marks = answer.question.points - answer.save() + answer.save() answerpaper = answer.answerpaper_set.get() answerpaper.update_marks(state='inprogress') return Response(result) @@ -372,3 +382,42 @@ class QuestionPaperDetail(APIView): questionpaper = self.get_questionpaper(pk, request.user) questionpaper.delete() return Response(status=status.HTTP_204_NO_CONTENT) + + +class ICourse(APIView): + def get(self, request, pk, format=None): + course = Course.objects.get(id=pk) + serializer = CourseSerializer(course) + return Response(serializer.data) + + +@api_view(['POST']) +@authentication_classes(()) +@permission_classes(()) +def login(request): + data = {} + if request.method == "POST": + username = request.data.get('username') + password = request.data.get('password') + user = authenticate(username=username, password=password) + if user is not None and user.is_authenticated: + token, created = Token.objects.get_or_create(user=user) + data = { + 'token': token.key + } + return Response(data, status=status.HTTP_201_CREATED) + + +class QuitQuiz(APIView): + def get_answerpaper(self, answerpaper_id): + try: + return AnswerPaper.objects.get(id=answerpaper_id) + except AnswerPaper.DoesNotExist: + raise Http404 + + def get(self, request, answerpaper_id, format=None): + answerpaper = self.get_answerpaper(answerpaper_id) + answerpaper.status = 'completed' + answerpaper.save() + serializer = AnswerPaperSerializer(answerpaper) + return Response(serializer.data)
\ No newline at end of file diff --git a/online_test/settings.py b/online_test/settings.py index 99c6d96..284ec64 100644 --- a/online_test/settings.py +++ b/online_test/settings.py @@ -218,6 +218,7 @@ REST_FRAMEWORK = { # Use Django's standard `django.contrib.auth` permissions, # or allow read-only access for unauthenticated users. 'DEFAULT_AUTHENTICATION_CLASSES': [ + 'rest_framework.authentication.TokenAuthentication', 'rest_framework.authentication.BasicAuthentication', 'rest_framework.authentication.SessionAuthentication', ], @@ -226,3 +227,6 @@ REST_FRAMEWORK = { ], 'TEST_REQUEST_DEFAULT_FORMAT': 'json' } + +CORS_ORIGIN_ALLOW_ALL = True +CORS_ALLOW_CREDENTIALS = True
\ No newline at end of file |