serialzer user
This commit is contained in:
@@ -29,3 +29,7 @@ Lista de la compra
|
||||
#### Commit 6
|
||||
|
||||
- 01:12:28
|
||||
|
||||
#### Commit 7
|
||||
|
||||
- 01:31:15
|
||||
|
||||
18
authentication/serializers.py
Normal file
18
authentication/serializers.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from .models import User
|
||||
from rest_framework import serializers
|
||||
|
||||
|
||||
class UserCreationSerializer(serializers.ModelSerializer):
|
||||
email = serializers.EmailField(allow_null=False, allow_blank=False)
|
||||
password = serializers.CharField(min_length=8)
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ['email', 'password']
|
||||
|
||||
def validate(self, attrs):
|
||||
email_exists = User.objects.filter(email=attrs['email']).exists()
|
||||
if email_exists:
|
||||
raise serializers.ValidationError(detail='El email es necesario')
|
||||
attrs['username'] = attrs['email']
|
||||
return super().validate(attrs)
|
||||
@@ -3,4 +3,5 @@ from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('', views.HelloAuthView.as_view(), name='hello_auth'),
|
||||
path('registro/', views.UserCreateView.as_view(), name='crear_user')
|
||||
]
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
# from django.shortcuts import render
|
||||
from rest_framework import generics, status
|
||||
from rest_framework.response import Response
|
||||
from .models import User
|
||||
from . import serializers
|
||||
|
||||
# Create your views here.
|
||||
|
||||
@@ -10,3 +12,18 @@ class HelloAuthView(generics.GenericAPIView):
|
||||
def get(self, request):
|
||||
return Response(data={"message": "Hello Auth"},
|
||||
status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
class UserCreateView(generics.GenericAPIView):
|
||||
serializer_class = serializers.UserCreationSerializer
|
||||
|
||||
def post(self, request):
|
||||
data = request.data
|
||||
serializer = self.serializer_class(data=data)
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
return Response(data=serializer.data,
|
||||
status=status.HTTP_201_CREATED)
|
||||
|
||||
return Response(data=serializer.errors,
|
||||
status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
Reference in New Issue
Block a user