From ce90491e87c923356760977ff883491521830ed2 Mon Sep 17 00:00:00 2001 From: Hubert Jankowski Date: Sun, 12 Dec 2021 23:04:19 +0100 Subject: [PATCH] Registration --- config/urls.py | 2 +- users/serializers.py | 28 ++++++++++++++++++++++++++++ users/urls.py | 4 +++- users/views.py | 15 ++++++++++++++- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/config/urls.py b/config/urls.py index 8f3d25f..212ac1d 100644 --- a/config/urls.py +++ b/config/urls.py @@ -23,7 +23,7 @@ urlpatterns = [ path('login', login), path('logout', logout), path('register', register), - path('users', include("users.urls")), + path('users/', include("users.urls")), path('questions/', include("questions.urls")), path('answers/', include("answers.urls")), path('tests/', include("trials.urls")), diff --git a/users/serializers.py b/users/serializers.py index cc2725c..beefa60 100644 --- a/users/serializers.py +++ b/users/serializers.py @@ -5,6 +5,34 @@ from users import cons from tools.tools import decode_uid, encode_uid +class RegisterSerializer(serializers.ModelSerializer): + password = serializers.CharField( + write_only=True, + required=False, + min_length=8, + style={"input_type": "password"}, + ) + email = serializers.CharField( + write_only=True + ) + first_name = serializers.CharField( + write_only=True + ) + last_name = serializers.CharField( + write_only=True + ) + #todo + # avatar = serializers.ImageField(allow_empty_file=True, source="profile.avatar", read_only=True) + + class Meta: + model = User + fields = ( + "email", + "first_name", + "last_name", + "password" + ) + class UserSerializer(serializers.ModelSerializer): password = serializers.CharField( diff --git a/users/urls.py b/users/urls.py index d377d91..4cf6597 100644 --- a/users/urls.py +++ b/users/urls.py @@ -4,14 +4,16 @@ from django.urls import path from users.views import UserModelViewSet from rest_framework_simplejwt.views import TokenObtainPairView from rest_framework_simplejwt.views import TokenRefreshView -from .views import PasswordReset, UserPasswordResetConfirmView +from .views import PasswordReset, UserPasswordResetConfirmView, RegisterViewSet router = DefaultRouter(trailing_slash=False) router.register("items", UserModelViewSet) +# router.register("register", RegisterViewSet, basename="pyk") urlpatterns = [ path("", include(router.urls)), + path('register', RegisterViewSet.as_view(), name='register'), path('api/token', TokenObtainPairView.as_view(), name='token_obtain_pair'), path('api/token/refresh', TokenRefreshView.as_view(), name='token_refresh'), path("password/reset", PasswordReset.as_view()), diff --git a/users/views.py b/users/views.py index 45088d3..1fb1104 100644 --- a/users/views.py +++ b/users/views.py @@ -5,7 +5,8 @@ from users.serializers import UserSerializer from rest_framework import views, exceptions, status, viewsets, permissions, generics from tools.tools import PasswordResetShortcut, PasswordResetConfirmShortcut from django.contrib.auth.tokens import default_token_generator -from .serializers import UserPasswordResetSerializer, UserPasswordResetConfirmSerializer +from .serializers import UserPasswordResetSerializer, UserPasswordResetConfirmSerializer, RegisterSerializer +from rest_framework.response import Response class UserModelViewSet(viewsets.ModelViewSet): @@ -13,6 +14,18 @@ class UserModelViewSet(viewsets.ModelViewSet): serializer_class = UserSerializer +class RegisterViewSet(generics.GenericAPIView): + serializer_class = RegisterSerializer + permission_classes = (permissions.AllowAny, ) + + def post(self, request): + serializer = self.get_serializer(data=request.data) + if serializer.is_valid(): + return Response({"detail": "Done"}, status=status.HTTP_200_OK) + else: + return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + class PasswordReset(PasswordResetShortcut, generics.GenericAPIView): """ ```json