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

This commit is contained in:
Keannu Christian Bernasol 2023-06-26 23:50:55 +08:00
parent ca527289af
commit 1256efa3d2
11 changed files with 142 additions and 10 deletions

View file

@ -1,3 +1,4 @@
from django.contrib import admin
from .models import StudentStatus
# Register your models here.
admin.site.register(StudentStatus)

View file

@ -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',
),
]

View file

@ -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),
),
]

View file

@ -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),
),
]

View file

@ -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)

View file

@ -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

View file

@ -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()),
]

View file

@ -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)