From 134df378c36229c4967f308ed4006dd3bd41deec Mon Sep 17 00:00:00 2001 From: keannu125 Date: Sat, 22 Apr 2023 11:00:04 +0800 Subject: [PATCH] Initial transition to move relationships from subject to schedules --- infotech/api/urls.py | 2 + infotech/config/settings.py | 4 +- infotech/professors/__init__.py | 0 infotech/professors/admin.py | 6 +++ infotech/professors/apps.py | 6 +++ .../professors/migrations/0001_initial.py | 26 +++++++++++ infotech/professors/migrations/__init__.py | 0 infotech/professors/models.py | 25 ++++++++++ infotech/professors/serializers.py | 14 ++++++ infotech/professors/tests.py | 3 ++ infotech/professors/urls.py | 13 ++++++ infotech/professors/views.py | 10 ++++ infotech/schedules/__init__.py | 0 infotech/schedules/admin.py | 3 ++ infotech/schedules/apps.py | 6 +++ infotech/schedules/migrations/0001_initial.py | 46 +++++++++++++++++++ .../0002_alter_schedule_professor.py | 20 ++++++++ infotech/schedules/migrations/__init__.py | 0 infotech/schedules/models.py | 21 +++++++++ infotech/schedules/serializers.py | 13 ++++++ infotech/schedules/tests.py | 3 ++ infotech/schedules/urls.py | 13 ++++++ infotech/schedules/views.py | 13 ++++++ .../0005_remove_student_enrolled_subjects.py | 17 +++++++ infotech/students/models.py | 4 +- infotech/students/serializers.py | 4 +- ...ct_code_alter_subject_semester_and_more.py | 29 ++++++++++++ .../0006_remove_subject_students.py | 17 +++++++ .../migrations/0007_delete_subjectstudent.py | 17 +++++++ infotech/subjects/models.py | 15 ++---- infotech/subjects/serializers.py | 6 +-- 31 files changed, 334 insertions(+), 22 deletions(-) create mode 100644 infotech/professors/__init__.py create mode 100644 infotech/professors/admin.py create mode 100644 infotech/professors/apps.py create mode 100644 infotech/professors/migrations/0001_initial.py create mode 100644 infotech/professors/migrations/__init__.py create mode 100644 infotech/professors/models.py create mode 100644 infotech/professors/serializers.py create mode 100644 infotech/professors/tests.py create mode 100644 infotech/professors/urls.py create mode 100644 infotech/professors/views.py create mode 100644 infotech/schedules/__init__.py create mode 100644 infotech/schedules/admin.py create mode 100644 infotech/schedules/apps.py create mode 100644 infotech/schedules/migrations/0001_initial.py create mode 100644 infotech/schedules/migrations/0002_alter_schedule_professor.py create mode 100644 infotech/schedules/migrations/__init__.py create mode 100644 infotech/schedules/models.py create mode 100644 infotech/schedules/serializers.py create mode 100644 infotech/schedules/tests.py create mode 100644 infotech/schedules/urls.py create mode 100644 infotech/schedules/views.py create mode 100644 infotech/students/migrations/0005_remove_student_enrolled_subjects.py create mode 100644 infotech/subjects/migrations/0005_subject_code_alter_subject_semester_and_more.py create mode 100644 infotech/subjects/migrations/0006_remove_subject_students.py create mode 100644 infotech/subjects/migrations/0007_delete_subjectstudent.py diff --git a/infotech/api/urls.py b/infotech/api/urls.py index 43755cc..8f94b5f 100644 --- a/infotech/api/urls.py +++ b/infotech/api/urls.py @@ -4,5 +4,7 @@ from django.urls import path, include urlpatterns = [ path('', include('subjects.urls')), path('', include('students.urls')), + path('', include('professors.urls')), + path('', include('schedules.urls')), path('accounts/', include('accounts.urls')), ] diff --git a/infotech/config/settings.py b/infotech/config/settings.py index 668b789..9d51a7f 100644 --- a/infotech/config/settings.py +++ b/infotech/config/settings.py @@ -44,7 +44,9 @@ INSTALLED_APPS = [ 'djoser', 'subjects', 'corsheaders', - 'students' + 'students', + 'professors', + 'schedules', ] MIDDLEWARE = [ diff --git a/infotech/professors/__init__.py b/infotech/professors/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/infotech/professors/admin.py b/infotech/professors/admin.py new file mode 100644 index 0000000..cc161ae --- /dev/null +++ b/infotech/professors/admin.py @@ -0,0 +1,6 @@ +from django.contrib import admin +from .models import Professor + +# Register your models here. + +admin.site.register(Professor) diff --git a/infotech/professors/apps.py b/infotech/professors/apps.py new file mode 100644 index 0000000..40c3e75 --- /dev/null +++ b/infotech/professors/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ProfessorsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'professors' diff --git a/infotech/professors/migrations/0001_initial.py b/infotech/professors/migrations/0001_initial.py new file mode 100644 index 0000000..439f074 --- /dev/null +++ b/infotech/professors/migrations/0001_initial.py @@ -0,0 +1,26 @@ +# Generated by Django 4.2 on 2023-04-22 01:17 + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Professor', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('first_name', models.CharField(max_length=40)), + ('last_name', models.CharField(max_length=40)), + ('age', models.IntegerField()), + ('date_joined', models.DateTimeField(default=django.utils.timezone.now, editable=False)), + ('gender', models.CharField(choices=[('Male', 'Male'), ('Female', 'Female')], max_length=20)), + ], + ), + ] diff --git a/infotech/professors/migrations/__init__.py b/infotech/professors/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/infotech/professors/models.py b/infotech/professors/models.py new file mode 100644 index 0000000..052cc35 --- /dev/null +++ b/infotech/professors/models.py @@ -0,0 +1,25 @@ +from django.db import models +from django.utils.timezone import now +# Create your models here. + + +class Professor(models.Model): + + class Genders(models.TextChoices): + MALE = 'Male', + FEMALE = 'Female', + + first_name = models.CharField(max_length=40) + last_name = models.CharField(max_length=40) + age = models.IntegerField() + date_joined = models.DateTimeField(default=now, editable=False) + gender = models.CharField(max_length=20, choices=Genders.choices) + # subjects = models.ManyToManyField( + # 'subjects.Subject', through='subjects.SubjectProfessor') + + @property + def full_name(self): + return f"{self.first_name} {self.last_name}" + + def __str__(self): + return self.full_name diff --git a/infotech/professors/serializers.py b/infotech/professors/serializers.py new file mode 100644 index 0000000..b4b92c1 --- /dev/null +++ b/infotech/professors/serializers.py @@ -0,0 +1,14 @@ +from rest_framework import serializers +from .models import Professor +from subjects.models import Subject + + +class ProfessorSerializer(serializers.HyperlinkedModelSerializer): + date_joined = serializers.DateTimeField( + format="%d-%m-%Y %I:%M%p", read_only=True) + + class Meta: + model = Professor + fields = ('id', 'first_name', + 'last_name', 'age', 'gender', 'date_joined') + read_only_fields = ('id', 'date_joined') diff --git a/infotech/professors/tests.py b/infotech/professors/tests.py new file mode 100644 index 0000000..de8bdc0 --- /dev/null +++ b/infotech/professors/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/infotech/professors/urls.py b/infotech/professors/urls.py new file mode 100644 index 0000000..a944baa --- /dev/null +++ b/infotech/professors/urls.py @@ -0,0 +1,13 @@ +from django.urls import include, path +from rest_framework import routers +from . import views + +router = routers.DefaultRouter() +router.register(r'professors', views.ProfessorViewSet) + +# Wire up our API using automatic URL routing. +# Additionally, we include login URLs for the browsable API. +urlpatterns = [ + path('', include(router.urls)), + +] \ No newline at end of file diff --git a/infotech/professors/views.py b/infotech/professors/views.py new file mode 100644 index 0000000..de6e102 --- /dev/null +++ b/infotech/professors/views.py @@ -0,0 +1,10 @@ +from rest_framework.permissions import IsAuthenticated +from rest_framework import viewsets +from .serializers import ProfessorSerializer +from .models import Professor + + +class ProfessorViewSet(viewsets.ModelViewSet): + # permission_classes = [IsAuthenticated] + serializer_class = ProfessorSerializer + queryset = Professor.objects.all().order_by('date_joined') diff --git a/infotech/schedules/__init__.py b/infotech/schedules/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/infotech/schedules/admin.py b/infotech/schedules/admin.py new file mode 100644 index 0000000..ea5d68b --- /dev/null +++ b/infotech/schedules/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/infotech/schedules/apps.py b/infotech/schedules/apps.py new file mode 100644 index 0000000..f7840a2 --- /dev/null +++ b/infotech/schedules/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class SchedulesConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'schedules' diff --git a/infotech/schedules/migrations/0001_initial.py b/infotech/schedules/migrations/0001_initial.py new file mode 100644 index 0000000..11dff5f --- /dev/null +++ b/infotech/schedules/migrations/0001_initial.py @@ -0,0 +1,46 @@ +# Generated by Django 4.2 on 2023-04-22 02:49 + +from django.db import migrations, models +import django.db.models.deletion +import django.utils.timezone + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('subjects', '0006_remove_subject_students'), + ('professors', '0001_initial'), + ('students', '0004_student_enrolled_subjects'), + ] + + operations = [ + migrations.CreateModel( + name='Schedule', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('date_created', models.DateTimeField(default=django.utils.timezone.now, editable=False)), + ('professor', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='Professor_last_name', to='professors.professor')), + ], + ), + migrations.CreateModel( + name='StudentSchedule', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('date_joined', models.DateTimeField(default=django.utils.timezone.now, editable=False)), + ('schedule', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='schedules.schedule')), + ('student_assigned', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='students.student')), + ], + ), + migrations.AddField( + model_name='schedule', + name='students_assigned', + field=models.ManyToManyField(related_name='StudentSchedule_student_assigned', through='schedules.StudentSchedule', to='students.student'), + ), + migrations.AddField( + model_name='schedule', + name='subject', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='subjects.subject'), + ), + ] diff --git a/infotech/schedules/migrations/0002_alter_schedule_professor.py b/infotech/schedules/migrations/0002_alter_schedule_professor.py new file mode 100644 index 0000000..6e8464d --- /dev/null +++ b/infotech/schedules/migrations/0002_alter_schedule_professor.py @@ -0,0 +1,20 @@ +# Generated by Django 4.2 on 2023-04-22 02:53 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('professors', '0001_initial'), + ('schedules', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='schedule', + name='professor', + field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='Professor_full_name', to='professors.professor'), + ), + ] diff --git a/infotech/schedules/migrations/__init__.py b/infotech/schedules/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/infotech/schedules/models.py b/infotech/schedules/models.py new file mode 100644 index 0000000..5787f7a --- /dev/null +++ b/infotech/schedules/models.py @@ -0,0 +1,21 @@ +from django.db import models +from django.utils.timezone import now + +# Create your models here. + + +class Schedule(models.Model): + subject = models.ForeignKey('subjects.Subject', on_delete=models.CASCADE) + students_assigned = models.ManyToManyField( + 'students.Student', related_name='StudentSchedule_student_assigned', through='schedules.StudentSchedule') + professor = models.OneToOneField( + 'professors.Professor', related_name='Professor_full_name', on_delete=models.CASCADE) + date_created = models.DateTimeField(default=now, editable=False) + + +class StudentSchedule(models.Model): + schedule = models.ForeignKey( + 'schedules.Schedule', on_delete=models.CASCADE) + student_assigned = models.ForeignKey( + 'students.Student', on_delete=models.CASCADE) + date_joined = models.DateTimeField(default=now, editable=False) diff --git a/infotech/schedules/serializers.py b/infotech/schedules/serializers.py new file mode 100644 index 0000000..3017a56 --- /dev/null +++ b/infotech/schedules/serializers.py @@ -0,0 +1,13 @@ +from rest_framework import serializers +from .models import Schedule + + +class ScheduleSerializer(serializers.HyperlinkedModelSerializer): + date_created = serializers.DateTimeField( + format="%d-%m-%Y %I:%M%p", read_only=True) + + class Meta: + model = Schedule + fields = ('id', 'subject', 'students_assigned', + 'professor', 'date_created') + read_only_fields = ('id', 'date_created') diff --git a/infotech/schedules/tests.py b/infotech/schedules/tests.py new file mode 100644 index 0000000..de8bdc0 --- /dev/null +++ b/infotech/schedules/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/infotech/schedules/urls.py b/infotech/schedules/urls.py new file mode 100644 index 0000000..dd7d75e --- /dev/null +++ b/infotech/schedules/urls.py @@ -0,0 +1,13 @@ +from django.urls import include, path +from rest_framework import routers +from . import views + +router = routers.DefaultRouter() +router.register(r'schedules', views.ScheduleViewSet) + +# Wire up our API using automatic URL routing. +# Additionally, we include login URLs for the browsable API. +urlpatterns = [ + path('', include(router.urls)), + +] diff --git a/infotech/schedules/views.py b/infotech/schedules/views.py new file mode 100644 index 0000000..bbfe8f7 --- /dev/null +++ b/infotech/schedules/views.py @@ -0,0 +1,13 @@ +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 + + +class ScheduleViewSet(viewsets.ModelViewSet): + # permission_classes = [IsAuthenticated] + serializer_class = ScheduleSerializer + queryset = Schedule.objects.all() diff --git a/infotech/students/migrations/0005_remove_student_enrolled_subjects.py b/infotech/students/migrations/0005_remove_student_enrolled_subjects.py new file mode 100644 index 0000000..8ce957b --- /dev/null +++ b/infotech/students/migrations/0005_remove_student_enrolled_subjects.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2 on 2023-04-22 02:58 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('students', '0004_student_enrolled_subjects'), + ] + + operations = [ + migrations.RemoveField( + model_name='student', + name='enrolled_subjects', + ), + ] diff --git a/infotech/students/models.py b/infotech/students/models.py index af36211..d15539c 100644 --- a/infotech/students/models.py +++ b/infotech/students/models.py @@ -36,8 +36,8 @@ class Student(models.Model): clearance_done = models.BooleanField() pta_done = models.BooleanField() # - enrolled_subjects = models.ManyToManyField( - 'subjects.Subject', through='subjects.SubjectStudent') + # enrolled_subjects = models.ManyToManyField( + # 'subjects.Subject', through='subjects.SubjectStudent') year_level = models.CharField(max_length=20, choices=YearLevels.choices) current_semester = models.CharField( max_length=20, choices=Semesters.choices, default=Semesters.FIRST_SEM) diff --git a/infotech/students/serializers.py b/infotech/students/serializers.py index f49a23f..7b91c69 100644 --- a/infotech/students/serializers.py +++ b/infotech/students/serializers.py @@ -5,8 +5,6 @@ from subjects.models import Subject class StudentSerializer(serializers.HyperlinkedModelSerializer): - enrolled_subjects = serializers.SlugRelatedField( - queryset=Subject.objects.all(), many=True, slug_field='name', allow_null=True) class Meta: model = Student @@ -15,6 +13,6 @@ class StudentSerializer(serializers.HyperlinkedModelSerializer): 'address', 'birthplace', 'mother_name', 'father_name', 'registrar_done', 'clearance_done', 'pta_done', - 'enrolled_subjects', 'year_level', 'current_semester' + 'year_level', 'current_semester' ] read_only_fields = ['id'] diff --git a/infotech/subjects/migrations/0005_subject_code_alter_subject_semester_and_more.py b/infotech/subjects/migrations/0005_subject_code_alter_subject_semester_and_more.py new file mode 100644 index 0000000..36f4074 --- /dev/null +++ b/infotech/subjects/migrations/0005_subject_code_alter_subject_semester_and_more.py @@ -0,0 +1,29 @@ +# Generated by Django 4.2 on 2023-04-22 01:13 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('subjects', '0004_remove_subject_enrolled_count_subjectstudent_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='subject', + name='code', + field=models.CharField(default='PLCHLDER-1', max_length=20), + preserve_default=False, + ), + migrations.AlterField( + model_name='subject', + name='semester', + field=models.CharField(choices=[('1st Semester', 'First Sem'), ('2nd Semester', 'Second Sem')], default='1st Semester', max_length=20), + ), + migrations.AlterField( + model_name='subject', + name='year_level', + field=models.CharField(choices=[('1st Year', 'First Year'), ('2nd Year', 'Second Year'), ('3rd Year', 'Third Year'), ('4th Year', 'Fourth Year')], max_length=20), + ), + ] diff --git a/infotech/subjects/migrations/0006_remove_subject_students.py b/infotech/subjects/migrations/0006_remove_subject_students.py new file mode 100644 index 0000000..6b436e8 --- /dev/null +++ b/infotech/subjects/migrations/0006_remove_subject_students.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2 on 2023-04-22 02:49 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('subjects', '0005_subject_code_alter_subject_semester_and_more'), + ] + + operations = [ + migrations.RemoveField( + model_name='subject', + name='students', + ), + ] diff --git a/infotech/subjects/migrations/0007_delete_subjectstudent.py b/infotech/subjects/migrations/0007_delete_subjectstudent.py new file mode 100644 index 0000000..7764c88 --- /dev/null +++ b/infotech/subjects/migrations/0007_delete_subjectstudent.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2 on 2023-04-22 02:58 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('students', '0005_remove_student_enrolled_subjects'), + ('subjects', '0006_remove_subject_students'), + ] + + operations = [ + migrations.DeleteModel( + name='SubjectStudent', + ), + ] diff --git a/infotech/subjects/models.py b/infotech/subjects/models.py index e0e81a7..d0b4199 100644 --- a/infotech/subjects/models.py +++ b/infotech/subjects/models.py @@ -12,24 +12,15 @@ class Subject(models.Model): FOURTH_YEAR = '4th Year' class Semesters(models.TextChoices): - FIRST_SEM = 'Sem-1', '1st Semester' - SECOND_SEM = 'Sem-2', '2nd Semester' + FIRST_SEM = '1st Semester' + SECOND_SEM = '2nd Semester' 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) - students = models.ManyToManyField( - 'students.Student', related_name='SubjectStudent_student_assigned', through='subjects.SubjectStudent') - def __str__(self): return self.name - - -class SubjectStudent(models.Model): - subject = models.ForeignKey('subjects.Subject', on_delete=models.CASCADE) - student_assigned = models.ForeignKey( - 'students.Student', on_delete=models.CASCADE, null=True) - date_joined = models.DateTimeField(default=now, editable=False) diff --git a/infotech/subjects/serializers.py b/infotech/subjects/serializers.py index 8ef6883..8deffe7 100644 --- a/infotech/subjects/serializers.py +++ b/infotech/subjects/serializers.py @@ -5,11 +5,9 @@ from students.models import Student class SubjectSerializer(serializers.HyperlinkedModelSerializer): - students = serializers.SlugRelatedField( - queryset=Student.objects.all(), many=True, slug_field='last_name', allow_null=True) class Meta: model = Subject - fields = ('id', 'name', 'students', + fields = ('id', 'name', 'max_slots', 'year_level', 'semester') - read_only_fields = ('id',) + read_only_fields = ['id']