diff --git a/infotech/schedules/migrations/0005_schedule_max_slots.py b/infotech/schedules/migrations/0005_schedule_max_slots.py new file mode 100644 index 0000000..fd9d82d --- /dev/null +++ b/infotech/schedules/migrations/0005_schedule_max_slots.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2 on 2023-04-22 07:01 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('schedules', '0004_schedule_daytimes'), + ] + + operations = [ + migrations.AddField( + model_name='schedule', + name='max_slots', + field=models.IntegerField(default=50), + ), + ] diff --git a/infotech/schedules/models.py b/infotech/schedules/models.py index 907a2cd..7c4e8f6 100644 --- a/infotech/schedules/models.py +++ b/infotech/schedules/models.py @@ -15,6 +15,7 @@ class Schedule(models.Model): 'daytimes.DayTime', related_name='DayTime_full_name', on_delete=models.CASCADE, null=True) date_created = models.DateTimeField(default=now, editable=False) + max_slots = models.IntegerField(default=50) def save(self, *args, **kwargs): self.name = f"{self.subject} : {self.professor}" diff --git a/infotech/schedules/serializers.py b/infotech/schedules/serializers.py index 8a9209e..90dc2fe 100644 --- a/infotech/schedules/serializers.py +++ b/infotech/schedules/serializers.py @@ -24,6 +24,14 @@ class ScheduleSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Schedule - fields = ('id', 'name', 'subject', 'daytimes', 'students_assigned', + fields = ('id', 'name', 'max_slots', 'subject', 'daytimes', 'students_assigned', 'professor', 'date_created') read_only_fields = ('id', 'date_created', 'name') + + def validate(self, attrs): + students = attrs.get('students') + max_slots = attrs.get('max_slots') + if students.count() > max_slots: + raise serializers.ValidationError( + 'Too many students for this subject') + return attrs diff --git a/infotech/subjects/migrations/0008_remove_subject_max_slots.py b/infotech/subjects/migrations/0008_remove_subject_max_slots.py new file mode 100644 index 0000000..5067087 --- /dev/null +++ b/infotech/subjects/migrations/0008_remove_subject_max_slots.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2 on 2023-04-22 07:00 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('subjects', '0007_delete_subjectstudent'), + ] + + operations = [ + migrations.RemoveField( + model_name='subject', + name='max_slots', + ), + ] diff --git a/infotech/subjects/models.py b/infotech/subjects/models.py index d0b4199..386c90a 100644 --- a/infotech/subjects/models.py +++ b/infotech/subjects/models.py @@ -17,7 +17,6 @@ class Subject(models.Model): name = models.CharField(max_length=40) code = models.CharField(max_length=20) - max_slots = models.IntegerField(default=60) year_level = models.CharField(max_length=20, choices=YearLevels.choices) semester = models.CharField( max_length=20, choices=Semesters.choices, default=Semesters.FIRST_SEM) diff --git a/infotech/subjects/serializers.py b/infotech/subjects/serializers.py index a48a818..cbe4f49 100644 --- a/infotech/subjects/serializers.py +++ b/infotech/subjects/serializers.py @@ -8,6 +8,5 @@ class SubjectSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Subject - fields = ('id', 'name', 'code', - 'max_slots', 'year_level', 'semester') + fields = ('id', 'name', 'code', 'year_level', 'semester') read_only_fields = ['id']