-
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.
Прописал логику регистрации обычного пользователя и продавца
- Loading branch information
1 parent
60ef425
commit 9bf94d7
Showing
3 changed files
with
72 additions
and
21 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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
from django.urls import path | ||
from .apps import CustomUserConfig | ||
from .views import CustomUserListView, CustomUserCreateView | ||
from .views import CustomersListView, CustomerCreateView | ||
|
||
|
||
app_name = CustomUserConfig.name | ||
|
||
|
||
urlpatterns = [ | ||
path('', CustomUserListView.as_view(), name='custom_user_list'), | ||
path('create/', CustomUserCreateView.as_view(), name='custom_user_create') | ||
path('', CustomersListView.as_view(), name='custom_user_list'), | ||
path('create/', CustomerCreateView.as_view(), name='custom_user_create') | ||
] |
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,20 +1,47 @@ | ||
from django_filters.rest_framework import DjangoFilterBackend | ||
from rest_framework import generics | ||
from rest_framework import generics, status | ||
from rest_framework.permissions import IsAdminUser | ||
from rest_framework.response import Response | ||
from custom_user.models import CustomUser | ||
from custom_user.serializers import CustomUserListSerializer, CustomUserCreateSerializer | ||
from custom_user.serializers import CustomersListSerializer, VisitorSerializer, SellerSerializer | ||
|
||
|
||
class CustomUserListView(generics.ListAPIView): | ||
"""Контроллер для отображения списка зарегистрированных пользователей""" | ||
class CustomersListView(generics.ListAPIView): | ||
"""Контроллер для отображения списка зарегистрированных пользователей.""" | ||
|
||
serializer_class = CustomUserListSerializer | ||
serializer_class = CustomersListSerializer | ||
queryset = CustomUser.objects.all() | ||
filter_backends = [DjangoFilterBackend] | ||
filterset_fields = ('is_seller', ) | ||
permission_classes = IsAdminUser | ||
|
||
|
||
class CustomUserCreateView(generics.CreateAPIView): | ||
"""Контроллер для регистрации новых пользователей""" | ||
class CustomerCreateView(generics.CreateAPIView): | ||
"""Контроллер для регистрации новых пользователей.""" | ||
|
||
queryset = CustomUser.objects.all() | ||
serializer_class = CustomUserCreateSerializer | ||
def get_serializer_class(self): | ||
""" | ||
Метод переопределен для выбора конкретного сериализатора в зависимости от переданных в POST запросе данных. | ||
""" | ||
|
||
is_seller = self.request.data.get('is_seller', None) | ||
|
||
if is_seller: | ||
return SellerSerializer | ||
|
||
return VisitorSerializer | ||
|
||
def create(self, request, *args, **kwargs): | ||
is_seller = request.data.get('is_seller', False) | ||
serializer = self.get_serializer(data=request.data) | ||
|
||
if serializer.is_valid(): | ||
if is_seller: | ||
# Если выбран статус продавца, устанавливаем его | ||
serializer.save(is_seller=CustomUser.UserStatus.SELLER) | ||
|
||
else: | ||
# Если выбран статус посетителя, оставляем его по умолчанию | ||
serializer.save() | ||
return Response(serializer.data, status=status.HTTP_201_CREATED) | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |