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 Bernasol 2023-06-26 23:50:55 +08:00
parent ca527289af
commit 1256efa3d2
11 changed files with 142 additions and 10 deletions

View file

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

View file

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

View file

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

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)