-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from krish-patel1003/main
Added UserViewset and LoginViewset
- Loading branch information
Showing
10 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from rest_framework import permissions | ||
|
||
class UpdateOwnUserInfo(permissions.BasePermission): | ||
"""Allow users to eddit their own user info""" | ||
|
||
def has_object_permission(self, request, view, obj): | ||
"""Check user is trying to edit their own user info""" | ||
|
||
if request.method in permissions.SAFE_METHODS: | ||
"""Allows users to view data but not edit""" | ||
return True | ||
|
||
return obj.id == request.user.id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from rest_framework import serializers | ||
|
||
from .models import User | ||
|
||
|
||
class UserSerializer(serializers.ModelSerializer): | ||
"""A serializer for our user profile objects""" | ||
|
||
class Meta: | ||
model = User | ||
fields = ('id', 'email', 'name', 'password') | ||
extra_kwargs = { | ||
'password':{'write_only': True} | ||
} | ||
|
||
def create(self, validated_data): | ||
"""Create and return a new user""" | ||
user = User( | ||
email=validated_data['email'], | ||
name=validated_data['name'], | ||
|
||
) | ||
|
||
user.set_password(validated_data['password']) | ||
user.save() | ||
|
||
return user | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
from django.urls import path | ||
from django.urls import path, include | ||
from rest_framework.routers import DefaultRouter | ||
|
||
urlpatterns = [] | ||
from .views import LoginViewSet, UserViewSet | ||
|
||
router = DefaultRouter() | ||
router.register('users', UserViewSet) | ||
router.register('login', LoginViewSet, basename='login') | ||
|
||
urlpatterns = [ | ||
path(r'', include(router.urls), name='users'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,33 @@ | ||
import imp | ||
from django.shortcuts import render | ||
|
||
from rest_framework import filters | ||
from rest_framework import viewsets | ||
from rest_framework.authentication import TokenAuthentication | ||
from rest_framework.authtoken.serializers import AuthTokenSerializer | ||
from rest_framework.authtoken.views import ObtainAuthToken | ||
|
||
from .permissions import UpdateOwnUserInfo | ||
from .models import User | ||
from .serializers import UserSerializer | ||
|
||
# Create your views here. | ||
class UserViewSet(viewsets.ModelViewSet): | ||
"""Handles reading, creating, and updating User""" | ||
|
||
serializer_class = UserSerializer | ||
queryset = User.objects.all() | ||
authentication_classes = (TokenAuthentication,) | ||
permission_classes = (UpdateOwnUserInfo, ) | ||
filter_backends = (filters.SearchFilter,) | ||
search_fields = ('name', 'email',) | ||
|
||
class LoginViewSet(viewsets.ViewSet): | ||
"""Checks email and password and reurns an auth token""" | ||
|
||
serializer_class = AuthTokenSerializer | ||
|
||
def create(self, request): | ||
"""Use the ObtainAuthToken to validate and create a token""" | ||
|
||
return ObtainAuthToken().as_view()(request=request._request) |
Binary file not shown.
Binary file not shown.