2023-06-26 19:45:58 +08:00
|
|
|
from djoser.serializers import UserCreateSerializer as BaseUserRegistrationSerializer
|
|
|
|
from djoser.serializers import UserSerializer as BaseUserSerializer
|
2023-07-03 21:22:06 +08:00
|
|
|
from django.core import exceptions as django_exceptions
|
|
|
|
from rest_framework import exceptions as drf_exceptions
|
2023-06-28 17:12:14 +08:00
|
|
|
from rest_framework import serializers
|
2023-06-26 19:45:58 +08:00
|
|
|
from accounts.models import CustomUser
|
2023-06-26 22:06:05 +08:00
|
|
|
from student_status.serializers import StudentStatusSerializer
|
2023-06-26 23:50:55 +08:00
|
|
|
from student_status.models import StudentStatus
|
2023-07-03 21:22:06 +08:00
|
|
|
from rest_framework.settings import api_settings
|
|
|
|
from django.contrib.auth.password_validation import validate_password
|
2023-06-26 19:45:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CustomUserSerializer(BaseUserSerializer):
|
2023-06-26 22:06:05 +08:00
|
|
|
user_status = StudentStatusSerializer(
|
|
|
|
source='studentstatus', read_only=True)
|
|
|
|
|
2023-06-26 19:45:58 +08:00
|
|
|
class Meta(BaseUserSerializer.Meta):
|
|
|
|
model = CustomUser
|
2023-07-04 20:34:12 +08:00
|
|
|
fields = ('username', 'email',
|
2023-06-28 17:12:14 +08:00
|
|
|
'student_id_number', 'year_level', 'semester', 'course', 'subjects', 'avatar', 'first_name', 'last_name', 'is_banned', 'user_status')
|
2023-06-26 19:45:58 +08:00
|
|
|
|
2023-07-03 21:22:06 +08:00
|
|
|
# The model from your custom user
|
2023-06-26 19:45:58 +08:00
|
|
|
|
2023-07-03 21:22:06 +08:00
|
|
|
|
|
|
|
class UserRegistrationSerializer(serializers.ModelSerializer):
|
|
|
|
email = serializers.EmailField(required=True)
|
|
|
|
student_id_number = serializers.CharField(required=True)
|
|
|
|
password = serializers.CharField(write_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = CustomUser # Use your custom user model here
|
|
|
|
fields = ('username', 'email', 'password', 'student_id_number',
|
|
|
|
'year_level', 'semester', 'course', 'subjects', 'avatar',
|
|
|
|
'first_name', 'last_name')
|
|
|
|
|
|
|
|
def validate(self, attrs):
|
|
|
|
user = self.Meta.model(**attrs)
|
|
|
|
password = attrs.get("password")
|
|
|
|
try:
|
|
|
|
validate_password(password, user)
|
|
|
|
except django_exceptions.ValidationError as e:
|
|
|
|
serializer_error = serializers.as_serializer_error(e)
|
|
|
|
raise serializers.ValidationError(
|
|
|
|
{"password": serializer_error[api_settings.NON_FIELD_ERRORS_KEY]}
|
|
|
|
)
|
|
|
|
|
|
|
|
return super().validate(attrs)
|
2023-06-26 23:50:55 +08:00
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
user = self.Meta.model(**validated_data)
|
2023-07-03 21:22:06 +08:00
|
|
|
user.set_password(validated_data['password'])
|
|
|
|
user.save()
|
2023-06-26 23:50:55 +08:00
|
|
|
|
2023-06-26 23:56:51 +08:00
|
|
|
StudentStatus.objects.create(
|
2023-06-26 23:50:55 +08:00
|
|
|
user=user,
|
|
|
|
active=False,
|
|
|
|
x=None,
|
|
|
|
y=None,
|
|
|
|
subject=None
|
|
|
|
)
|
|
|
|
return user
|