From e567b5d399b07e2efebf4f1d4292ca5470882a03 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Mon, 3 Jul 2023 21:22:06 +0800 Subject: [PATCH] Fixed user serializer still using django default user --- Pipfile | 1 + Pipfile.lock | 18 ++++++++++++++- stude/accounts/serializers.py | 41 ++++++++++++++++++++++++++--------- stude/config/settings.py | 2 +- 4 files changed, 50 insertions(+), 12 deletions(-) diff --git a/Pipfile b/Pipfile index 6e5a135..c235739 100644 --- a/Pipfile +++ b/Pipfile @@ -10,6 +10,7 @@ python-dotenv = "*" djoser = "*" pillow = "*" whitenoise = "*" +djangochannelsrestframework = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index af0eb49..b67ebf8 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -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", diff --git a/stude/accounts/serializers.py b/stude/accounts/serializers.py index 79b3298..89f8487 100644 --- a/stude/accounts/serializers.py +++ b/stude/accounts/serializers.py @@ -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 diff --git a/stude/config/settings.py b/stude/config/settings.py index cd1dc15..3a18cd8 100644 --- a/stude/config/settings.py +++ b/stude/config/settings.py @@ -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 = ''