mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2024-11-17 06:19:24 +08:00
Fixed user serializer still using django default user
This commit is contained in:
parent
05d9dbd296
commit
e567b5d399
4 changed files with 50 additions and 12 deletions
1
Pipfile
1
Pipfile
|
@ -10,6 +10,7 @@ python-dotenv = "*"
|
||||||
djoser = "*"
|
djoser = "*"
|
||||||
pillow = "*"
|
pillow = "*"
|
||||||
whitenoise = "*"
|
whitenoise = "*"
|
||||||
|
djangochannelsrestframework = "*"
|
||||||
|
|
||||||
[dev-packages]
|
[dev-packages]
|
||||||
|
|
||||||
|
|
18
Pipfile.lock
generated
18
Pipfile.lock
generated
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"_meta": {
|
"_meta": {
|
||||||
"hash": {
|
"hash": {
|
||||||
"sha256": "7a186c2a779282b0072d0634e264e5e70842a4c9676dfc4d1e18e564860e87c8"
|
"sha256": "231f11224ec1ef1ad1406ac5644ebcdfac020af9478f407d577553bb05c637df"
|
||||||
},
|
},
|
||||||
"pipfile-spec": 6,
|
"pipfile-spec": 6,
|
||||||
"requires": {
|
"requires": {
|
||||||
|
@ -101,6 +101,14 @@
|
||||||
],
|
],
|
||||||
"version": "==1.15.1"
|
"version": "==1.15.1"
|
||||||
},
|
},
|
||||||
|
"channels": {
|
||||||
|
"hashes": [
|
||||||
|
"sha256:0ce53507a7da7b148eaa454526e0e05f7da5e5d1c23440e4886cf146981d8420",
|
||||||
|
"sha256:2253334ac76f67cba68c2072273f7e0e67dbdac77eeb7e318f511d2f9a53c5e4"
|
||||||
|
],
|
||||||
|
"markers": "python_version >= '3.7'",
|
||||||
|
"version": "==4.0.0"
|
||||||
|
},
|
||||||
"charset-normalizer": {
|
"charset-normalizer": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:04afa6387e2b282cf78ff3dbce20f0cc071c12dc8f685bd40960cc68644cfea6",
|
"sha256:04afa6387e2b282cf78ff3dbce20f0cc071c12dc8f685bd40960cc68644cfea6",
|
||||||
|
@ -230,6 +238,14 @@
|
||||||
],
|
],
|
||||||
"version": "==1.1.1"
|
"version": "==1.1.1"
|
||||||
},
|
},
|
||||||
|
"djangochannelsrestframework": {
|
||||||
|
"hashes": [
|
||||||
|
"sha256:937260996b78fad66ddf4aa03dc61434b81b21a757897a899cd541d0f197c4ce",
|
||||||
|
"sha256:ca37fb96bb2f746129972a81dafed42d9785a37a2db36827dbf17848a0a9df96"
|
||||||
|
],
|
||||||
|
"index": "pypi",
|
||||||
|
"version": "==1.1.0"
|
||||||
|
},
|
||||||
"djangorestframework": {
|
"djangorestframework": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:579a333e6256b09489cbe0a067e66abe55c6595d8926be6b99423786334350c8",
|
"sha256:579a333e6256b09489cbe0a067e66abe55c6595d8926be6b99423786334350c8",
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
from djoser.serializers import UserCreateSerializer as BaseUserRegistrationSerializer
|
from djoser.serializers import UserCreateSerializer as BaseUserRegistrationSerializer
|
||||||
from djoser.serializers import UserSerializer as BaseUserSerializer
|
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 rest_framework import serializers
|
||||||
from accounts.models import CustomUser
|
from accounts.models import CustomUser
|
||||||
from student_status.serializers import StudentStatusSerializer
|
from student_status.serializers import StudentStatusSerializer
|
||||||
from student_status.models import StudentStatus
|
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):
|
class CustomUserSerializer(BaseUserSerializer):
|
||||||
|
@ -15,20 +19,38 @@ class CustomUserSerializer(BaseUserSerializer):
|
||||||
fields = ('username', 'email', 'password',
|
fields = ('username', 'email', 'password',
|
||||||
'student_id_number', 'year_level', 'semester', 'course', 'subjects', 'avatar', 'first_name', 'last_name', 'is_banned', 'user_status')
|
'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):
|
class UserRegistrationSerializer(serializers.ModelSerializer):
|
||||||
fields = ('username', 'email', 'password',
|
email = serializers.EmailField(required=True)
|
||||||
'student_id_number', 'year_level', 'semester', 'course', 'subjects', 'avatar', 'first_name', 'last_name')
|
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):
|
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 = 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(
|
StudentStatus.objects.create(
|
||||||
user=user,
|
user=user,
|
||||||
active=False,
|
active=False,
|
||||||
|
@ -36,5 +58,4 @@ class UserRegistrationSerializer(BaseUserRegistrationSerializer):
|
||||||
y=None,
|
y=None,
|
||||||
subject=None
|
subject=None
|
||||||
)
|
)
|
||||||
|
|
||||||
return user
|
return user
|
||||||
|
|
|
@ -29,7 +29,7 @@ SECRET_KEY = str(os.getenv('SECRET_KEY'))
|
||||||
# SECURITY WARNING: don't run with debug turned on in production!
|
# SECURITY WARNING: don't run with debug turned on in production!
|
||||||
DEBUG = True
|
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 credentials
|
||||||
EMAIL_HOST = ''
|
EMAIL_HOST = ''
|
||||||
|
|
Loading…
Reference in a new issue