Automatically add subjects based on a .csv file

This commit is contained in:
Keannu Bernasol 2023-07-18 20:25:03 +08:00
parent c10ef2d784
commit 33e8218e51
8 changed files with 225 additions and 14 deletions

View file

@ -0,0 +1,23 @@
# 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),
),
]

View file

@ -5,8 +5,8 @@ from django.db import models
class Course(models.Model):
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)
subjects = models.ManyToManyField(
'subjects.Subject', related_name='SubjectCourse_course', through='subjects.SubjectCourse')

View file

@ -0,0 +1,23 @@
# 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),
),
]

View file

@ -6,8 +6,8 @@ from django.dispatch import receiver
class Semester(models.Model):
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)
def __str__(self):
return self.name

View file

@ -0,0 +1,76 @@
# 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')},
),
]

View file

@ -1,12 +1,19 @@
import os
import csv
from django.conf import settings
from django.db import models
from django.db.models.signals import post_migrate
from django.dispatch import receiver
from courses.models import Course
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)
code = models.CharField(max_length=16)
name = models.CharField(max_length=64, unique=True)
code = models.CharField(max_length=16, unique=True)
courses = models.ManyToManyField(
'courses.Course', through='subjects.SubjectCourse', related_name='SubjectCourse_subject')
students = models.ManyToManyField(
@ -23,9 +30,10 @@ class Subject(models.Model):
class SubjectCourse(models.Model):
subject = models.ForeignKey('subjects.Subject', on_delete=models.CASCADE)
subject = models.ForeignKey(
'subjects.Subject', on_delete=models.CASCADE, to_field='name')
course = models.ForeignKey(
'courses.Course', on_delete=models.CASCADE, null=True)
'courses.Course', on_delete=models.CASCADE, null=True, to_field='name')
def __str__(self):
return f'Subject={self.subject.name}, Course={self.course.name}'
@ -36,9 +44,9 @@ class SubjectCourse(models.Model):
class SubjectYearLevel(models.Model):
subject = models.ForeignKey(
'subjects.Subject', on_delete=models.CASCADE)
'subjects.Subject', on_delete=models.CASCADE, to_field='name')
year_level = models.ForeignKey(
'year_levels.Year_Level', on_delete=models.CASCADE)
'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}'
@ -49,12 +57,70 @@ class SubjectYearLevel(models.Model):
class SubjectSemester(models.Model):
subject = models.ForeignKey(
'subjects.Subject', on_delete=models.CASCADE)
'subjects.Subject', on_delete=models.CASCADE, to_field='name')
semester = models.ForeignKey(
'semesters.Semester', on_delete=models.CASCADE)
'semesters.Semester', on_delete=models.CASCADE, to_field='name')
def __str__(self):
return f'Subject={self.subject.name}, Semester={self.semester.name}'
class Meta:
unique_together = [['subject', 'semester']]
# Create subjects on initial migrate
@receiver(post_migrate)
def populate_subjects(sender, **kwargs):
if sender.name == 'subjects':
root_path = os.path.join(settings.MEDIA_ROOT, 'records')
csv_files = [f for f in os.listdir(root_path) if f.endswith('.csv')]
for csv_file in csv_files:
csv_file_path = os.path.join(root_path, csv_file)
# Filename contains course of subjects
filename = os.path.splitext(csv_file)[0]
print('Reading subjects from', filename)
with open(csv_file_path, newline='') as csvfile:
reader = csv.reader(csvfile)
next(reader) # Skip the header row
for row in reader:
if not any(row):
continue
# Get subject information
year_term = row[0].split('-')
subject_year_level = year_term[0].strip()
subject_semester = year_term[1].strip()
subject_code = row[1]
subject_name = row[2]
# Skip ROTC/NSTP Subjects
if (subject_code is 'NSTP102,ROTC/CWTS/LTS 2'):
continue
# Skip Practicum Subjects
if ('PRACTICUM' in subject_name):
continue
# Skip Capstone Subjects
if ('CAPSTONE' in subject_name):
continue
course = Course.objects.filter(
name=filename).first()
year_level = Year_Level.objects.filter(
name=subject_year_level).first()
semester = Semester.objects.filter(
name=subject_semester).first()
# Create the subject instance or get if it already exists
SUBJECT = Subject.objects.get_or_create(
name=subject_name,
code=subject_code,
)
# Set the course, year level, and semester of the subject
SUBJECT[0].courses.set([course])
SUBJECT[0].year_levels.set([year_level])
SUBJECT[0].semesters.set([semester])

View file

@ -0,0 +1,23 @@
# 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),
),
]

View file

@ -6,8 +6,8 @@ from django.dispatch import receiver
class Year_Level(models.Model):
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)
def __str__(self):
return self.name