Initial transition to move relationships from subject to schedules

This commit is contained in:
keannu125 2023-04-22 11:00:04 +08:00
parent 392bf195ae
commit 134df378c3
31 changed files with 334 additions and 22 deletions

View file

@ -4,5 +4,7 @@ from django.urls import path, include
urlpatterns = [ urlpatterns = [
path('', include('subjects.urls')), path('', include('subjects.urls')),
path('', include('students.urls')), path('', include('students.urls')),
path('', include('professors.urls')),
path('', include('schedules.urls')),
path('accounts/', include('accounts.urls')), path('accounts/', include('accounts.urls')),
] ]

View file

@ -44,7 +44,9 @@ INSTALLED_APPS = [
'djoser', 'djoser',
'subjects', 'subjects',
'corsheaders', 'corsheaders',
'students' 'students',
'professors',
'schedules',
] ]
MIDDLEWARE = [ MIDDLEWARE = [

View file

View file

@ -0,0 +1,6 @@
from django.contrib import admin
from .models import Professor
# Register your models here.
admin.site.register(Professor)

View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class ProfessorsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'professors'

View file

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

View file

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

View file

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

View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View file

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

View file

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

View file

View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class SchedulesConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'schedules'

View file

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

View file

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

View file

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

View file

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

View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View file

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

View file

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

View file

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

View file

@ -36,8 +36,8 @@ class Student(models.Model):
clearance_done = models.BooleanField() clearance_done = models.BooleanField()
pta_done = models.BooleanField() pta_done = models.BooleanField()
# #
enrolled_subjects = models.ManyToManyField( # enrolled_subjects = models.ManyToManyField(
'subjects.Subject', through='subjects.SubjectStudent') # 'subjects.Subject', through='subjects.SubjectStudent')
year_level = models.CharField(max_length=20, choices=YearLevels.choices) year_level = models.CharField(max_length=20, choices=YearLevels.choices)
current_semester = models.CharField( current_semester = models.CharField(
max_length=20, choices=Semesters.choices, default=Semesters.FIRST_SEM) max_length=20, choices=Semesters.choices, default=Semesters.FIRST_SEM)

View file

@ -5,8 +5,6 @@ from subjects.models import Subject
class StudentSerializer(serializers.HyperlinkedModelSerializer): class StudentSerializer(serializers.HyperlinkedModelSerializer):
enrolled_subjects = serializers.SlugRelatedField(
queryset=Subject.objects.all(), many=True, slug_field='name', allow_null=True)
class Meta: class Meta:
model = Student model = Student
@ -15,6 +13,6 @@ class StudentSerializer(serializers.HyperlinkedModelSerializer):
'address', 'birthplace', 'address', 'birthplace',
'mother_name', 'father_name', 'mother_name', 'father_name',
'registrar_done', 'clearance_done', 'pta_done', 'registrar_done', 'clearance_done', 'pta_done',
'enrolled_subjects', 'year_level', 'current_semester' 'year_level', 'current_semester'
] ]
read_only_fields = ['id'] read_only_fields = ['id']

View file

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

View file

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

View file

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

View file

@ -12,24 +12,15 @@ class Subject(models.Model):
FOURTH_YEAR = '4th Year' FOURTH_YEAR = '4th Year'
class Semesters(models.TextChoices): class Semesters(models.TextChoices):
FIRST_SEM = 'Sem-1', '1st Semester' FIRST_SEM = '1st Semester'
SECOND_SEM = 'Sem-2', '2nd Semester' SECOND_SEM = '2nd Semester'
name = models.CharField(max_length=40) name = models.CharField(max_length=40)
code = models.CharField(max_length=20)
max_slots = models.IntegerField(default=60) max_slots = models.IntegerField(default=60)
year_level = models.CharField(max_length=20, choices=YearLevels.choices) year_level = models.CharField(max_length=20, choices=YearLevels.choices)
semester = models.CharField( semester = models.CharField(
max_length=20, choices=Semesters.choices, default=Semesters.FIRST_SEM) 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): def __str__(self):
return self.name 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)

View file

@ -5,11 +5,9 @@ from students.models import Student
class SubjectSerializer(serializers.HyperlinkedModelSerializer): class SubjectSerializer(serializers.HyperlinkedModelSerializer):
students = serializers.SlugRelatedField(
queryset=Student.objects.all(), many=True, slug_field='last_name', allow_null=True)
class Meta: class Meta:
model = Subject model = Subject
fields = ('id', 'name', 'students', fields = ('id', 'name',
'max_slots', 'year_level', 'semester') 'max_slots', 'year_level', 'semester')
read_only_fields = ('id',) read_only_fields = ['id']