2024-01-06 12:13:39 +08:00
|
|
|
from djoser.serializers import UserSerializer as BaseUserSerializer
|
2024-05-10 23:15:29 +08:00
|
|
|
from rest_framework.serializers import ModelSerializer
|
2024-01-06 12:13:39 +08:00
|
|
|
from rest_framework import serializers
|
|
|
|
from accounts.models import CustomUser
|
2024-05-10 23:15:29 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2024-05-10 23:15:29 +08:00
|
|
|
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',
|
2024-05-10 23:15:29 +08:00
|
|
|
'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)
|
2024-05-10 23:15:29 +08:00
|
|
|
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'})
|
2024-05-10 23:15:29 +08:00
|
|
|
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:
|
2024-05-10 23:15:29 +08:00
|
|
|
model = CustomUser
|
|
|
|
fields = ['email', 'username', 'password',
|
|
|
|
'first_name', 'last_name']
|
2024-01-06 12:13:39 +08:00
|
|
|
|
|
|
|
def validate(self, attrs):
|
2024-05-10 23:15:29 +08:00
|
|
|
user_attrs = attrs.copy()
|
|
|
|
user = self.Meta.model(**user_attrs)
|
2024-01-06 12:13:39 +08:00
|
|
|
password = attrs.get("password")
|
2024-05-10 23:15:29 +08:00
|
|
|
|
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)
|
2024-05-10 23:15:29 +08:00
|
|
|
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(
|
2024-05-10 23:15:29 +08:00
|
|
|
"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)
|
2024-05-10 23:15:29 +08:00
|
|
|
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
|