mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2024-11-17 06:19:24 +08:00
Polished student_status serializer
This commit is contained in:
parent
0fc98ede20
commit
f0d052bc66
12 changed files with 35 additions and 123 deletions
|
@ -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')),
|
||||
|
|
|
@ -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)),
|
||||
],
|
||||
),
|
||||
]
|
|
@ -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',
|
||||
),
|
||||
]
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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)),
|
||||
],
|
||||
),
|
||||
|
|
|
@ -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',
|
||||
),
|
||||
]
|
|
@ -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),
|
||||
),
|
||||
]
|
|
@ -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),
|
||||
),
|
||||
]
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
|
@ -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]
|
||||
|
||||
|
|
Loading…
Reference in a new issue