-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a64b197
commit 3ba18d5
Showing
7 changed files
with
108 additions
and
2 deletions.
There are no files selected for viewing
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
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
Empty file.
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,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class UserConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = '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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from django.contrib.auth import ( | ||
get_user_model, | ||
authenticate, | ||
) | ||
from django.utils.translation import gettext as _ | ||
|
||
from rest_framework import serializers | ||
|
||
|
||
class UserSerializer(serializers.ModelSerializer): | ||
"""Serializer for the user object.""" | ||
|
||
class Meta: | ||
model = get_user_model() | ||
fields = ['email', 'password', 'name'] | ||
extra_kwargs = {'password': {'write_only': True, 'min_length': 5}} | ||
|
||
def create(self, validated_data): | ||
"""Create and return a user with encrypted password.""" | ||
return get_user_model().objects.create_user(**validated_data) | ||
|
||
def update(self, instance, validated_data): | ||
"""Update and return user.""" | ||
password = validated_data.pop('password', None) | ||
user = super().update(instance, validated_data) | ||
|
||
if password: | ||
user.set_password(password) | ||
user.save() | ||
|
||
return user | ||
|
||
|
||
class AuthTokenSerializer(serializers.Serializer): | ||
"""Serializer for the user auth token.""" | ||
email = serializers.EmailField() | ||
password = serializers.CharField( | ||
style={'input_type': 'password'}, | ||
trim_whitespace=False, | ||
) | ||
|
||
def validate(self, attrs): | ||
"""Validate and authenticate the user.""" | ||
email = attrs.get('email') | ||
password = attrs.get('password') | ||
user = authenticate( | ||
request=self.context.get('request'), | ||
username=email, | ||
password=password, | ||
) | ||
if not user: | ||
msg = _('Unable to authenticate with provided credentials.') | ||
raise serializers.ValidationError(msg, code='authorization') | ||
|
||
attrs['user'] = user | ||
return attrs |
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,12 @@ | ||
from django.urls import path | ||
|
||
from user import views | ||
|
||
|
||
app_name = 'user' | ||
|
||
urlpatterns = [ | ||
path('create/', views.CreateUserView.as_view(), name='create'), | ||
path('token/', views.CreateTokenView.as_view(), name='token'), | ||
path('me/', views.ManageUserView.as_view(), name='me'), | ||
] |
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,29 @@ | ||
from rest_framework import generics, authentication, permissions | ||
from rest_framework.authtoken.views import ObtainAuthToken | ||
from rest_framework.settings import api_settings | ||
from user.serializers import ( | ||
UserSerializer, | ||
AuthTokenSerializer, | ||
) | ||
|
||
|
||
class CreateUserView(generics.CreateAPIView): | ||
"""Create a new user in the system.""" | ||
serializer_class = UserSerializer | ||
|
||
|
||
class CreateTokenView(ObtainAuthToken): | ||
"""Create a new auth token for user.""" | ||
serializer_class = AuthTokenSerializer | ||
renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES | ||
|
||
|
||
class ManageUserView(generics.RetrieveUpdateAPIView): | ||
"""Manage the authenticated user.""" | ||
serializer_class = UserSerializer | ||
authentication_classes = [authentication.TokenAuthentication] | ||
permission_classes = [permissions.IsAuthenticated] | ||
|
||
def get_object(self): | ||
"""Retrieve and return the authenticated user.""" | ||
return self.request.user |