Polished and fixed custom user model

This commit is contained in:
Keannu Bernasol 2023-06-26 20:36:58 +08:00
parent e19c88527a
commit 87d30a0ecc
6 changed files with 93 additions and 21 deletions

View file

@ -1,4 +1,4 @@
# Generated by Django 4.2.2 on 2023-06-26 11:43
# Generated by Django 4.2.2 on 2023-06-26 12:08
import django.contrib.auth.models
import django.contrib.auth.validators
@ -31,8 +31,8 @@ class Migration(migrations.Migration):
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('is_student', models.BooleanField(default=True)),
('is_banned', models.BooleanField(default=False)),
('year_level', models.CharField(blank=True, max_length=50, null=True)),
('semester', models.CharField(blank=True, max_length=50, null=True)),
('year_level', models.CharField(choices=[('1st', '1st year'), ('2nd', '2nd year'), ('3rd', '3rd year'), ('4th', '4th year'), ('5th', '5th Year'), ('Irreg', 'Irregular')], max_length=50)),
('semester', models.CharField(choices=[('1st', '1st semester'), ('2nd', '2nd semester')], max_length=50)),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')),
],

View file

@ -0,0 +1,28 @@
# Generated by Django 4.2.2 on 2023-06-26 12:23
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('accounts', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='customuser',
name='is_studying',
field=models.BooleanField(default=False),
),
migrations.AlterField(
model_name='customuser',
name='first_name',
field=models.CharField(max_length=100),
),
migrations.AlterField(
model_name='customuser',
name='last_name',
field=models.CharField(max_length=100),
),
]

View file

@ -0,0 +1,18 @@
# Generated by Django 4.2.2 on 2023-06-26 12:29
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('accounts', '0002_customuser_is_studying_alter_customuser_first_name_and_more'),
]
operations = [
migrations.AddField(
model_name='customuser',
name='avatar',
field=models.ImageField(null=True, upload_to='images'),
),
]

View file

@ -3,8 +3,30 @@ from django.db import models
class CustomUser(AbstractUser):
YEAR_LEVELS = (
('1st', '1st year'),
('2nd', '2nd year'),
('3rd', '3rd year'),
('4th', '4th year'),
('5th', '5th Year'),
('Irreg', 'Irregular'),
)
SEMESTERS = (
('1st', '1st semester'),
('2nd', '2nd semester'),
)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
# Email inherited from base user class
# Username inherited from base user class
# Password inherited from base user class
# is_admin inherited from base user class
is_student = models.BooleanField(default=True)
is_studying = models.BooleanField(default=False)
is_banned = models.BooleanField(default=False)
year_level = models.CharField(max_length=50, null=True, blank=True)
semester = models.CharField(max_length=50, null=True, blank=True)
year_level = models.CharField(
max_length=50, choices=YEAR_LEVELS)
semester = models.CharField(
max_length=50, choices=SEMESTERS)
avatar = models.ImageField(upload_to='images', null=True)
pass

View file

@ -11,5 +11,5 @@ class CustomUserSerializer(BaseUserSerializer):
class UserRegistrationSerializer(BaseUserRegistrationSerializer):
class Meta(BaseUserRegistrationSerializer.Meta):
fields = ('is_student',
'is_banned', 'year_level', 'semester')
fields = ('username', 'email', 'password',
'is_student', 'year_level', 'semester', 'avatar')

View file

@ -31,11 +31,27 @@ DEBUG = True
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
# Email credentials
EMAIL_HOST = ''
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_PORT = ''
if (DEBUG == True):
EMAIL_HOST = str(os.getenv('DEV_EMAIL_HOST'))
EMAIL_HOST_USER = str(os.getenv('DEV_EMAIL_HOST_USER'))
EMAIL_HOST_PASSWORD = str(os.getenv('DEV_EMAIL_HOST_PASSWORD'))
EMAIL_PORT = str(os.getenv('DEV_EMAIL_PORT'))
else:
EMAIL_HOST = str(os.getenv('PROD_EMAIL_HOST'))
EMAIL_HOST_USER = str(os.getenv('PROD_EMAIL_HOST_USER'))
EMAIL_HOST_PASSWORD = str(os.getenv('PROD_EMAIL_HOST_PASSWORD'))
EMAIL_PORT = str(os.getenv('PROD_EMAIL_PORT'))
# Application definition
INSTALLED_APPS = [
'accounts',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
@ -45,6 +61,7 @@ INSTALLED_APPS = [
'rest_framework',
'djoser',
'rest_framework.authtoken',
'accounts',
]
@ -108,19 +125,6 @@ DJOSER = {
},
}
# Email credentials
if (DEBUG == True):
EMAIL_HOST = str(os.getenv('DEV_EMAIL_HOST'))
EMAIL_HOST_USER = str(os.getenv('DEV_EMAIL_HOST_USER'))
EMAIL_HOST_PASSWORD = str(os.getenv('DEV_EMAIL_HOST_PASSWORD'))
EMAIL_PORT = str(os.getenv('DEV_EMAIL_PORT'))
else:
EMAIL_HOST = str(os.getenv('PROD_EMAIL_HOST'))
EMAIL_HOST_USER = str(os.getenv('PROD_EMAIL_HOST_USER'))
EMAIL_HOST_PASSWORD = str(os.getenv('PROD_EMAIL_HOST_PASSWORD'))
EMAIL_PORT = str(os.getenv('PROD_EMAIL_PORT'))
# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators