diff --git a/infotech/schedules/models.py b/infotech/schedules/models.py index 5787f7a..556b216 100644 --- a/infotech/schedules/models.py +++ b/infotech/schedules/models.py @@ -12,6 +12,13 @@ class Schedule(models.Model): 'professors.Professor', related_name='Professor_full_name', on_delete=models.CASCADE) date_created = models.DateTimeField(default=now, editable=False) + @property + def name(self): + return f"{self.subject}" + + def __str__(self): + return self.subject + class StudentSchedule(models.Model): schedule = models.ForeignKey( diff --git a/infotech/schedules/serializers.py b/infotech/schedules/serializers.py index 3017a56..7c94c8a 100644 --- a/infotech/schedules/serializers.py +++ b/infotech/schedules/serializers.py @@ -1,13 +1,31 @@ from rest_framework import serializers from .models import Schedule +from professors.models import Professor +from students.models import Student class ScheduleSerializer(serializers.HyperlinkedModelSerializer): date_created = serializers.DateTimeField( format="%d-%m-%Y %I:%M%p", read_only=True) + # students_assigned = serializers.SlugRelatedField( + # queryset=Student.objects.all(), slug_field='student_assigned', allow_null=True) + + professor = serializers.SlugRelatedField( + queryset=Professor.objects.all(), slug_field='full_name', allow_null=True) + class Meta: model = Schedule fields = ('id', 'subject', 'students_assigned', 'professor', 'date_created') read_only_fields = ('id', 'date_created') + + +class StudentScheduleSerializer(serializers.HyperlinkedModelSerializer): + date_joined = serializers.DateTimeField( + format="%d-%m-%Y %I:%M%p", read_only=True) + + class Meta: + model = Schedule + fields = ('id', 'schedule', 'student_assigned', 'date_joined') + read_only_fields = ('id', 'date_joined') diff --git a/infotech/schedules/views.py b/infotech/schedules/views.py index bbfe8f7..dcfcf26 100644 --- a/infotech/schedules/views.py +++ b/infotech/schedules/views.py @@ -3,11 +3,17 @@ from django.shortcuts import render # Create your views here. from rest_framework.permissions import IsAuthenticated from rest_framework import viewsets -from .serializers import ScheduleSerializer -from .models import Schedule +from .serializers import ScheduleSerializer, StudentScheduleSerializer +from .models import Schedule, StudentSchedule class ScheduleViewSet(viewsets.ModelViewSet): # permission_classes = [IsAuthenticated] serializer_class = ScheduleSerializer queryset = Schedule.objects.all() + + +class StudentScheduleViewSet(viewsets.ModelViewSet): + # permission_classes = [IsAuthenticated] + serializer_class = StudentScheduleSerializer + queryset = StudentSchedule.objects.all() diff --git a/infotech/students/migrations/0006_student_schedules.py b/infotech/students/migrations/0006_student_schedules.py new file mode 100644 index 0000000..0ff9da0 --- /dev/null +++ b/infotech/students/migrations/0006_student_schedules.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2 on 2023-04-22 03:05 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('schedules', '0002_alter_schedule_professor'), + ('students', '0005_remove_student_enrolled_subjects'), + ] + + operations = [ + migrations.AddField( + model_name='student', + name='schedules', + field=models.ManyToManyField(through='schedules.StudentSchedule', to='schedules.schedule'), + ), + ] diff --git a/infotech/students/migrations/0007_alter_student_schedules.py b/infotech/students/migrations/0007_alter_student_schedules.py new file mode 100644 index 0000000..6ea4d66 --- /dev/null +++ b/infotech/students/migrations/0007_alter_student_schedules.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2 on 2023-04-22 03:24 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('schedules', '0002_alter_schedule_professor'), + ('students', '0006_student_schedules'), + ] + + operations = [ + migrations.AlterField( + model_name='student', + name='schedules', + field=models.ManyToManyField(related_name='Schedule_subject', through='schedules.StudentSchedule', to='schedules.schedule'), + ), + ] diff --git a/infotech/students/migrations/0008_remove_student_schedules.py b/infotech/students/migrations/0008_remove_student_schedules.py new file mode 100644 index 0000000..9f8917c --- /dev/null +++ b/infotech/students/migrations/0008_remove_student_schedules.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2 on 2023-04-22 03:58 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('students', '0007_alter_student_schedules'), + ] + + operations = [ + migrations.RemoveField( + model_name='student', + name='schedules', + ), + ] diff --git a/infotech/students/models.py b/infotech/students/models.py index d15539c..7ab0af2 100644 --- a/infotech/students/models.py +++ b/infotech/students/models.py @@ -36,15 +36,15 @@ class Student(models.Model): clearance_done = models.BooleanField() pta_done = models.BooleanField() # - # enrolled_subjects = models.ManyToManyField( - # 'subjects.Subject', through='subjects.SubjectStudent') + # schedules = models.ManyToManyField( + # 'schedules.Schedule', related_name='StudentSchedule_subject', through='schedules.StudentSchedule') year_level = models.CharField(max_length=20, choices=YearLevels.choices) current_semester = models.CharField( max_length=20, choices=Semesters.choices, default=Semesters.FIRST_SEM) - def __str__(self): - return self.first_name - @property def full_name(self): - return self.first_name + " " + self.middle_name + " " + self.last_name + return f"{self.first_name} {self.last_name}" + + def __str__(self): + return self.full_name diff --git a/infotech/students/serializers.py b/infotech/students/serializers.py index 7b91c69..4363896 100644 --- a/infotech/students/serializers.py +++ b/infotech/students/serializers.py @@ -1,7 +1,7 @@ from rest_framework import serializers from django.contrib.auth.models import User from .models import Student -from subjects.models import Subject +from schedules.models import Schedule class StudentSerializer(serializers.HyperlinkedModelSerializer):