generated from codersforcauses/django-vue-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 12 - Created api view and serializer for registering users. (#41)
* Created api view and serializer for registering users. * Added additional tests and validators. Moved bingo urls into bingo/ * Moved validation into serializer.
- Loading branch information
1 parent
19d687f
commit c98fc9a
Showing
5 changed files
with
100 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from rest_framework import serializers | ||
from django.contrib.auth import get_user_model | ||
from django.contrib.auth.password_validation import validate_password | ||
|
||
User = get_user_model() | ||
|
||
|
||
class UserRegisterSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = User | ||
fields = ['username', 'first_name', 'last_name', 'email', 'password'] | ||
extra_kwargs = { | ||
'password': {'write_only': True}, | ||
'first_name': {'required': True}, | ||
'last_name': {'required': True} | ||
} | ||
|
||
def create(self, validated_data): | ||
user = User.objects.create_user( | ||
username=validated_data['username'], | ||
first_name=validated_data['first_name'], | ||
last_name=validated_data['last_name'], | ||
email=validated_data['email'], | ||
password=validated_data['password'], | ||
) | ||
return user | ||
|
||
def validate_password(self, value): | ||
validate_password(value) | ||
return value |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from django.urls import path | ||
|
||
from rest_framework_simplejwt.views import ( | ||
TokenObtainPairView, | ||
TokenRefreshView, | ||
) | ||
from bingo import views | ||
|
||
urlpatterns = [ | ||
path('token/', TokenObtainPairView.as_view(), name='token_obtain_pair'), | ||
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'), | ||
path('register/', views.register_user, name='register_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,15 @@ | ||
from django.shortcuts import render # noqa | ||
from rest_framework import status | ||
from rest_framework.decorators import api_view | ||
from rest_framework.response import Response | ||
from .serializers import UserRegisterSerializer | ||
|
||
# Create your views here. | ||
|
||
|
||
@api_view(['POST']) | ||
def register_user(request): | ||
serializer = UserRegisterSerializer(data=request.data) | ||
if serializer.is_valid(): | ||
serializer.save() | ||
return Response({'User created successfully.'}, status=status.HTTP_201_CREATED) | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |