11from django .http import HttpResponse
22from rest_framework .renderers import JSONRenderer
33from rest_framework .decorators import api_view , permission_classes
4+ from rest_framework .views import APIView
45from rest_framework import status
56from .serializers import tutorialSerializer , tagSerializer , tutorialPOST
67from django .shortcuts import render
78from app .models import tutorial , tag
89from taggie .parser import generateTags
10+ from rest_framework .pagination import PageNumberPagination
911
1012
1113# Just wraps a simple HTTP Response to a JSON Response
@@ -26,55 +28,66 @@ def tutorial_Tags(request, tags):
2628 """
2729 Return tutorials with a {tag}
2830 """
31+ paginator = PageNumberPagination ()
2932 tags = tags .split (',' )
30- customTutorials = tutorial .objects .filter (tags__name__in = tags ).distinct ()
31- serializer = tutorialSerializer (customTutorials , many = True )
32- return JSONResponse (serializer .data )
33+ customTutorials = tutorial .objects .filter (tags__name__in = tags ).order_by ('id' ).distinct ()
34+ context = paginator .paginate_queryset (customTutorials , request )
35+ serializer = tutorialSerializer (context , many = True )
36+ return paginator .get_paginated_response (serializer .data )
37+
3338
3439@api_view (['GET' ])
3540def latest (request ):
3641 """
3742 Return latest 10 tutorials from tutorialdb
3843 """
44+ paginator = PageNumberPagination ()
3945 results = tutorial .objects .all ().order_by ('-created_date' )[:10 ]
40- serializer = tutorialSerializer (results , many = True )
41- return JSONResponse (serializer .data )
46+ context = paginator .paginate_queryset (results , request )
47+ serializer = tutorialSerializer (context , many = True )
48+ return paginator .get_paginated_response (serializer .data )
4249
4350
4451@api_view (['GET' ])
4552def tutorial_Tags_Category (request , tags , category ):
4653 """
4754 Return tutorials with a {tag} and {category}
4855 """
56+ paginator = PageNumberPagination ()
4957 tags = tags .split (',' )
5058 category = category .split (',' )
51- customTutorials = tutorial .objects .filter (tags__name__in = tags , category__in = category ).distinct ()
52- serializer = tutorialSerializer (customTutorials , many = True )
53- return JSONResponse (serializer .data )
59+ customTutorials = tutorial .objects .filter (tags__name__in = tags , category__in = category ).order_by ('id' ).distinct ()
60+ context = paginator .paginate_queryset (customTutorials , request )
61+ serializer = tutorialSerializer (context , many = True )
62+ return paginator .get_paginated_response (serializer .data )
5463
5564
5665@api_view (['GET' ])
5766def tags (request ):
5867 """
59- Returns all tags
68+ Return all tags
6069
6170 """
62- tags = tag .objects .all ()
63- serializer = tagSerializer (tags , many = True )
64- return JSONResponse (serializer .data )
71+ paginator = PageNumberPagination ()
72+ tags = tag .objects .all ().order_by ('id' )
73+ context = paginator .paginate_queryset (tags , request )
74+ serializer = tagSerializer (context , many = True )
75+ return paginator .get_paginated_response (serializer .data )
6576
6677
6778@api_view (['GET' , 'POST' ])
6879def tutorials (request ):
6980 """
70- get: Returns all tutorials
81+ get: Return all tutorials
7182
72- post: POST a tutorial
83+ post: submit a tutorial
7384 """
7485 if request .method == 'GET' :
75- tutorials = tutorial .objects .all ()
76- serializer = tutorialSerializer (tutorials , many = True )
77- return JSONResponse (serializer .data )
86+ paginator = PageNumberPagination ()
87+ tutorials = tutorial .objects .all ().order_by ('id' )
88+ context = paginator .paginate_queryset (tutorials , request )
89+ serializer = tutorialSerializer (context , many = True )
90+ return paginator .get_paginated_response (serializer .data )
7891 elif request .method == 'POST' :
7992 postserializer = tutorialPOST (data = request .data )
8093 if postserializer .is_valid ():
0 commit comments