StudE-Backend/stude/accounts/serializers.py

40 lines
1.5 KiB
Python
Raw Normal View History

2023-06-26 19:45:58 +08:00
from djoser.serializers import UserCreateSerializer as BaseUserRegistrationSerializer
from djoser.serializers import UserSerializer as BaseUserSerializer
from accounts.models import CustomUser
2023-06-26 22:06:05 +08:00
from student_status.serializers import StudentStatusSerializer
from student_status.models import StudentStatus
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-06-26 22:06:05 +08:00
fields = ('username', 'email', 'password',
2023-06-27 13:40:31 +08:00
'student_id_number', 'year_level', 'semester', 'course', 'avatar', 'first_name', 'last_name', 'is_banned', 'user_status')
2023-06-26 19:45:58 +08:00
class UserRegistrationSerializer(BaseUserRegistrationSerializer):
class Meta(BaseUserRegistrationSerializer.Meta):
2023-06-26 20:36:58 +08:00
fields = ('username', 'email', 'password',
2023-06-27 13:40:31 +08:00
'student_id_number', 'year_level', 'semester', 'course', 'avatar', 'first_name', 'last_name')
def create(self, validated_data):
# Get the user's year_level and semester from the user model instance
user = self.Meta.model(**validated_data)
# Create a new user using the base serializer's create() method
user = super().create(validated_data)
# Create a student_status object for the user
2023-06-26 23:56:51 +08:00
StudentStatus.objects.create(
user=user,
active=False,
x=None,
y=None,
subject=None
)
return user