├── __init__.py ├── __pycache__ ├── __init__.cpython-38.pyc ├── serializers.cpython-38.pyc ├── tests.cpython-38.pyc ├── token.cpython-38.pyc ├── urls.cpython-38.pyc └── views.cpython-38.pyc ├── details.txt ├── serializers.py ├── tests.py ├── urls.py └── views.py /__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MbuguaGeorge/Rest_API/66e6121f84f8a3d65da89fce2ec698833989982f/__init__.py -------------------------------------------------------------------------------- /__pycache__/__init__.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MbuguaGeorge/Rest_API/66e6121f84f8a3d65da89fce2ec698833989982f/__pycache__/__init__.cpython-38.pyc -------------------------------------------------------------------------------- /__pycache__/serializers.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MbuguaGeorge/Rest_API/66e6121f84f8a3d65da89fce2ec698833989982f/__pycache__/serializers.cpython-38.pyc -------------------------------------------------------------------------------- /__pycache__/tests.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MbuguaGeorge/Rest_API/66e6121f84f8a3d65da89fce2ec698833989982f/__pycache__/tests.cpython-38.pyc -------------------------------------------------------------------------------- /__pycache__/token.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MbuguaGeorge/Rest_API/66e6121f84f8a3d65da89fce2ec698833989982f/__pycache__/token.cpython-38.pyc -------------------------------------------------------------------------------- /__pycache__/urls.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MbuguaGeorge/Rest_API/66e6121f84f8a3d65da89fce2ec698833989982f/__pycache__/urls.cpython-38.pyc -------------------------------------------------------------------------------- /__pycache__/views.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MbuguaGeorge/Rest_API/66e6121f84f8a3d65da89fce2ec698833989982f/__pycache__/views.cpython-38.pyc -------------------------------------------------------------------------------- /details.txt: -------------------------------------------------------------------------------- 1 | api/movies 2 | -retrieves all movies 3 | -GET 4 | 5 | api/movies/ 6 | -retrieves a single movie specified by the id 7 | -GET, PUT & DELETE 8 | -for staff only 9 | 10 | api/users 11 | -retrieves all users 12 | -GET 13 | 14 | api/user/ 15 | -retrieves a specific user by id 16 | -GET, PUT & DELETE 17 | -for staff only 18 | 19 | api/token 20 | -takes a user's username and password and returns a token 21 | -POST -------------------------------------------------------------------------------- /serializers.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers 2 | from django.contrib.auth.models import User 3 | from iflix.models import Movie, User_Account 4 | 5 | class MovieSerializer(serializers.ModelSerializer): 6 | url = serializers.SerializerMethodField(read_only=True) 7 | class Meta: 8 | model = Movie 9 | fields = ('url','pk', 'movie_name', 'thumbnail', 'video_id') 10 | read_only_fields = ['pk'] 11 | 12 | def validate_movie_name(self, value): 13 | queryset = Movie.objects.filter(movie_name__iexact=value) 14 | if self.instance: 15 | queryset = queryset.exclude(pk=self.instance.pk) 16 | if queryset.exists(): 17 | raise serializers.ValidationError("This Movie exists in our database") 18 | return value 19 | 20 | def get_url(self, obj): 21 | request = self.context.get("request") 22 | return obj.get_api_url(request=request) 23 | 24 | class UserSerializer(serializers.ModelSerializer): 25 | class Meta: 26 | model = User 27 | fields = ('pk', 'username', 'email', 'date_joined', 'last_login') -------------------------------------------------------------------------------- /tests.py: -------------------------------------------------------------------------------- 1 | from rest_framework.test import APITestCase 2 | from django.contrib.auth import get_user_model 3 | from rest_framework.reverse import reverse as api_reverse 4 | from rest_framework import status 5 | 6 | from iflix.models import Movie 7 | 8 | User = get_user_model() 9 | 10 | class MovielistApiTest(APITestCase): 11 | def setUp(self): 12 | user = User(username='Martin', email='njoro@gmail.com') 13 | user.set_password=('njoroge026#') 14 | user.save() 15 | 16 | movies = Movie.objects.create( 17 | movie_name = 'grownish', 18 | thumbnail = 'http://127.0.0.1:8000/media/belgravia.jpg', 19 | video_id = 5 20 | ) 21 | 22 | def test_single_user(self): 23 | user_count = User.objects.count() 24 | self.assertEqual(user_count, 1) 25 | 26 | def test_single_movie(self): 27 | movie_count = Movie.objects.count() 28 | self.assertEqual(movie_count, 1) 29 | 30 | def test_get_list(self): 31 | data={} 32 | url = api_reverse("iflix_api:review") 33 | response = self.client.get(url, data, format='json') 34 | self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) 35 | -------------------------------------------------------------------------------- /urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path 2 | from api.views import movielist, moviecreation, userlist, usercreation, TokenView 3 | from api import views 4 | 5 | 6 | app_name = 'iflix_api' 7 | urlpatterns = [ 8 | path('movies//', views.movielist.as_view(), name='movie_s'), 9 | path('movies/', views.moviecreation.as_view(), name='review'), 10 | path('users/', views.userlist.as_view()), 11 | path('users//', views.usercreation.as_view(), name='user_s'), 12 | path('token/', views.TokenView.as_view()), 13 | ] -------------------------------------------------------------------------------- /views.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers, generics 2 | from rest_framework.views import APIView 3 | from api.serializers import MovieSerializer, UserSerializer 4 | from iflix.models import Movie 5 | from django.contrib.auth.models import User 6 | from rest_framework.response import Response 7 | from django.contrib.auth import authenticate 8 | from rest_framework.exceptions import PermissionDenied 9 | from rest_framework import status 10 | from rest_framework.permissions import IsAuthenticated, IsAdminUser 11 | from rest_framework.authtoken.models import Token 12 | 13 | class movielist(generics.RetrieveUpdateDestroyAPIView): 14 | lookup_field = 'pk' 15 | serializer_class = MovieSerializer 16 | permission_classes = [IsAdminUser] 17 | 18 | def get_queryset(self): 19 | return Movie.objects.all() 20 | 21 | def get_serializer_context(self, *args, **kwargs): 22 | return {"request": self.request} 23 | 24 | class moviecreation(generics.ListAPIView): 25 | lookup_field = 'pk' 26 | serializer_class = MovieSerializer 27 | permission_classes = [IsAuthenticated] 28 | 29 | def get_queryset(self): 30 | return Movie.objects.all() 31 | 32 | def get_serializer_context(self, *args, **kwargs): 33 | return {"request": self.request} 34 | 35 | class userlist(generics.ListAPIView): 36 | lookup_field = 'pk' 37 | serializer_class = UserSerializer 38 | permission_classes = [IsAuthenticated] 39 | 40 | def get_queryset(self): 41 | return User.objects.all() 42 | 43 | class usercreation(generics.RetrieveAPIView): 44 | lookup_field = 'pk' 45 | serializer_class = UserSerializer 46 | permission_classes = [IsAdminUser] 47 | 48 | def get_queryset(self): 49 | return User.objects.all() 50 | 51 | class TokenView(APIView): 52 | permission_classes = () 53 | def post(self, request,): 54 | username = request.data.get("username") 55 | password = request.data.get("password") 56 | user = authenticate(username=username, password=password) 57 | if user: 58 | token, _ = Token.objects.get_or_create(user=user) 59 | return Response({"token": user.auth_token.key}) 60 | else: 61 | return Response({"error": "Wrong Credentials"}, status=status.HTTP_400_BAD_REQUEST) 62 | --------------------------------------------------------------------------------