Skip to content

Commit

Permalink
Merge pull request #7 from Abramov0Alexandr/feature
Browse files Browse the repository at this point in the history
Доработал логику регистрации для продавцов, вынес проверку паролей в …
  • Loading branch information
Abramov0Alexandr authored Sep 26, 2023
2 parents ae91e7d + 49d28e5 commit 708b4bf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
14 changes: 6 additions & 8 deletions custom_user/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from rest_framework import serializers
from custom_user.models import CustomUser
from custom_user.validators import PasswordValidation


class CustomersListSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -28,6 +29,7 @@ class VisitorSerializer(serializers.ModelSerializer):
class Meta:
model = CustomUser
fields = ('first_name', 'last_name', 'patronymic', 'email', 'password', 'password_confirmation', )
validators = [PasswordValidation()]

def create(self, validated_data):
"""
Expand All @@ -38,13 +40,6 @@ def create(self, validated_data):
"""

password_confirmation = validated_data.pop('password_confirmation', None)

if password_confirmation is None:
raise serializers.ValidationError("Поле 'password_confirmation' обязательно")

if validated_data.get('password') != password_confirmation:
raise serializers.ValidationError("Пароль и его подтверждение не совпадают")

new_common_user = CustomUser.objects.create_user(**validated_data)
return new_common_user

Expand All @@ -60,11 +55,14 @@ class SellerSerializer(serializers.ModelSerializer):

shop_name = serializers.CharField()
product_images = serializers.ImageField(required=False)
password_confirmation = serializers.CharField(write_only=True)

class Meta:
model = CustomUser
fields = ('shop_name', 'product_images', 'email', )
fields = ('shop_name', 'product_images', 'email', 'password', 'password_confirmation', )
validators = [PasswordValidation()]

def create(self, validated_data):
password_confirmation = validated_data.pop('password_confirmation', None)
new_seller = CustomUser.objects.create_user(**validated_data)
return new_seller
14 changes: 14 additions & 0 deletions custom_user/validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from rest_framework.validators import ValidationError


class PasswordValidation:

def __call__(self, data: dict):
password_confirmation = data.pop('password_confirmation', None)
checking_password = data.get('password')

if password_confirmation is None:
raise ValidationError("Поле 'password_confirmation' обязательно")

if checking_password != password_confirmation:
raise ValidationError("Пароль и его подтверждение не совпадают")

0 comments on commit 708b4bf

Please sign in to comment.