Skip to content

Commit f6379a3

Browse files
committed
minor api improvements - v1.1.0
- add throttling (limiting no.of requests/day) - add pagination (10 results/page)
1 parent 7cada64 commit f6379a3

2 files changed

Lines changed: 43 additions & 38 deletions

File tree

api/views.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from django.http import HttpResponse
22
from rest_framework.renderers import JSONRenderer
33
from rest_framework.decorators import api_view, permission_classes
4+
from rest_framework.views import APIView
45
from rest_framework import status
56
from .serializers import tutorialSerializer, tagSerializer, tutorialPOST
67
from django.shortcuts import render
78
from app.models import tutorial, tag
89
from 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'])
3540
def 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'])
4552
def 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'])
5766
def 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'])
6879
def 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():

tutorialdb/settings.py

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,10 @@
1-
"""
2-
Django settings for tutorialdb project.
3-
4-
Generated by 'django-admin startproject' using Django 2.2.3.
5-
6-
For more information on this file, see
7-
https://docs.djangoproject.com/en/2.2/topics/settings/
8-
9-
For the full list of settings and their values, see
10-
https://docs.djangoproject.com/en/2.2/ref/settings/
11-
"""
12-
131
import os
142

153
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
164
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
175

18-
19-
# Quick-start development settings - unsuitable for production
20-
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
21-
22-
# SECURITY WARNING: keep the secret key used in production secret!
236
SECRET_KEY = os.environ['SECRET_KEY']
247

25-
# SECURITY WARNING: don't run with debug turned on in production!
268
DEBUG = True
279

2810
ALLOWED_HOSTS = ['127.0.0.1','192.168.42.2']
@@ -43,6 +25,19 @@
4325
'taggie',
4426
]
4527

28+
REST_FRAMEWORK = {
29+
'DEFAULT_THROTTLE_CLASSES': [
30+
'rest_framework.throttling.AnonRateThrottle',
31+
'rest_framework.throttling.UserRateThrottle'
32+
],
33+
'DEFAULT_THROTTLE_RATES': {
34+
'anon': '100/day',
35+
'user': '100/day'
36+
},
37+
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
38+
'PAGE_SIZE': 10
39+
}
40+
4641
MIDDLEWARE = [
4742
'django.middleware.security.SecurityMiddleware',
4843
'django.contrib.sessions.middleware.SessionMiddleware',
@@ -118,8 +113,5 @@
118113
USE_TZ = True
119114

120115

121-
# Static files (CSS, JavaScript, Images)
122-
# https://docs.djangoproject.com/en/2.2/howto/static-files/
123-
124116
STATIC_URL = '/static/'
125117
STATICFILES_DIRS = ( os.path.join('static'), )

0 commit comments

Comments
 (0)