Skip to content

Commit 60706fe

Browse files
author
Juan Benitez
committed
improving some views and authentication methods
1 parent 6877411 commit 60706fe

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

api_crud/authentication.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from rest_framework import authentication
2+
3+
4+
class JWTAuthentication(authentication.TokenAuthentication):
5+
6+
keyword = 'JWT'

api_crud/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
REST_FRAMEWORK = {
3131
'DEFAULT_AUTHENTICATION_CLASSES': (
32-
'rest_framework.authentication.TokenAuthentication',
32+
'api_crud.authentication.JWTAuthentication',
3333
)
3434
}
3535

movies/permissions.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from rest_framework import permissions
2+
from rest_framework.exceptions import PermissionDenied
23

34

45
class IsOwnerOrReadOnly(permissions.BasePermission):
@@ -13,4 +14,19 @@ def has_object_permission(self, request, view, obj):
1314
return True
1415

1516
# Write permissions are only allowed to the creator of the movie
16-
return obj.creator == request.user
17+
return obj.creator == request.user
18+
19+
20+
class IsAuthenticated(permissions.BasePermission):
21+
"""
22+
Allows access only to authenticated users.
23+
"""
24+
print("df")
25+
26+
def has_permission(self, request, view):
27+
message = 'No estas autenticado mi vale!'
28+
is_it = bool(request.user and request.user.is_authenticated)
29+
if is_it:
30+
return is_it
31+
else:
32+
raise PermissionDenied(detail=message)

movies/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from rest_framework.decorators import api_view, permission_classes
2-
from rest_framework.permissions import IsAuthenticated
32
from rest_framework.response import Response
43
from rest_framework import status
54
from rest_framework.parsers import JSONParser
65
from .models import Movie
76
from .serializers import MovieSerializer
8-
from .permissions import IsOwnerOrReadOnly
7+
from .permissions import IsOwnerOrReadOnly, IsAuthenticated
98

109

1110
@api_view(['GET', 'DELETE', 'PUT']) # Methods Allowed
@@ -54,6 +53,7 @@ def get_delete_update_movie(request, pk): #pk es PrimaryKey(Id)
5453
@api_view(['GET', 'POST'])
5554
@permission_classes((IsAuthenticated, ))
5655
def get_post_movies(request):
56+
print('adasd')
5757
# get all movies
5858
if request.method == 'GET':
5959
puppies = Movie.objects.all()

0 commit comments

Comments
 (0)