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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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