From c761e8751e9670196d9eea46c8d1f15faa194102 Mon Sep 17 00:00:00 2001 From: clonbg Date: Mon, 20 Feb 2023 08:04:59 +0100 Subject: [PATCH] serialzer user --- README.md | 4 ++++ authentication/serializers.py | 18 ++++++++++++++++++ authentication/urls.py | 1 + authentication/views.py | 17 +++++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 authentication/serializers.py diff --git a/README.md b/README.md index eea4cc0..288cd43 100644 --- a/README.md +++ b/README.md @@ -29,3 +29,7 @@ Lista de la compra #### Commit 6 - 01:12:28 + +#### Commit 7 + +- 01:31:15 diff --git a/authentication/serializers.py b/authentication/serializers.py new file mode 100644 index 0000000..f7fcfec --- /dev/null +++ b/authentication/serializers.py @@ -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) diff --git a/authentication/urls.py b/authentication/urls.py index 8a86c17..35c1b32 100644 --- a/authentication/urls.py +++ b/authentication/urls.py @@ -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') ] diff --git a/authentication/views.py b/authentication/views.py index 38bb4e1..59b87b5 100644 --- a/authentication/views.py +++ b/authentication/views.py @@ -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)