From f0d052bc661cd0ab3ac885faa7f2ed00e7eacd35 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Tue, 27 Jun 2023 13:40:31 +0800 Subject: [PATCH] Polished student_status serializer --- stude/accounts/migrations/0001_initial.py | 3 ++- .../accounts/migrations/0002_studentstatus.py | 27 ------------------- .../migrations/0003_delete_studentstatus.py | 16 ----------- stude/accounts/models.py | 7 +++++ stude/accounts/serializers.py | 8 ++---- .../student_status/migrations/0001_initial.py | 13 +++++---- ...ame_x_latitude_studentstatus_x_and_more.py | 23 ---------------- .../migrations/0003_studentstatus_active.py | 18 ------------- .../0004_alter_studentstatus_subject.py | 18 ------------- stude/student_status/models.py | 3 --- stude/student_status/serializers.py | 18 +++++++++++-- stude/student_status/views.py | 4 +-- 12 files changed, 35 insertions(+), 123 deletions(-) delete mode 100644 stude/accounts/migrations/0002_studentstatus.py delete mode 100644 stude/accounts/migrations/0003_delete_studentstatus.py delete mode 100644 stude/student_status/migrations/0002_rename_x_latitude_studentstatus_x_and_more.py delete mode 100644 stude/student_status/migrations/0003_studentstatus_active.py delete mode 100644 stude/student_status/migrations/0004_alter_studentstatus_subject.py diff --git a/stude/accounts/migrations/0001_initial.py b/stude/accounts/migrations/0001_initial.py index 3b66ba3..c72d83e 100644 --- a/stude/accounts/migrations/0001_initial.py +++ b/stude/accounts/migrations/0001_initial.py @@ -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')), diff --git a/stude/accounts/migrations/0002_studentstatus.py b/stude/accounts/migrations/0002_studentstatus.py deleted file mode 100644 index 7eb3b60..0000000 --- a/stude/accounts/migrations/0002_studentstatus.py +++ /dev/null @@ -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)), - ], - ), - ] diff --git a/stude/accounts/migrations/0003_delete_studentstatus.py b/stude/accounts/migrations/0003_delete_studentstatus.py deleted file mode 100644 index c8084c1..0000000 --- a/stude/accounts/migrations/0003_delete_studentstatus.py +++ /dev/null @@ -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', - ), - ] diff --git a/stude/accounts/models.py b/stude/accounts/models.py index 903d026..a06cb15 100644 --- a/stude/accounts/models.py +++ b/stude/accounts/models.py @@ -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 diff --git a/stude/accounts/serializers.py b/stude/accounts/serializers.py index 4f3d2d4..ca91ceb 100644 --- a/stude/accounts/serializers.py +++ b/stude/accounts/serializers.py @@ -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, diff --git a/stude/student_status/migrations/0001_initial.py b/stude/student_status/migrations/0001_initial.py index 5425574..7da3ffe 100644 --- a/stude/student_status/migrations/0001_initial.py +++ b/stude/student_status/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.2 on 2023-06-26 14:03 +# Generated by Django 4.2.2 on 2023-06-27 05:15 from django.conf import settings from django.db import migrations, models @@ -10,7 +10,7 @@ class Migration(migrations.Migration): initial = True dependencies = [ - ('accounts', '0003_delete_studentstatus'), + ('accounts', '0001_initial'), ] operations = [ @@ -18,11 +18,10 @@ class Migration(migrations.Migration): 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)), + ('x', models.FloatField(null=True)), + ('y', models.FloatField(null=True)), + ('subject', models.CharField(max_length=100, null=True)), + ('active', models.BooleanField(default=False)), ('timestamp', models.DateField(auto_now_add=True)), ], ), diff --git a/stude/student_status/migrations/0002_rename_x_latitude_studentstatus_x_and_more.py b/stude/student_status/migrations/0002_rename_x_latitude_studentstatus_x_and_more.py deleted file mode 100644 index a365913..0000000 --- a/stude/student_status/migrations/0002_rename_x_latitude_studentstatus_x_and_more.py +++ /dev/null @@ -1,23 +0,0 @@ -# Generated by Django 4.2.2 on 2023-06-26 14:51 - -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('student_status', '0001_initial'), - ] - - operations = [ - migrations.RenameField( - model_name='studentstatus', - old_name='x_latitude', - new_name='x', - ), - migrations.RenameField( - model_name='studentstatus', - old_name='y_latitude', - new_name='y', - ), - ] diff --git a/stude/student_status/migrations/0003_studentstatus_active.py b/stude/student_status/migrations/0003_studentstatus_active.py deleted file mode 100644 index bdb6339..0000000 --- a/stude/student_status/migrations/0003_studentstatus_active.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 4.2.2 on 2023-06-26 15:11 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('student_status', '0002_rename_x_latitude_studentstatus_x_and_more'), - ] - - operations = [ - migrations.AddField( - model_name='studentstatus', - name='active', - field=models.BooleanField(default=False), - ), - ] diff --git a/stude/student_status/migrations/0004_alter_studentstatus_subject.py b/stude/student_status/migrations/0004_alter_studentstatus_subject.py deleted file mode 100644 index da76abb..0000000 --- a/stude/student_status/migrations/0004_alter_studentstatus_subject.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 4.2.2 on 2023-06-26 15:48 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('student_status', '0003_studentstatus_active'), - ] - - operations = [ - migrations.AlterField( - model_name='studentstatus', - name='subject', - field=models.CharField(max_length=100, null=True), - ), - ] diff --git a/stude/student_status/models.py b/stude/student_status/models.py index 2e60170..8b24184 100644 --- a/stude/student_status/models.py +++ b/stude/student_status/models.py @@ -22,8 +22,5 @@ class StudentStatus(models.Model): x = models.FloatField(null=True) y = models.FloatField(null=True) subject = models.CharField(max_length=100, null=True) - year_level = models.CharField( - max_length=50, choices=CustomUser.YEAR_LEVELS) - semester = models.CharField(max_length=50, choices=CustomUser.SEMESTERS) active = models.BooleanField(default=False) timestamp = models.DateField(auto_now_add=True) diff --git a/stude/student_status/serializers.py b/stude/student_status/serializers.py index 3811d4a..26768ad 100644 --- a/stude/student_status/serializers.py +++ b/stude/student_status/serializers.py @@ -3,13 +3,27 @@ from .models import StudentStatus class StudentStatusSerializer(serializers.ModelSerializer): + year_level = serializers.CharField(source='user.year_level', read_only=True) + course = serializers.CharField(source='user.course', read_only=True) + semester = serializers.CharField(source='user.semester', read_only=True) + class Meta: model = StudentStatus fields = '__all__' - read_only_fields = ('user',) + read_only_fields = ['user'] def create(self, validated_data): user = self.context['request'].user - student_status, created = StudentStatus.objects.update_or_create( + student_status = StudentStatus.objects.create( user=user, defaults=validated_data) return student_status + + def update(self, instance, validated_data): + active = validated_data.get('active', None) + + if active is not None and active is False: + validated_data['x'] = None + validated_data['y'] = None + validated_data['subject'] = None + + return super().update(instance, validated_data) \ No newline at end of file diff --git a/stude/student_status/views.py b/stude/student_status/views.py index 029d4b1..0eae6c0 100644 --- a/stude/student_status/views.py +++ b/stude/student_status/views.py @@ -1,10 +1,10 @@ -from rest_framework import generics +from rest_framework import generics, viewsets from rest_framework.permissions import IsAuthenticated from .models import StudentStatus from .serializers import StudentStatusSerializer -class StudentStatusAPIView(generics.RetrieveUpdateDestroyAPIView): +class StudentStatusAPIView(generics.RetrieveUpdateAPIView): serializer_class = StudentStatusSerializer permission_classes = [IsAuthenticated]