2023-07-18 20:25:03 +08:00
|
|
|
|
|
|
|
import os
|
|
|
|
import csv
|
|
|
|
from django.conf import settings
|
2023-06-27 18:15:31 +08:00
|
|
|
from django.db import models
|
2023-07-18 20:25:03 +08:00
|
|
|
from django.db.models.signals import post_migrate
|
|
|
|
from django.dispatch import receiver
|
|
|
|
from courses.models import Course
|
2023-06-27 18:15:31 +08:00
|
|
|
from year_levels.models import Year_Level
|
|
|
|
from semesters.models import Semester
|
|
|
|
# Create your models here.
|
|
|
|
|
|
|
|
|
|
|
|
class Subject(models.Model):
|
2023-07-18 20:25:03 +08:00
|
|
|
name = models.CharField(max_length=64, unique=True)
|
2023-07-19 15:24:59 +08:00
|
|
|
codes = models.ManyToManyField(
|
|
|
|
'subjects.SubjectCode')
|
2023-06-27 18:15:31 +08:00
|
|
|
courses = models.ManyToManyField(
|
2023-07-10 17:35:54 +08:00
|
|
|
'courses.Course', through='subjects.SubjectCourse', related_name='SubjectCourse_subject')
|
2023-07-18 17:56:23 +08:00
|
|
|
students = models.ManyToManyField(
|
|
|
|
'accounts.CustomUser', blank=True)
|
2023-06-27 18:15:31 +08:00
|
|
|
|
2023-07-17 21:23:27 +08:00
|
|
|
year_levels = models.ManyToManyField(
|
2023-07-11 15:03:42 +08:00
|
|
|
'year_levels.Year_Level', through='subjects.SubjectYearLevel', related_name='SubjectYearLevel_subject')
|
|
|
|
|
2023-07-17 21:23:27 +08:00
|
|
|
semesters = models.ManyToManyField(
|
2023-07-11 15:03:42 +08:00
|
|
|
'semesters.Semester', through='subjects.SubjectSemester', related_name='SubjectSemester_subject')
|
2023-06-27 18:15:31 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
2023-07-19 15:24:59 +08:00
|
|
|
code_list = ', '.join(self.codes.values_list('code', flat=True))
|
|
|
|
return f'{self.name} ({code_list})'
|
|
|
|
|
|
|
|
|
|
|
|
class SubjectCode(models.Model):
|
|
|
|
code = models.CharField(max_length=16, unique=True)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.code
|
2023-06-28 17:12:14 +08:00
|
|
|
|
|
|
|
|
2023-07-10 17:35:54 +08:00
|
|
|
class SubjectCourse(models.Model):
|
2023-07-18 20:25:03 +08:00
|
|
|
subject = models.ForeignKey(
|
|
|
|
'subjects.Subject', on_delete=models.CASCADE, to_field='name')
|
2023-07-10 17:35:54 +08:00
|
|
|
course = models.ForeignKey(
|
2023-07-18 20:25:03 +08:00
|
|
|
'courses.Course', on_delete=models.CASCADE, null=True, to_field='name')
|
2023-07-10 17:35:54 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
2023-07-11 15:03:42 +08:00
|
|
|
return f'Subject={self.subject.name}, Course={self.course.name}'
|
|
|
|
|
2023-07-18 17:56:23 +08:00
|
|
|
class Meta:
|
|
|
|
unique_together = [['subject', 'course']]
|
|
|
|
|
2023-07-11 15:03:42 +08:00
|
|
|
|
|
|
|
class SubjectYearLevel(models.Model):
|
|
|
|
subject = models.ForeignKey(
|
2023-07-18 20:25:03 +08:00
|
|
|
'subjects.Subject', on_delete=models.CASCADE, to_field='name')
|
2023-07-11 15:03:42 +08:00
|
|
|
year_level = models.ForeignKey(
|
2023-07-18 20:25:03 +08:00
|
|
|
'year_levels.Year_Level', on_delete=models.CASCADE, to_field='name')
|
2023-07-11 15:03:42 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f'Subject={self.subject.name}, Year Level={self.year_level.name}'
|
|
|
|
|
2023-07-18 17:56:23 +08:00
|
|
|
class Meta:
|
|
|
|
unique_together = [['subject', 'year_level']]
|
|
|
|
|
2023-07-11 15:03:42 +08:00
|
|
|
|
|
|
|
class SubjectSemester(models.Model):
|
|
|
|
subject = models.ForeignKey(
|
2023-07-18 20:25:03 +08:00
|
|
|
'subjects.Subject', on_delete=models.CASCADE, to_field='name')
|
2023-07-11 15:03:42 +08:00
|
|
|
semester = models.ForeignKey(
|
2023-07-18 20:25:03 +08:00
|
|
|
'semesters.Semester', on_delete=models.CASCADE, to_field='name')
|
2023-07-11 15:03:42 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f'Subject={self.subject.name}, Semester={self.semester.name}'
|
2023-07-18 17:56:23 +08:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = [['subject', 'semester']]
|
2023-07-18 20:25:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
# 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]
|
2023-07-23 23:19:47 +08:00
|
|
|
print('---', 'Adding Subjects from', filename, '---')
|
2023-07-18 20:25:03 +08:00
|
|
|
with open(csv_file_path, newline='') as csvfile:
|
|
|
|
|
|
|
|
reader = csv.reader(csvfile)
|
|
|
|
next(reader) # Skip the header row
|
2023-07-18 22:13:31 +08:00
|
|
|
subject_count = 0
|
2023-07-19 15:36:06 +08:00
|
|
|
updated_subjects = 0
|
2023-07-19 15:50:47 +08:00
|
|
|
ignored_subjects = 0
|
2023-07-19 22:51:23 +08:00
|
|
|
existing_subjects = 0
|
2023-07-18 20:25:03 +08:00
|
|
|
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]
|
|
|
|
|
2023-07-19 15:50:47 +08:00
|
|
|
# Definitions of subjects to ignore
|
2023-07-18 22:15:42 +08:00
|
|
|
ignored_subject_codes = ['NSTP', 'ROTC', 'CWTS', 'LTS']
|
|
|
|
ignored_subject_names = [
|
|
|
|
'PRACTICUM', 'On the Job Training', 'CAPSTONE', 'Capstone']
|
2023-07-19 15:50:47 +08:00
|
|
|
|
2023-07-18 22:15:42 +08:00
|
|
|
# Skip ignored subjects
|
|
|
|
if any(ignored_code in subject_code for ignored_code in ignored_subject_codes):
|
2023-07-19 15:50:47 +08:00
|
|
|
ignored_subjects += 1
|
2023-07-18 20:25:03 +08:00
|
|
|
continue
|
|
|
|
|
2023-07-18 22:15:42 +08:00
|
|
|
if any(ignored_name in subject_name for ignored_name in ignored_subject_names):
|
2023-07-19 15:50:47 +08:00
|
|
|
ignored_subjects += 1
|
2023-07-18 20:25:03 +08:00
|
|
|
continue
|
|
|
|
|
2023-07-19 15:50:47 +08:00
|
|
|
# Get relevant info for specific subject
|
2023-07-18 20:25:03 +08:00
|
|
|
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()
|
2023-07-19 15:50:47 +08:00
|
|
|
|
|
|
|
# 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')
|
2023-07-19 22:51:23 +08:00
|
|
|
existing_subjects += 1
|
2023-07-19 15:50:47 +08:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Else if subject exists without relevant info, add relevant info
|
2023-07-19 15:24:59 +08:00
|
|
|
if (Subject.objects.filter(name=subject_name).exists()):
|
|
|
|
SUBJECT = Subject.objects.filter(name=subject_name
|
|
|
|
).first()
|
2023-07-19 15:50:47 +08:00
|
|
|
|
2023-07-19 15:24:59 +08:00
|
|
|
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])
|
2023-07-19 15:36:06 +08:00
|
|
|
updated_subjects += 1
|
2023-07-19 15:50:47 +08:00
|
|
|
|
|
|
|
# If subject does not exist at all, then create new subject
|
2023-07-19 15:24:59 +08:00
|
|
|
else:
|
|
|
|
|
|
|
|
SUBJECT = 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(
|
|
|
|
code=subject_code)
|
|
|
|
SUBJECT[0].codes.add(SUBJECT_CODE[0])
|
|
|
|
subject_count += 1
|
2023-07-18 20:25:03 +08:00
|
|
|
|
|
|
|
# Set the course, year level, and semester of the subject
|
2023-07-19 22:51:23 +08:00
|
|
|
print('Skipped', existing_subjects,
|
|
|
|
'already existing subjects')
|
|
|
|
print('Added', subject_count, 'subjects')
|
|
|
|
print('Updated', updated_subjects, 'subjects')
|
2023-07-19 15:50:47 +08:00
|
|
|
print('Ignored', ignored_subjects,
|
2023-07-19 22:51:23 +08:00
|
|
|
'subjects', '\n')
|