Polished student_status serializer

This commit is contained in:
Keannu Christian Bernasol 2023-06-27 13:40:31 +08:00
parent 0fc98ede20
commit f0d052bc66
12 changed files with 35 additions and 123 deletions

View file

@ -1,4 +1,4 @@
# Generated by Django 4.2.2 on 2023-06-26 13:38
# Generated by Django 4.2.2 on 2023-06-27 05:15
import accounts.models
import django.contrib.auth.models
@ -36,6 +36,7 @@ class Migration(migrations.Migration):
('student_id_number', models.CharField(max_length=16, validators=[accounts.models.validate_student_id])),
('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)),
('course', models.CharField(choices=[('BSIT', 'Bachelor of Science in Information Technology'), ('BSCS', 'Bachelor of Science in Computer Science'), ('BSCpE', 'Bachelor of Science in Computer Engineering')], max_length=50)),
('avatar', models.ImageField(null=True, upload_to=accounts.models.CustomUser._get_upload_to)),
('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

@ -1,27 +0,0 @@
# Generated by Django 4.2.2 on 2023-06-26 13:46
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('accounts', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='StudentStatus',
fields=[
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to=settings.AUTH_USER_MODEL)),
('x_latitude', models.FloatField(null=True)),
('y_latitude', models.FloatField(null=True)),
('subject', models.CharField(max_length=100)),
('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)),
('timestamp', models.DateField(auto_now_add=True)),
],
),
]

View file

@ -1,16 +0,0 @@
# Generated by Django 4.2.2 on 2023-06-26 14:03
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('accounts', '0002_studentstatus'),
]
operations = [
migrations.DeleteModel(
name='StudentStatus',
),
]

View file

@ -26,6 +26,11 @@ class CustomUser(AbstractUser):
('1st', '1st semester'),
('2nd', '2nd semester'),
)
COURSES = (
('BSIT', 'Bachelor of Science in Information Technology'),
('BSCS', 'Bachelor of Science in Computer Science'),
('BSCpE', 'Bachelor of Science in Computer Engineering'),
)
def _get_upload_to(instance, filename):
base_filename, file_extension = os.path.splitext(filename)
@ -51,6 +56,8 @@ class CustomUser(AbstractUser):
max_length=50, choices=YEAR_LEVELS)
semester = models.CharField(
max_length=50, choices=SEMESTERS)
course = models.CharField(
max_length=50, choices=COURSES)
avatar = models.ImageField(upload_to=_get_upload_to, null=True)
@property

View file

@ -12,19 +12,17 @@ class CustomUserSerializer(BaseUserSerializer):
class Meta(BaseUserSerializer.Meta):
model = CustomUser
fields = ('username', 'email', 'password',
'student_id_number', 'year_level', 'semester', 'avatar', 'first_name', 'last_name', 'is_banned', 'user_status')
'student_id_number', 'year_level', 'semester', 'course', 'avatar', 'first_name', 'last_name', 'is_banned', 'user_status')
class UserRegistrationSerializer(BaseUserRegistrationSerializer):
class Meta(BaseUserRegistrationSerializer.Meta):
fields = ('username', 'email', 'password',
'student_id_number', 'year_level', 'semester', 'avatar', 'first_name', 'last_name')
'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)
year_level = user.year_level
semester = user.semester
# Create a new user using the base serializer's create() method
user = super().create(validated_data)
@ -32,8 +30,6 @@ class UserRegistrationSerializer(BaseUserRegistrationSerializer):
# Create a student_status object for the user
StudentStatus.objects.create(
user=user,
year_level=year_level,
semester=semester,
active=False,
x=None,
y=None,