mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2024-11-17 06:19:24 +08:00
Overhauled the entire subjects app to be more simplified and to remove anomalies when filtering in queries
This commit is contained in:
parent
4b82ffbbc2
commit
0eff07a1ae
25 changed files with 122 additions and 407 deletions
|
@ -1,9 +1,10 @@
|
|||
# Generated by Django 4.2.3 on 2023-07-18 07:43
|
||||
# Generated by Django 4.2.3 on 2023-07-26 03:53
|
||||
|
||||
import accounts.models
|
||||
import django.contrib.auth.models
|
||||
import django.contrib.auth.validators
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import django.utils.timezone
|
||||
|
||||
|
||||
|
@ -12,6 +13,9 @@ class Migration(migrations.Migration):
|
|||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('courses', '0001_initial'),
|
||||
('auth', '0012_alter_user_first_name_max_length'),
|
||||
('semesters', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
|
@ -34,6 +38,9 @@ class Migration(migrations.Migration):
|
|||
('is_banned', models.BooleanField(default=False)),
|
||||
('student_id_number', models.CharField(max_length=16, validators=[accounts.models.validate_student_id])),
|
||||
('avatar', models.ImageField(null=True, upload_to=accounts.models.CustomUser._get_upload_to)),
|
||||
('course', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='courses.course')),
|
||||
('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')),
|
||||
('semester', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='semesters.semester')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'user',
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# Generated by Django 4.2.3 on 2023-07-18 07:43
|
||||
# Generated by Django 4.2.3 on 2023-07-26 03:53
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
@ -9,34 +9,17 @@ class Migration(migrations.Migration):
|
|||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('auth', '0012_alter_user_first_name_max_length'),
|
||||
('year_levels', '0001_initial'),
|
||||
('accounts', '0001_initial'),
|
||||
('semesters', '0001_initial'),
|
||||
('courses', '0002_initial'),
|
||||
('subjects', '0001_initial'),
|
||||
('accounts', '0001_initial'),
|
||||
('auth', '0012_alter_user_first_name_max_length'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='customuser',
|
||||
name='course',
|
||||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='courses.course'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='customuser',
|
||||
name='groups',
|
||||
field=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'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='customuser',
|
||||
name='semester',
|
||||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='semesters.semester'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='customuser',
|
||||
name='subjects',
|
||||
field=models.ManyToManyField(to='subjects.subject'),
|
||||
field=models.ManyToManyField(to='subjects.subjectinstance'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='customuser',
|
||||
|
|
|
@ -53,7 +53,7 @@ class CustomUser(AbstractUser):
|
|||
on_delete=models.SET_NULL,
|
||||
null=True
|
||||
)
|
||||
subjects = models.ManyToManyField('subjects.Subject')
|
||||
subjects = models.ManyToManyField('subjects.SubjectInstance')
|
||||
|
||||
@property
|
||||
def full_name(self):
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# Generated by Django 4.2.3 on 2023-07-18 07:43
|
||||
# Generated by Django 4.2.3 on 2023-07-26 03:53
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
@ -15,8 +15,8 @@ class Migration(migrations.Migration):
|
|||
name='Course',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(max_length=64)),
|
||||
('shortname', models.CharField(max_length=16)),
|
||||
('name', models.CharField(max_length=64, unique=True)),
|
||||
('shortname', models.CharField(max_length=16, unique=True)),
|
||||
],
|
||||
),
|
||||
]
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
# Generated by Django 4.2.3 on 2023-07-18 07:43
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('courses', '0001_initial'),
|
||||
('subjects', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='course',
|
||||
name='subjects',
|
||||
field=models.ManyToManyField(related_name='SubjectCourse_course', through='subjects.SubjectCourse', to='subjects.subject'),
|
||||
),
|
||||
]
|
|
@ -1,23 +0,0 @@
|
|||
# Generated by Django 4.2.3 on 2023-07-18 10:28
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('courses', '0002_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='course',
|
||||
name='name',
|
||||
field=models.CharField(max_length=64, unique=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='course',
|
||||
name='shortname',
|
||||
field=models.CharField(max_length=16, unique=True),
|
||||
),
|
||||
]
|
|
@ -9,8 +9,6 @@ from django.db import models
|
|||
class Course(models.Model):
|
||||
name = models.CharField(max_length=64, unique=True)
|
||||
shortname = models.CharField(max_length=16, unique=True)
|
||||
subjects = models.ManyToManyField(
|
||||
'subjects.Subject', related_name='SubjectCourse_course', through='subjects.SubjectCourse')
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# Generated by Django 4.2.3 on 2023-07-18 07:43
|
||||
# Generated by Django 4.2.3 on 2023-07-26 03:53
|
||||
|
||||
import django.contrib.gis.db.models.fields
|
||||
from django.db import migrations, models
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# Generated by Django 4.2.3 on 2023-07-18 07:43
|
||||
# Generated by Django 4.2.3 on 2023-07-26 03:53
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
@ -15,8 +15,8 @@ class Migration(migrations.Migration):
|
|||
name='Semester',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(max_length=64)),
|
||||
('shortname', models.CharField(max_length=16)),
|
||||
('name', models.CharField(max_length=64, unique=True)),
|
||||
('shortname', models.CharField(max_length=16, unique=True)),
|
||||
],
|
||||
),
|
||||
]
|
||||
|
|
|
@ -1,23 +0,0 @@
|
|||
# Generated by Django 4.2.3 on 2023-07-18 10:28
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('semesters', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='semester',
|
||||
name='name',
|
||||
field=models.CharField(max_length=64, unique=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='semester',
|
||||
name='shortname',
|
||||
field=models.CharField(max_length=16, unique=True),
|
||||
),
|
||||
]
|
|
@ -1,4 +1,4 @@
|
|||
# Generated by Django 4.2.3 on 2023-07-18 07:43
|
||||
# Generated by Django 4.2.3 on 2023-07-26 03:53
|
||||
|
||||
from django.conf import settings
|
||||
import django.contrib.gis.db.models.fields
|
||||
|
@ -11,8 +11,8 @@ class Migration(migrations.Migration):
|
|||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('accounts', '0002_initial'),
|
||||
('landmarks', '0001_initial'),
|
||||
('accounts', '0002_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# Generated by Django 4.2.3 on 2023-07-18 07:43
|
||||
# Generated by Django 4.2.3 on 2023-07-26 03:53
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
@ -9,8 +9,8 @@ class Migration(migrations.Migration):
|
|||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('study_groups', '0001_initial'),
|
||||
('student_status', '0001_initial'),
|
||||
('study_groups', '0001_initial'),
|
||||
('subjects', '0001_initial'),
|
||||
]
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# Generated by Django 4.2.3 on 2023-07-18 07:43
|
||||
# Generated by Django 4.2.3 on 2023-07-26 03:53
|
||||
|
||||
import django.contrib.gis.db.models.fields
|
||||
from django.db import migrations, models
|
||||
|
|
|
@ -4,7 +4,7 @@ from rest_framework.exceptions import PermissionDenied
|
|||
from rest_framework.permissions import IsAuthenticated
|
||||
from .serializers import StudyGroupSerializer
|
||||
from .models import StudyGroup
|
||||
from subjects.models import SubjectCourse
|
||||
from subjects.models import SubjectInstance
|
||||
# Create your views here.
|
||||
|
||||
|
||||
|
@ -24,15 +24,16 @@ class StudyGroupListView(generics.ListAPIView):
|
|||
# Get the user's course
|
||||
user_course = user.course
|
||||
print(user_course)
|
||||
# Get subject ids related to the user's course through SubjectCourse
|
||||
subject_ids = SubjectCourse.objects.filter(
|
||||
|
||||
# Get subject names related to the user's course
|
||||
subject_names = SubjectInstance.objects.filter(
|
||||
course=user_course
|
||||
).values_list('subject', flat=True)
|
||||
|
||||
print(subject_ids)
|
||||
print(subject_names)
|
||||
|
||||
# Now fetch the StudyGroups with the subjects from the obtained subject_ids
|
||||
studygroups = StudyGroup.objects.filter(subject_id__in=subject_ids)
|
||||
# Now fetch the StudyGroups with the matching subject names
|
||||
studygroups = StudyGroup.objects.filter(subject_name__in=subject_names)
|
||||
return studygroups
|
||||
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# Generated by Django 4.2.3 on 2023-07-18 07:43
|
||||
# Generated by Django 4.2.3 on 2023-07-26 03:53
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
@ -10,8 +10,8 @@ class Migration(migrations.Migration):
|
|||
initial = True
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('study_groups', '0001_initial'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
|
|
|
@ -1,12 +1,6 @@
|
|||
from django.contrib import admin
|
||||
from .models import Subject, SubjectCourse, SubjectSemester, SubjectYearLevel
|
||||
from .models import Subject, SubjectCode, SubjectInstance
|
||||
|
||||
|
||||
class SubjectAdmin(admin.ModelAdmin):
|
||||
filter_horizontal = ['courses']
|
||||
|
||||
|
||||
admin.site.register(Subject, SubjectAdmin)
|
||||
admin.site.register(SubjectCourse)
|
||||
admin.site.register(SubjectSemester)
|
||||
admin.site.register(SubjectYearLevel)
|
||||
admin.site.register(Subject)
|
||||
admin.site.register(SubjectInstance)
|
||||
admin.site.register(SubjectCode)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# Generated by Django 4.2.3 on 2023-07-18 07:43
|
||||
# Generated by Django 4.2.3 on 2023-07-26 03:53
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
@ -10,10 +10,10 @@ class Migration(migrations.Migration):
|
|||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('semesters', '0001_initial'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('courses', '0001_initial'),
|
||||
('year_levels', '0001_initial'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('semesters', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
|
@ -21,52 +21,26 @@ class Migration(migrations.Migration):
|
|||
name='Subject',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(max_length=64)),
|
||||
('code', models.CharField(max_length=16)),
|
||||
('name', models.CharField(max_length=64, unique=True)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='SubjectYearLevel',
|
||||
name='SubjectCode',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('code', models.CharField(max_length=16, unique=True)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='SubjectInstance',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('code', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='subjects.subjectcode')),
|
||||
('course', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='courses.course')),
|
||||
('semester', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='semesters.semester')),
|
||||
('students', models.ManyToManyField(blank=True, to=settings.AUTH_USER_MODEL)),
|
||||
('subject', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='subjects.subject')),
|
||||
('year_level', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='year_levels.year_level')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='SubjectSemester',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('semester', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='semesters.semester')),
|
||||
('subject', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='subjects.subject')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='SubjectCourse',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('course', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='courses.course')),
|
||||
('subject', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='subjects.subject')),
|
||||
],
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='subject',
|
||||
name='courses',
|
||||
field=models.ManyToManyField(related_name='SubjectCourse_subject', through='subjects.SubjectCourse', to='courses.course'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='subject',
|
||||
name='semesters',
|
||||
field=models.ManyToManyField(related_name='SubjectSemester_subject', through='subjects.SubjectSemester', to='semesters.semester'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='subject',
|
||||
name='students',
|
||||
field=models.ManyToManyField(to=settings.AUTH_USER_MODEL),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='subject',
|
||||
name='year_levels',
|
||||
field=models.ManyToManyField(related_name='SubjectYearLevel_subject', through='subjects.SubjectYearLevel', to='year_levels.year_level'),
|
||||
),
|
||||
]
|
||||
|
|
|
@ -1,76 +0,0 @@
|
|||
# Generated by Django 4.2.3 on 2023-07-18 10:28
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('year_levels', '0002_alter_year_level_name_alter_year_level_shortname'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('courses', '0003_alter_course_name_alter_course_shortname'),
|
||||
('semesters', '0002_alter_semester_name_alter_semester_shortname'),
|
||||
('subjects', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='subject',
|
||||
name='code',
|
||||
field=models.CharField(max_length=16, unique=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='subject',
|
||||
name='name',
|
||||
field=models.CharField(max_length=64, unique=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='subject',
|
||||
name='students',
|
||||
field=models.ManyToManyField(blank=True, to=settings.AUTH_USER_MODEL),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='subjectcourse',
|
||||
name='course',
|
||||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='courses.course', to_field='name'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='subjectcourse',
|
||||
name='subject',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='subjects.subject', to_field='name'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='subjectsemester',
|
||||
name='semester',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='semesters.semester', to_field='name'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='subjectsemester',
|
||||
name='subject',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='subjects.subject', to_field='name'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='subjectyearlevel',
|
||||
name='subject',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='subjects.subject', to_field='name'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='subjectyearlevel',
|
||||
name='year_level',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='year_levels.year_level', to_field='name'),
|
||||
),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='subjectcourse',
|
||||
unique_together={('subject', 'course')},
|
||||
),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='subjectsemester',
|
||||
unique_together={('subject', 'semester')},
|
||||
),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='subjectyearlevel',
|
||||
unique_together={('subject', 'year_level')},
|
||||
),
|
||||
]
|
|
@ -1,29 +0,0 @@
|
|||
# Generated by Django 4.2.3 on 2023-07-19 07:04
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('subjects', '0002_alter_subject_code_alter_subject_name_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='SubjectCode',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('code', models.CharField(max_length=16, unique=True)),
|
||||
],
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='subject',
|
||||
name='code',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='subject',
|
||||
name='code',
|
||||
field=models.ManyToManyField(to='subjects.subjectcode'),
|
||||
),
|
||||
]
|
|
@ -1,18 +0,0 @@
|
|||
# Generated by Django 4.2.3 on 2023-07-19 07:09
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('subjects', '0003_subjectcode_remove_subject_code_subject_code'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameField(
|
||||
model_name='subject',
|
||||
old_name='code',
|
||||
new_name='codes',
|
||||
),
|
||||
]
|
|
@ -6,29 +6,16 @@ from django.db import models
|
|||
from django.db.models.signals import post_migrate
|
||||
from django.dispatch import receiver
|
||||
from courses.models import Course
|
||||
from accounts.models import CustomUser
|
||||
from year_levels.models import Year_Level
|
||||
from semesters.models import Semester
|
||||
# Create your models here.
|
||||
|
||||
|
||||
class Subject(models.Model):
|
||||
name = models.CharField(max_length=64, unique=True)
|
||||
codes = models.ManyToManyField(
|
||||
'subjects.SubjectCode')
|
||||
courses = models.ManyToManyField(
|
||||
'courses.Course', through='subjects.SubjectCourse', related_name='SubjectCourse_subject')
|
||||
students = models.ManyToManyField(
|
||||
'accounts.CustomUser', blank=True)
|
||||
|
||||
year_levels = models.ManyToManyField(
|
||||
'year_levels.Year_Level', through='subjects.SubjectYearLevel', related_name='SubjectYearLevel_subject')
|
||||
|
||||
semesters = models.ManyToManyField(
|
||||
'semesters.Semester', through='subjects.SubjectSemester', related_name='SubjectSemester_subject')
|
||||
|
||||
def __str__(self):
|
||||
code_list = ', '.join(self.codes.values_list('code', flat=True))
|
||||
return f'{self.name} ({code_list})'
|
||||
return f'{self.name}'
|
||||
|
||||
|
||||
class SubjectCode(models.Model):
|
||||
|
@ -38,46 +25,27 @@ class SubjectCode(models.Model):
|
|||
return self.code
|
||||
|
||||
|
||||
class SubjectCourse(models.Model):
|
||||
class SubjectInstance(models.Model):
|
||||
subject = models.ForeignKey(
|
||||
'subjects.Subject', on_delete=models.CASCADE, to_field='name')
|
||||
'subjects.Subject', on_delete=models.CASCADE)
|
||||
students = models.ManyToManyField(
|
||||
CustomUser, blank=True)
|
||||
code = models.ForeignKey(
|
||||
SubjectCode, on_delete=models.CASCADE)
|
||||
course = models.ForeignKey(
|
||||
'courses.Course', on_delete=models.CASCADE, null=True, to_field='name')
|
||||
|
||||
def __str__(self):
|
||||
return f'Subject={self.subject.name}, Course={self.course.name}'
|
||||
|
||||
class Meta:
|
||||
unique_together = [['subject', 'course']]
|
||||
|
||||
|
||||
class SubjectYearLevel(models.Model):
|
||||
subject = models.ForeignKey(
|
||||
'subjects.Subject', on_delete=models.CASCADE, to_field='name')
|
||||
Course, on_delete=models.CASCADE)
|
||||
year_level = models.ForeignKey(
|
||||
'year_levels.Year_Level', on_delete=models.CASCADE, to_field='name')
|
||||
|
||||
def __str__(self):
|
||||
return f'Subject={self.subject.name}, Year Level={self.year_level.name}'
|
||||
|
||||
class Meta:
|
||||
unique_together = [['subject', 'year_level']]
|
||||
|
||||
|
||||
class SubjectSemester(models.Model):
|
||||
subject = models.ForeignKey(
|
||||
'subjects.Subject', on_delete=models.CASCADE, to_field='name')
|
||||
Year_Level, on_delete=models.CASCADE)
|
||||
semester = models.ForeignKey(
|
||||
'semesters.Semester', on_delete=models.CASCADE, to_field='name')
|
||||
Semester, on_delete=models.CASCADE)
|
||||
|
||||
def __str__(self):
|
||||
return f'Subject={self.subject.name}, Semester={self.semester.name}'
|
||||
|
||||
class Meta:
|
||||
unique_together = [['subject', 'semester']]
|
||||
return f'Subject: {self.name}({self.code}) - {self.course.shortname} - {self.year_level} - {self.semester}'
|
||||
|
||||
|
||||
# Create subjects on initial migrate
|
||||
|
||||
|
||||
@receiver(post_migrate)
|
||||
def populate_subjects(sender, **kwargs):
|
||||
if sender.name == 'subjects':
|
||||
|
@ -130,37 +98,35 @@ def populate_subjects(sender, **kwargs):
|
|||
semester = Semester.objects.filter(
|
||||
name=subject_semester).first()
|
||||
|
||||
# If subject already exists with relevant info, skip over it
|
||||
if (Subject.objects.filter(name=subject_name, year_levels=year_level, semesters=semester).exists()):
|
||||
# print('Duplicate subject')
|
||||
existing_subjects += 1
|
||||
continue
|
||||
|
||||
# Else if subject exists without relevant info, add relevant info
|
||||
# If Subject exists
|
||||
if (Subject.objects.filter(name=subject_name).exists()):
|
||||
SUBJECT = Subject.objects.filter(name=subject_name
|
||||
).first()
|
||||
|
||||
SUBJECT.courses.add(course)
|
||||
SUBJECT.year_levels.add(year_level)
|
||||
SUBJECT.semesters.add(semester)
|
||||
SUBJECT_CODE = SubjectCode.objects.get_or_create(
|
||||
code=subject_code)
|
||||
SUBJECT.codes.add(SUBJECT_CODE[0])
|
||||
updated_subjects += 1
|
||||
# If subject instance exists, skip over
|
||||
if (SubjectInstance.objects.filter(subject__name=subject_name, year_level=year_level, semester=semester).exists()):
|
||||
# print('Duplicate subject')
|
||||
existing_subjects += 1
|
||||
continue
|
||||
|
||||
# If no subject instance exists, create one
|
||||
else:
|
||||
SUBJECT = Subject.objects.filter(
|
||||
name=subject_name).first()
|
||||
SUBJECT_CODE, created = SubjectCode.objects.get_or_create(
|
||||
code=subject_code)
|
||||
SUBJECT_INSTANCE = SubjectInstance.objects.get_or_create(
|
||||
subject=SUBJECT, course=course, year_level=year_level, semester=semester, code=SUBJECT_CODE)
|
||||
updated_subjects += 1
|
||||
|
||||
# If subject does not exist at all, then create new subject
|
||||
else:
|
||||
|
||||
SUBJECT = Subject.objects.get_or_create(
|
||||
SUBJECT, created = Subject.objects.get_or_create(
|
||||
name=subject_name,
|
||||
)
|
||||
SUBJECT[0].courses.set([course])
|
||||
SUBJECT[0].year_levels.set([year_level])
|
||||
SUBJECT[0].semesters.set([semester])
|
||||
SUBJECT_CODE = SubjectCode.objects.get_or_create(
|
||||
SUBJECT_CODE, created = SubjectCode.objects.get_or_create(
|
||||
code=subject_code)
|
||||
SUBJECT[0].codes.add(SUBJECT_CODE[0])
|
||||
SUBJECT_INSTANCE = SubjectInstance.objects.get_or_create(
|
||||
subject=SUBJECT, course=course, year_level=year_level, semester=semester, code=SUBJECT_CODE)
|
||||
subject_count += 1
|
||||
|
||||
# Set the course, year level, and semester of the subject
|
||||
|
|
|
@ -1,21 +1,29 @@
|
|||
from rest_framework import serializers
|
||||
from .models import Subject, SubjectCode
|
||||
from .models import Subject, SubjectInstance, SubjectCode
|
||||
from courses.models import Course
|
||||
from year_levels.models import Year_Level
|
||||
from semesters.models import Semester
|
||||
from accounts.models import CustomUser
|
||||
|
||||
|
||||
class SubjectSerializer(serializers.ModelSerializer):
|
||||
year_levels = serializers.SlugRelatedField(
|
||||
queryset=Year_Level.objects.all(), many=True, slug_field='name', allow_null=True)
|
||||
semesters = serializers.SlugRelatedField(
|
||||
queryset=Semester.objects.all(), many=True, slug_field='name', allow_null=True)
|
||||
courses = serializers.SlugRelatedField(
|
||||
queryset=Course.objects.all(), many=True, slug_field='name', allow_null=True)
|
||||
codes = serializers.SlugRelatedField(
|
||||
queryset=SubjectCode.objects.all(), many=True, slug_field='code', allow_null=False)
|
||||
|
||||
class Meta:
|
||||
model = Subject
|
||||
fields = ('id', 'name', 'codes', 'courses', 'year_levels', 'semesters')
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class SubjectInstanceSerializer(serializers.ModelSerializer):
|
||||
subject = serializers.SlugRelatedField(
|
||||
queryset=SubjectCode.objects.all(), slug_field='name', allow_null=False)
|
||||
code = serializers.SlugRelatedField(
|
||||
queryset=SubjectCode.objects.all(), slug_field='code', allow_null=False)
|
||||
year_level = serializers.SlugRelatedField(
|
||||
queryset=Year_Level.objects.all(), slug_field='name', allow_null=True)
|
||||
semester = serializers.SlugRelatedField(
|
||||
queryset=Semester.objects.all(), slug_field='name', allow_null=True)
|
||||
course = serializers.SlugRelatedField(
|
||||
queryset=Course.objects.all(), slug_field='name', allow_null=True)
|
||||
|
||||
class Meta:
|
||||
model = SubjectInstance
|
||||
fields = ('id', 'subject', 'code', 'course', 'year_level', 'semester')
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
from rest_framework import generics, viewsets
|
||||
from .models import Subject
|
||||
from .serializers import SubjectSerializer
|
||||
from .models import Subject, SubjectInstance
|
||||
from .serializers import SubjectSerializer, SubjectInstanceSerializer
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
|
||||
|
@ -11,30 +11,27 @@ class SubjectListView(generics.ListAPIView):
|
|||
|
||||
|
||||
class SubjectByCourseView(generics.ListAPIView):
|
||||
queryset = Subject.objects.all()
|
||||
serializer_class = SubjectSerializer
|
||||
queryset = SubjectInstance.objects.all()
|
||||
serializer_class = SubjectInstanceSerializer
|
||||
|
||||
def get(self, request, course_slug):
|
||||
# Retrieve the subjects based on year level and semester slugs
|
||||
subjects = Subject.objects.filter(
|
||||
courses__shortname=course_slug).distinct()
|
||||
# Retrieve the subjects based on course slug
|
||||
subjects = SubjectInstance.objects.filter(
|
||||
course__shortname=course_slug)
|
||||
|
||||
# Serialize the subjects
|
||||
serializer = SubjectSerializer(subjects, many=True)
|
||||
|
||||
serializer = SubjectInstanceSerializer(subjects, many=True)
|
||||
return Response(serializer.data)
|
||||
|
||||
|
||||
class SubjectByCourseYearSemesterView(generics.ListAPIView):
|
||||
queryset = Subject.objects.all()
|
||||
queryset = SubjectInstance.objects.all()
|
||||
serializer_class = SubjectSerializer
|
||||
|
||||
def get(self, request, course_slug, year_slug, semester_slug):
|
||||
# Retrieve the subjects based on year level and semester slugs
|
||||
subjects = Subject.objects.filter(
|
||||
courses__shortname=course_slug, year_levels__shortname=year_slug, semesters__shortname=semester_slug).distinct()
|
||||
|
||||
subjects = SubjectInstance.objects.filter(
|
||||
course__shortname=course_slug, year_level__shortname=year_slug, semester__shortname=semester_slug)
|
||||
# Serialize the subjects
|
||||
serializer = SubjectSerializer(subjects, many=True)
|
||||
|
||||
serializer = SubjectInstanceSerializer(subjects, many=True)
|
||||
return Response(serializer.data)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# Generated by Django 4.2.3 on 2023-07-18 07:43
|
||||
# Generated by Django 4.2.3 on 2023-07-26 03:53
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
@ -15,8 +15,8 @@ class Migration(migrations.Migration):
|
|||
name='Year_Level',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(max_length=64)),
|
||||
('shortname', models.CharField(max_length=16)),
|
||||
('name', models.CharField(max_length=64, unique=True)),
|
||||
('shortname', models.CharField(max_length=16, unique=True)),
|
||||
],
|
||||
),
|
||||
]
|
||||
|
|
|
@ -1,23 +0,0 @@
|
|||
# Generated by Django 4.2.3 on 2023-07-18 10:28
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('year_levels', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='year_level',
|
||||
name='name',
|
||||
field=models.CharField(max_length=64, unique=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='year_level',
|
||||
name='shortname',
|
||||
field=models.CharField(max_length=16, unique=True),
|
||||
),
|
||||
]
|
Loading…
Reference in a new issue