From f72057f558fd4b2e3cfe6cc483512f75233c208e Mon Sep 17 00:00:00 2001 From: Arif Date: Sun, 14 Mar 2021 11:41:56 +0530 Subject: [PATCH 1/4] Added Pagination for quiz app --- AlgoPhantomBackend/.idea/.gitignore | 5 ++++ .../.idea/AlgoPhantomBackend.iml | 23 +++++++++++++++ AlgoPhantomBackend/.idea/dataSources.xml | 19 ++++++++++++ .../inspectionProfiles/profiles_settings.xml | 6 ++++ AlgoPhantomBackend/.idea/misc.xml | 7 +++++ AlgoPhantomBackend/.idea/modules.xml | 8 +++++ AlgoPhantomBackend/.idea/vcs.xml | 6 ++++ .../AlgoPhantomBackend/settings.py | 6 ++-- AlgoPhantomBackend/quiz/pagination.py | 29 +++++++++++++++++++ AlgoPhantomBackend/quiz/views.py | 24 +++++++++++---- 10 files changed, 124 insertions(+), 9 deletions(-) create mode 100644 AlgoPhantomBackend/.idea/.gitignore create mode 100644 AlgoPhantomBackend/.idea/AlgoPhantomBackend.iml create mode 100644 AlgoPhantomBackend/.idea/dataSources.xml create mode 100644 AlgoPhantomBackend/.idea/inspectionProfiles/profiles_settings.xml create mode 100644 AlgoPhantomBackend/.idea/misc.xml create mode 100644 AlgoPhantomBackend/.idea/modules.xml create mode 100644 AlgoPhantomBackend/.idea/vcs.xml create mode 100644 AlgoPhantomBackend/quiz/pagination.py diff --git a/AlgoPhantomBackend/.idea/.gitignore b/AlgoPhantomBackend/.idea/.gitignore new file mode 100644 index 0000000..7a47212 --- /dev/null +++ b/AlgoPhantomBackend/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/AlgoPhantomBackend/.idea/AlgoPhantomBackend.iml b/AlgoPhantomBackend/.idea/AlgoPhantomBackend.iml new file mode 100644 index 0000000..fda559a --- /dev/null +++ b/AlgoPhantomBackend/.idea/AlgoPhantomBackend.iml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/AlgoPhantomBackend/.idea/dataSources.xml b/AlgoPhantomBackend/.idea/dataSources.xml new file mode 100644 index 0000000..5ca9dc9 --- /dev/null +++ b/AlgoPhantomBackend/.idea/dataSources.xml @@ -0,0 +1,19 @@ + + + + + sqlite.xerial + true + org.sqlite.JDBC + jdbc:sqlite:$PROJECT_DIR$/db.sqlite3 + + + file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.31.1/license.txt + + + file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.31.1/sqlite-jdbc-3.31.1.jar + + + + + \ No newline at end of file diff --git a/AlgoPhantomBackend/.idea/inspectionProfiles/profiles_settings.xml b/AlgoPhantomBackend/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/AlgoPhantomBackend/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/AlgoPhantomBackend/.idea/misc.xml b/AlgoPhantomBackend/.idea/misc.xml new file mode 100644 index 0000000..6649a8c --- /dev/null +++ b/AlgoPhantomBackend/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/AlgoPhantomBackend/.idea/modules.xml b/AlgoPhantomBackend/.idea/modules.xml new file mode 100644 index 0000000..3d3facb --- /dev/null +++ b/AlgoPhantomBackend/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/AlgoPhantomBackend/.idea/vcs.xml b/AlgoPhantomBackend/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/AlgoPhantomBackend/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/AlgoPhantomBackend/AlgoPhantomBackend/settings.py b/AlgoPhantomBackend/AlgoPhantomBackend/settings.py index 8063054..beda2b4 100644 --- a/AlgoPhantomBackend/AlgoPhantomBackend/settings.py +++ b/AlgoPhantomBackend/AlgoPhantomBackend/settings.py @@ -11,7 +11,7 @@ """ import os -from .local_settings import * +# from .local_settings import * # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -162,6 +162,6 @@ EMAIL_HOST = 'smtp.gmail.com' EMAIL_USE_TLS = True EMAIL_PORT = 587 -EMAIL_HOST_USER = EMAIL_HOST_USER -EMAIL_HOST_PASSWORD = EMAIL_HOST_PASSWORD +# EMAIL_HOST_USER = EMAIL_HOST_USER +# EMAIL_HOST_PASSWORD = EMAIL_HOST_PASSWORD DATA_UPLOAD_MAX_NUMBER_FIELDS = 10240 \ No newline at end of file diff --git a/AlgoPhantomBackend/quiz/pagination.py b/AlgoPhantomBackend/quiz/pagination.py new file mode 100644 index 0000000..905265b --- /dev/null +++ b/AlgoPhantomBackend/quiz/pagination.py @@ -0,0 +1,29 @@ +from rest_framework.pagination import PageNumberPagination +class QuizPagePagination(PageNumberPagination): + page_size_query_param = 'page_size' + + MAX_PAGINATE_BY: 100 + + +class PaginationHandlerMixin(object): + @property + def paginator(self): + if not hasattr(self, '_paginator'): + if self.pagination_class is None: + self._paginator = None + else: + self._paginator = self.pagination_class() + else: + pass + return self._paginator + + def paginate_queryset(self, queryset): + + if self.paginator is None: + return None + return self.paginator.paginate_queryset(queryset, + self.request, view=self) + + def get_paginated_response(self, data): + assert self.paginator is not None + return self.paginator.get_paginated_response(data) diff --git a/AlgoPhantomBackend/quiz/views.py b/AlgoPhantomBackend/quiz/views.py index ac81fab..39d19a5 100644 --- a/AlgoPhantomBackend/quiz/views.py +++ b/AlgoPhantomBackend/quiz/views.py @@ -1,9 +1,10 @@ from django.shortcuts import render from rest_framework import generics from rest_framework.response import Response -from .models import * +from rest_framework import status from .serializers import * from rest_framework.views import APIView +from .pagination import QuizPagePagination, PaginationHandlerMixin class Quiz(generics.ListAPIView): @@ -19,9 +20,20 @@ def get(self, request, format=None, **kwargs): return Response(serializer.data) -class QuizQuestion(APIView): +class QuizQuestion(APIView, PaginationHandlerMixin): + pagination_class = QuizPagePagination + serializer_class = QuestionSerializer - def get(self, request, format=None, **kwargs): - quiz = Question.objects.filter(quiz__title__icontains=kwargs['topic']) - serializer = QuestionSerializer(quiz, many=True) - return Response(serializer.data) + # def get(self, request, format=None, **kwargs): + # quiz = Question.objects.filter(quiz__title__icontains=kwargs['topic']) + # serializer = QuestionSerializer(quiz, many=True) + # return Response(serializer.data) + + def get(self, request, format=None, *args, **kwargs): + instance = Question.objects.all() + page = self.paginate_queryset(instance) + if page is not None: + serializer = self.get_paginated_response(self.serializer_class(page,many=True).data) + else: + serializer = self.serializer_class(instance, many=True) + return Response(serializer.data, status= status.HTTP_200_OK) \ No newline at end of file From 752d989dbb6d45326eb093c5cac4fdf4261188da Mon Sep 17 00:00:00 2001 From: Arif Date: Mon, 15 Mar 2021 18:35:13 +0530 Subject: [PATCH 2/4] Updated pagination, Changed QuizQuestion(APIView) to QuizQuestion(ListAPIView) for better implementation of pagination --- AlgoPhantomBackend/quiz/pagination.py | 24 ------------------------ AlgoPhantomBackend/quiz/views.py | 21 ++++----------------- 2 files changed, 4 insertions(+), 41 deletions(-) diff --git a/AlgoPhantomBackend/quiz/pagination.py b/AlgoPhantomBackend/quiz/pagination.py index 905265b..04d9631 100644 --- a/AlgoPhantomBackend/quiz/pagination.py +++ b/AlgoPhantomBackend/quiz/pagination.py @@ -3,27 +3,3 @@ class QuizPagePagination(PageNumberPagination): page_size_query_param = 'page_size' MAX_PAGINATE_BY: 100 - - -class PaginationHandlerMixin(object): - @property - def paginator(self): - if not hasattr(self, '_paginator'): - if self.pagination_class is None: - self._paginator = None - else: - self._paginator = self.pagination_class() - else: - pass - return self._paginator - - def paginate_queryset(self, queryset): - - if self.paginator is None: - return None - return self.paginator.paginate_queryset(queryset, - self.request, view=self) - - def get_paginated_response(self, data): - assert self.paginator is not None - return self.paginator.get_paginated_response(data) diff --git a/AlgoPhantomBackend/quiz/views.py b/AlgoPhantomBackend/quiz/views.py index 39d19a5..577f08a 100644 --- a/AlgoPhantomBackend/quiz/views.py +++ b/AlgoPhantomBackend/quiz/views.py @@ -1,10 +1,10 @@ -from django.shortcuts import render from rest_framework import generics from rest_framework.response import Response from rest_framework import status from .serializers import * +from rest_framework.generics import ListAPIView from rest_framework.views import APIView -from .pagination import QuizPagePagination, PaginationHandlerMixin +from .pagination import QuizPagePagination class Quiz(generics.ListAPIView): @@ -20,20 +20,7 @@ def get(self, request, format=None, **kwargs): return Response(serializer.data) -class QuizQuestion(APIView, PaginationHandlerMixin): +class QuizQuestion(ListAPIView): pagination_class = QuizPagePagination serializer_class = QuestionSerializer - - # def get(self, request, format=None, **kwargs): - # quiz = Question.objects.filter(quiz__title__icontains=kwargs['topic']) - # serializer = QuestionSerializer(quiz, many=True) - # return Response(serializer.data) - - def get(self, request, format=None, *args, **kwargs): - instance = Question.objects.all() - page = self.paginate_queryset(instance) - if page is not None: - serializer = self.get_paginated_response(self.serializer_class(page,many=True).data) - else: - serializer = self.serializer_class(instance, many=True) - return Response(serializer.data, status= status.HTTP_200_OK) \ No newline at end of file + queryset = Question.objects.all() From ab8e7ba99b1a7ad8b015d893f51f3376f2f50674 Mon Sep 17 00:00:00 2001 From: Arif Date: Tue, 30 Mar 2021 17:32:43 +0530 Subject: [PATCH 3/4] Added unittest for quiz model --- AlgoPhantomBackend/quiz/tests.py | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 AlgoPhantomBackend/quiz/tests.py diff --git a/AlgoPhantomBackend/quiz/tests.py b/AlgoPhantomBackend/quiz/tests.py deleted file mode 100644 index 7ce503c..0000000 --- a/AlgoPhantomBackend/quiz/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. From 1e44e7f526f6d5b2043bc10ac87f41015d429eff Mon Sep 17 00:00:00 2001 From: Arif Date: Tue, 30 Mar 2021 17:33:22 +0530 Subject: [PATCH 4/4] Added unittest for quiz model --- AlgoPhantomBackend/quiz/tests/__init__.py | 0 AlgoPhantomBackend/quiz/tests/test_model.py | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 AlgoPhantomBackend/quiz/tests/__init__.py create mode 100644 AlgoPhantomBackend/quiz/tests/test_model.py diff --git a/AlgoPhantomBackend/quiz/tests/__init__.py b/AlgoPhantomBackend/quiz/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/AlgoPhantomBackend/quiz/tests/test_model.py b/AlgoPhantomBackend/quiz/tests/test_model.py new file mode 100644 index 0000000..1f297f7 --- /dev/null +++ b/AlgoPhantomBackend/quiz/tests/test_model.py @@ -0,0 +1,20 @@ +from django.test import TestCase +from quiz.models import Quizzes,Category +from django.utils import timezone + + +# models test + +class QuizTest(TestCase): + + + def setUp(self): + self.category = Category.objects.create(name="quiz") + self.quiz = Quizzes.objects.create( + title='quiz', + category=self.category, + ) + + def test_course_category(self): + quiz = Quizzes.objects.get(title='quiz') + self.assertEqual(quiz.category_id, self.category.id)