Fixed user serializer still using django default user

This commit is contained in:
Keannu Bernasol 2023-07-03 21:22:06 +08:00
parent 05d9dbd296
commit e567b5d399
4 changed files with 50 additions and 12 deletions

View file

@ -10,6 +10,7 @@ python-dotenv = "*"
djoser = "*"
pillow = "*"
whitenoise = "*"
djangochannelsrestframework = "*"
[dev-packages]

18
Pipfile.lock generated
View file

@ -1,7 +1,7 @@
{
"_meta": {
"hash": {
"sha256": "7a186c2a779282b0072d0634e264e5e70842a4c9676dfc4d1e18e564860e87c8"
"sha256": "231f11224ec1ef1ad1406ac5644ebcdfac020af9478f407d577553bb05c637df"
},
"pipfile-spec": 6,
"requires": {
@ -101,6 +101,14 @@
],
"version": "==1.15.1"
},
"channels": {
"hashes": [
"sha256:0ce53507a7da7b148eaa454526e0e05f7da5e5d1c23440e4886cf146981d8420",
"sha256:2253334ac76f67cba68c2072273f7e0e67dbdac77eeb7e318f511d2f9a53c5e4"
],
"markers": "python_version >= '3.7'",
"version": "==4.0.0"
},
"charset-normalizer": {
"hashes": [
"sha256:04afa6387e2b282cf78ff3dbce20f0cc071c12dc8f685bd40960cc68644cfea6",
@ -230,6 +238,14 @@
],
"version": "==1.1.1"
},
"djangochannelsrestframework": {
"hashes": [
"sha256:937260996b78fad66ddf4aa03dc61434b81b21a757897a899cd541d0f197c4ce",
"sha256:ca37fb96bb2f746129972a81dafed42d9785a37a2db36827dbf17848a0a9df96"
],
"index": "pypi",
"version": "==1.1.0"
},
"djangorestframework": {
"hashes": [
"sha256:579a333e6256b09489cbe0a067e66abe55c6595d8926be6b99423786334350c8",

View file

@ -1,9 +1,13 @@
from djoser.serializers import UserCreateSerializer as BaseUserRegistrationSerializer
from djoser.serializers import UserSerializer as BaseUserSerializer
from django.core import exceptions as django_exceptions
from rest_framework import exceptions as drf_exceptions
from rest_framework import serializers
from accounts.models import CustomUser
from student_status.serializers import StudentStatusSerializer
from student_status.models import StudentStatus
from rest_framework.settings import api_settings
from django.contrib.auth.password_validation import validate_password
class CustomUserSerializer(BaseUserSerializer):
@ -15,20 +19,38 @@ class CustomUserSerializer(BaseUserSerializer):
fields = ('username', 'email', 'password',
'student_id_number', 'year_level', 'semester', 'course', 'subjects', 'avatar', 'first_name', 'last_name', 'is_banned', 'user_status')
# The model from your custom user
class UserRegistrationSerializer(BaseUserRegistrationSerializer):
class Meta(BaseUserRegistrationSerializer.Meta):
fields = ('username', 'email', 'password',
'student_id_number', 'year_level', 'semester', 'course', 'subjects', 'avatar', 'first_name', 'last_name')
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)
def create(self, validated_data):
# Get the user's year_level and semester from the user model instance
user = self.Meta.model(**validated_data)
user.set_password(validated_data['password'])
user.save()
# Create a new user using the base serializer's create() method
user = super().create(validated_data)
# Create a student_status object for the user
StudentStatus.objects.create(
user=user,
active=False,
@ -36,5 +58,4 @@ class UserRegistrationSerializer(BaseUserRegistrationSerializer):
y=None,
subject=None
)
return user

View file

@ -29,7 +29,7 @@ SECRET_KEY = str(os.getenv('SECRET_KEY'))
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
ALLOWED_HOSTS = ['*', '127.0.0.1', 'localhost', '10.0.10.32', '10.0.10.8']
# Email credentials
EMAIL_HOST = ''