Skip to content

Commit

Permalink
minor api improvements - v1.1.0
Browse files Browse the repository at this point in the history
- add throttling (limiting no.of requests/day)
- add pagination (10 results/page)
  • Loading branch information
Bhupesh-V committed Jul 27, 2019
1 parent 7cada64 commit f6379a3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 38 deletions.
47 changes: 30 additions & 17 deletions api/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from django.http import HttpResponse
from rest_framework.renderers import JSONRenderer
from rest_framework.decorators import api_view, permission_classes
from rest_framework.views import APIView
from rest_framework import status
from .serializers import tutorialSerializer, tagSerializer, tutorialPOST
from django.shortcuts import render
from app.models import tutorial, tag
from taggie.parser import generateTags
from rest_framework.pagination import PageNumberPagination


# Just wraps a simple HTTP Response to a JSON Response
Expand All @@ -26,55 +28,66 @@ def tutorial_Tags(request, tags):
"""
Return tutorials with a {tag}
"""
paginator = PageNumberPagination()
tags = tags.split(',')
customTutorials = tutorial.objects.filter(tags__name__in = tags).distinct()
serializer = tutorialSerializer(customTutorials, many=True)
return JSONResponse(serializer.data)
customTutorials = tutorial.objects.filter(tags__name__in = tags).order_by('id').distinct()
context = paginator.paginate_queryset(customTutorials, request)
serializer = tutorialSerializer(context, many=True)
return paginator.get_paginated_response(serializer.data)


@api_view(['GET'])
def latest(request):
"""
Return latest 10 tutorials from tutorialdb
"""
paginator = PageNumberPagination()
results = tutorial.objects.all().order_by('-created_date')[:10]
serializer = tutorialSerializer(results, many=True)
return JSONResponse(serializer.data)
context = paginator.paginate_queryset(results, request)
serializer = tutorialSerializer(context, many=True)
return paginator.get_paginated_response(serializer.data)


@api_view(['GET'])
def tutorial_Tags_Category(request, tags, category):
"""
Return tutorials with a {tag} and {category}
"""
paginator = PageNumberPagination()
tags = tags.split(',')
category = category.split(',')
customTutorials = tutorial.objects.filter(tags__name__in = tags, category__in = category).distinct()
serializer = tutorialSerializer(customTutorials, many=True)
return JSONResponse(serializer.data)
customTutorials = tutorial.objects.filter(tags__name__in = tags, category__in = category).order_by('id').distinct()
context = paginator.paginate_queryset(customTutorials, request)
serializer = tutorialSerializer(context, many=True)
return paginator.get_paginated_response(serializer.data)


@api_view(['GET'])
def tags(request):
"""
Returns all tags
Return all tags
"""
tags = tag.objects.all()
serializer = tagSerializer(tags, many=True)
return JSONResponse(serializer.data)
paginator = PageNumberPagination()
tags = tag.objects.all().order_by('id')
context = paginator.paginate_queryset(tags, request)
serializer = tagSerializer(context, many=True)
return paginator.get_paginated_response(serializer.data)


@api_view(['GET', 'POST'])
def tutorials(request):
"""
get: Returns all tutorials
get: Return all tutorials
post: POST a tutorial
post: submit a tutorial
"""
if request.method == 'GET':
tutorials = tutorial.objects.all()
serializer = tutorialSerializer(tutorials, many=True)
return JSONResponse(serializer.data)
paginator = PageNumberPagination()
tutorials = tutorial.objects.all().order_by('id')
context = paginator.paginate_queryset(tutorials, request)
serializer = tutorialSerializer(context, many=True)
return paginator.get_paginated_response(serializer.data)
elif request.method == 'POST':
postserializer = tutorialPOST(data = request.data)
if postserializer.is_valid():
Expand Down
34 changes: 13 additions & 21 deletions tutorialdb/settings.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
"""
Django settings for tutorialdb project.
Generated by 'django-admin startproject' using Django 2.2.3.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""

import os

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


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ['SECRET_KEY']

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

ALLOWED_HOSTS = ['127.0.0.1','192.168.42.2']
Expand All @@ -43,6 +25,19 @@
'taggie',
]

REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': [
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle'
],
'DEFAULT_THROTTLE_RATES': {
'anon': '100/day',
'user': '100/day'
},
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 10
}

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
Expand Down Expand Up @@ -118,8 +113,5 @@
USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_DIRS = ( os.path.join('static'), )

0 comments on commit f6379a3

Please sign in to comment.