2023-08-17 15:43:39 +03:00
|
|
|
''' Serializers: User profile and Authentification. '''
|
2023-07-15 17:46:19 +03:00
|
|
|
from django.contrib.auth import authenticate
|
|
|
|
from django.contrib.auth.password_validation import validate_password
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
2023-08-26 17:26:49 +03:00
|
|
|
from apps.rsform.models import Subscription
|
2023-07-15 17:46:19 +03:00
|
|
|
from . import models
|
|
|
|
|
|
|
|
|
|
|
|
class LoginSerializer(serializers.Serializer):
|
2023-08-17 15:43:39 +03:00
|
|
|
''' Serializer: User authentification by login/password. '''
|
2023-07-15 17:46:19 +03:00
|
|
|
username = serializers.CharField(
|
|
|
|
label='Имя пользователя',
|
|
|
|
write_only=True
|
|
|
|
)
|
|
|
|
password = serializers.CharField(
|
|
|
|
label='Пароль',
|
|
|
|
style={'input_type': 'password'},
|
|
|
|
trim_whitespace=False,
|
|
|
|
write_only=True
|
|
|
|
)
|
|
|
|
|
|
|
|
def validate(self, attrs):
|
|
|
|
username = attrs.get('username')
|
|
|
|
password = attrs.get('password')
|
2023-07-27 22:04:25 +03:00
|
|
|
user = authenticate(
|
|
|
|
request=self.context.get('request'),
|
|
|
|
username=username,
|
|
|
|
password=password
|
|
|
|
)
|
|
|
|
if not user:
|
|
|
|
msg = 'Неправильное сочетание имени пользователя и пароля.'
|
2023-07-15 17:46:19 +03:00
|
|
|
raise serializers.ValidationError(msg, code='authorization')
|
|
|
|
attrs['user'] = user
|
|
|
|
return attrs
|
|
|
|
|
2023-08-17 15:43:39 +03:00
|
|
|
def create(self, validated_data):
|
|
|
|
raise NotImplementedError('unexpected `create()` call')
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
raise NotImplementedError('unexpected `update()` call')
|
|
|
|
|
2023-07-15 17:46:19 +03:00
|
|
|
|
2023-08-26 17:26:49 +03:00
|
|
|
class AuthSerializer(serializers.Serializer):
|
2023-08-17 15:43:39 +03:00
|
|
|
''' Serializer: Authentication data. '''
|
2023-08-26 17:26:49 +03:00
|
|
|
def to_representation(self, instance: models.User) -> dict:
|
|
|
|
if instance.is_anonymous:
|
|
|
|
return {
|
|
|
|
'id': None,
|
|
|
|
'username': '',
|
|
|
|
'is_staff': False,
|
|
|
|
'subscriptions': []
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
return {
|
|
|
|
'id': instance.pk,
|
|
|
|
'username': instance.username,
|
|
|
|
'is_staff': instance.is_staff,
|
|
|
|
'subscriptions': [sub.item.pk for sub in Subscription.objects.filter(user=instance)]
|
|
|
|
}
|
2023-07-15 17:46:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
class UserInfoSerializer(serializers.ModelSerializer):
|
2023-08-17 15:43:39 +03:00
|
|
|
''' Serializer: User data. '''
|
2023-07-15 17:46:19 +03:00
|
|
|
class Meta:
|
2023-08-17 15:43:39 +03:00
|
|
|
''' serializer metadata. '''
|
2023-07-15 17:46:19 +03:00
|
|
|
model = models.User
|
|
|
|
fields = [
|
|
|
|
'id',
|
|
|
|
'username',
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class UserSerializer(serializers.ModelSerializer):
|
2023-08-17 15:43:39 +03:00
|
|
|
''' Serializer: User data. '''
|
2023-07-15 17:46:19 +03:00
|
|
|
id = serializers.IntegerField(read_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
2023-08-17 15:43:39 +03:00
|
|
|
''' serializer metadata. '''
|
2023-07-15 17:46:19 +03:00
|
|
|
model = models.User
|
|
|
|
fields = [
|
|
|
|
'id',
|
|
|
|
'username',
|
|
|
|
'email',
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
]
|
|
|
|
|
2023-08-17 15:43:39 +03:00
|
|
|
|
2023-08-10 13:53:19 +03:00
|
|
|
class ChangePasswordSerializer(serializers.Serializer):
|
2023-08-17 15:43:39 +03:00
|
|
|
''' Serializer: Change password. '''
|
2023-08-10 13:53:19 +03:00
|
|
|
old_password = serializers.CharField(required=True)
|
|
|
|
new_password = serializers.CharField(required=True)
|
|
|
|
|
2023-08-17 15:43:39 +03:00
|
|
|
def create(self, validated_data):
|
|
|
|
raise NotImplementedError('unexpected `create()` call')
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
raise NotImplementedError('unexpected `update()` call')
|
|
|
|
|
2023-07-15 17:46:19 +03:00
|
|
|
|
|
|
|
class SignupSerializer(serializers.ModelSerializer):
|
2023-08-17 15:43:39 +03:00
|
|
|
''' Serializer: Create user profile. '''
|
2023-07-15 17:46:19 +03:00
|
|
|
id = serializers.IntegerField(read_only=True)
|
|
|
|
password = serializers.CharField(write_only=True, required=True, validators=[validate_password])
|
|
|
|
password2 = serializers.CharField(write_only=True, required=True)
|
|
|
|
|
|
|
|
class Meta:
|
2023-08-17 15:43:39 +03:00
|
|
|
''' serializer metadata. '''
|
2023-07-15 17:46:19 +03:00
|
|
|
model = models.User
|
|
|
|
fields = [
|
|
|
|
'id',
|
|
|
|
'username',
|
|
|
|
'email',
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'password',
|
|
|
|
'password2'
|
|
|
|
]
|
|
|
|
|
|
|
|
def validate(self, attrs):
|
|
|
|
if attrs['password'] != attrs['password2']:
|
|
|
|
raise serializers.ValidationError({"password": "Введенные пароли не совпадают"})
|
|
|
|
return attrs
|
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
user = models.User.objects.create_user(
|
|
|
|
validated_data['username'], validated_data['email'], validated_data['password']
|
|
|
|
)
|
|
|
|
user.first_name = validated_data['first_name']
|
|
|
|
user.last_name = validated_data['last_name']
|
|
|
|
user.save()
|
|
|
|
return user
|