From 1256efa3d233d96e1425fcce4f526dea95dfadc6 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Mon, 26 Jun 2023 23:50:55 +0800 Subject: [PATCH] Made student_status persistent rather than be deleted when unused. Add active field to student_status and auto create student_status instance when a user is created --- stude/accounts/models.py | 4 +++ stude/accounts/serializers.py | 35 +++++++++++++++++-- stude/api/urls.py | 3 +- stude/student_status/admin.py | 3 +- ...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 | 7 ++-- stude/student_status/serializers.py | 12 +++++-- stude/student_status/urls.py | 7 ++++ stude/student_status/views.py | 22 ++++++++++++ 11 files changed, 142 insertions(+), 10 deletions(-) create mode 100644 stude/student_status/migrations/0002_rename_x_latitude_studentstatus_x_and_more.py create mode 100644 stude/student_status/migrations/0003_studentstatus_active.py create mode 100644 stude/student_status/migrations/0004_alter_studentstatus_subject.py create mode 100644 stude/student_status/urls.py create mode 100644 stude/student_status/views.py diff --git a/stude/accounts/models.py b/stude/accounts/models.py index ad9cb4f..903d026 100644 --- a/stude/accounts/models.py +++ b/stude/accounts/models.py @@ -52,4 +52,8 @@ class CustomUser(AbstractUser): semester = models.CharField( max_length=50, choices=SEMESTERS) avatar = models.ImageField(upload_to=_get_upload_to, null=True) + + @property + def full_name(self): + return f"{self.first_name} {self.last_name}" pass diff --git a/stude/accounts/serializers.py b/stude/accounts/serializers.py index 4659fa8..36d3c26 100644 --- a/stude/accounts/serializers.py +++ b/stude/accounts/serializers.py @@ -2,6 +2,7 @@ from djoser.serializers import UserCreateSerializer as BaseUserRegistrationSeria from djoser.serializers import UserSerializer as BaseUserSerializer from accounts.models import CustomUser from student_status.serializers import StudentStatusSerializer +from student_status.models import StudentStatus class CustomUserSerializer(BaseUserSerializer): @@ -11,10 +12,40 @@ class CustomUserSerializer(BaseUserSerializer): class Meta(BaseUserSerializer.Meta): model = CustomUser fields = ('username', 'email', 'password', - 'student_id_number', 'year_level', 'semester', 'avatar', 'is_banned', 'user_status') + 'student_id_number', 'year_level', 'semester', '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') + 'student_id_number', 'year_level', 'semester', 'avatar', 'first_name', 'last_name') + + def create(self, validated_data): + # Extract the necessary fields from validated_data + username = validated_data['username'] + email = validated_data['email'] + password = validated_data['password'] + student_id_number = validated_data['student_id_number'] + first_name = validated_data['first_name'] + last_name = validated_data['last_name'] + + # 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) + + # Create a student_status object for the user + student_status = StudentStatus.objects.create( + user=user, + year_level=year_level, + semester=semester, + active=False, + x=None, + y=None, + subject=None + ) + + return user diff --git a/stude/api/urls.py b/stude/api/urls.py index a46b59c..dc63ab8 100644 --- a/stude/api/urls.py +++ b/stude/api/urls.py @@ -2,5 +2,6 @@ from django.contrib import admin from django.urls import path, include urlpatterns = [ - path('accounts/', include('accounts.urls')) + path('accounts/', include('accounts.urls')), + path('student_status/', include('student_status.urls')) ] diff --git a/stude/student_status/admin.py b/stude/student_status/admin.py index 8c38f3f..ba4cb5f 100644 --- a/stude/student_status/admin.py +++ b/stude/student_status/admin.py @@ -1,3 +1,4 @@ from django.contrib import admin +from .models import StudentStatus -# Register your models here. +admin.site.register(StudentStatus) 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 new file mode 100644 index 0000000..a365913 --- /dev/null +++ b/stude/student_status/migrations/0002_rename_x_latitude_studentstatus_x_and_more.py @@ -0,0 +1,23 @@ +# 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 new file mode 100644 index 0000000..bdb6339 --- /dev/null +++ b/stude/student_status/migrations/0003_studentstatus_active.py @@ -0,0 +1,18 @@ +# 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 new file mode 100644 index 0000000..da76abb --- /dev/null +++ b/stude/student_status/migrations/0004_alter_studentstatus_subject.py @@ -0,0 +1,18 @@ +# 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 cf1c9cf..2e60170 100644 --- a/stude/student_status/models.py +++ b/stude/student_status/models.py @@ -19,10 +19,11 @@ class StudentStatus(models.Model): ) user = models.OneToOneField( CustomUser, on_delete=models.CASCADE, primary_key=True) - x_latitude = models.FloatField(null=True) - y_latitude = models.FloatField(null=True) - subject = models.CharField(max_length=100) + 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 151deaf..3811d4a 100644 --- a/stude/student_status/serializers.py +++ b/stude/student_status/serializers.py @@ -1,9 +1,15 @@ from rest_framework import serializers -from student_status.models import StudentStatus +from .models import StudentStatus class StudentStatusSerializer(serializers.ModelSerializer): class Meta: model = StudentStatus - fields = ('x_latitude', 'y_latitude', 'subject', - 'year_level', 'semester', 'timestamp') + fields = '__all__' + read_only_fields = ('user',) + + def create(self, validated_data): + user = self.context['request'].user + student_status, created = StudentStatus.objects.update_or_create( + user=user, defaults=validated_data) + return student_status diff --git a/stude/student_status/urls.py b/stude/student_status/urls.py new file mode 100644 index 0000000..963e0cd --- /dev/null +++ b/stude/student_status/urls.py @@ -0,0 +1,7 @@ +from django.urls import path +from .views import StudentStatusAPIView, ActiveStudentStatusListAPIView + +urlpatterns = [ + path('self/', StudentStatusAPIView.as_view()), + path('active_list/', ActiveStudentStatusListAPIView.as_view()), +] diff --git a/stude/student_status/views.py b/stude/student_status/views.py new file mode 100644 index 0000000..029d4b1 --- /dev/null +++ b/stude/student_status/views.py @@ -0,0 +1,22 @@ +from rest_framework import generics +from rest_framework.permissions import IsAuthenticated +from .models import StudentStatus +from .serializers import StudentStatusSerializer + + +class StudentStatusAPIView(generics.RetrieveUpdateDestroyAPIView): + serializer_class = StudentStatusSerializer + permission_classes = [IsAuthenticated] + + def get_object(self): + user = self.request.user + return StudentStatus.objects.get(user=user) + + +class ActiveStudentStatusListAPIView(generics.ListAPIView): + serializer_class = StudentStatusSerializer + permission_classes = [IsAuthenticated] + + def get_queryset(self): + user = self.request.user + return StudentStatus.objects.filter(active=True).exclude(user=user)