DRF_Template/backend/accounts/serializers.py

83 lines
3.3 KiB
Python
Raw Normal View History

2024-01-06 12:13:39 +08:00
from djoser.serializers import UserSerializer as BaseUserSerializer
from rest_framework.serializers import ModelSerializer
2024-01-06 12:13:39 +08:00
from rest_framework import serializers
from accounts.models import CustomUser
from drf_extra_fields.fields import Base64ImageField
from user_groups.serializers import SimpleUserGroupSerializer
from django.core.cache import cache
from django.core import exceptions as django_exceptions
2024-01-06 12:13:39 +08:00
from rest_framework.settings import api_settings
from django.contrib.auth.password_validation import validate_password
# There can be multiple subject instances with the same name, only differing in course, year level, and semester. We filter them here
class SimpleCustomUserSerializer(ModelSerializer):
class Meta(BaseUserSerializer.Meta):
model = CustomUser
fields = ('id', 'username', 'email', 'full_name')
2024-01-06 12:13:39 +08:00
class CustomUserSerializer(BaseUserSerializer):
avatar = Base64ImageField()
class Meta(BaseUserSerializer.Meta):
model = CustomUser
fields = ('id', 'username', 'email', 'avatar', 'first_name',
'last_name', 'user_group', 'group_member', 'group_owner')
read_only_fields = ('id', 'username', 'email', 'user_group',
'group_member', 'group_owner')
def to_representation(self, instance):
representation = super().to_representation(instance)
representation['user_group'] = SimpleUserGroupSerializer(
instance.user_group, many=False).data
return representation
def update(self, instance, validated_data):
cache.delete(f'user:{instance.id}')
return super().update(instance, validated_data)
2024-01-06 12:13:39 +08:00
class UserRegistrationSerializer(serializers.ModelSerializer):
email = serializers.EmailField(required=True)
username = serializers.CharField(required=True)
2024-01-06 12:13:39 +08:00
password = serializers.CharField(
write_only=True, style={'input_type': 'password', 'placeholder': 'Password'})
first_name = serializers.CharField(
required=True, allow_blank=False, allow_null=False)
last_name = serializers.CharField(
required=True, allow_blank=False, allow_null=False)
2024-01-06 12:13:39 +08:00
class Meta:
model = CustomUser
fields = ['email', 'username', 'password',
'first_name', 'last_name']
2024-01-06 12:13:39 +08:00
def validate(self, attrs):
user_attrs = attrs.copy()
user = self.Meta.model(**user_attrs)
2024-01-06 12:13:39 +08:00
password = attrs.get("password")
2024-01-06 12:13:39 +08:00
try:
validate_password(password, user)
except django_exceptions.ValidationError as e:
serializer_error = serializers.as_serializer_error(e)
errors = serializer_error[api_settings.NON_FIELD_ERRORS_KEY]
if len(errors) > 1:
raise serializers.ValidationError({"password": errors[0]})
else:
raise serializers.ValidationError({"password": errors})
if self.Meta.model.objects.filter(username=attrs.get("username")).exists():
2024-01-06 12:13:39 +08:00
raise serializers.ValidationError(
"A user with that username already exists.")
2024-01-06 12:13:39 +08:00
return super().validate(attrs)
def create(self, validated_data):
user = self.Meta.model(**validated_data)
user.username = validated_data['username']
user.is_active = False
2024-01-06 12:13:39 +08:00
user.set_password(validated_data['password'])
user.save()
return user