Improved code for uupdating course year level and semester for student and for postmigration generation of subjects per course

This commit is contained in:
Keannu Bernasol 2023-07-18 22:13:31 +08:00
parent 33e8218e51
commit 41b2747495
2 changed files with 25 additions and 12 deletions

View file

@ -48,14 +48,23 @@ class CustomUserSerializer(BaseUserSerializer):
def update(self, instance, validated_data):
# First, we'll remove all the existing subjects from the user
instance.subjects.clear()
print('Clearing user subjects')
print(validated_data)
# If course, year_level, or semester is changed
if ('course' in validated_data or 'year_level' in validated_data or 'semester' in validated_data):
if (instance.course is not validated_data['course'] or
instance.year_level is not validated_data['year_level'] or
instance.semester is not validated_data['semester']):
# Update the user instance with the validated data
instance = super().update(instance, validated_data)
print('Subjects:', instance.subjects)
# Next, we'll add new subjects based on the matching criteria
self.add_subjects(instance)
# Clear all subjects
instance.subjects.clear()
# Update the user instance with the validated data
instance = super().update(instance, validated_data)
# Then add new subjects matching the new criteria
self.add_subjects(instance)
# Else update as usual
else:
instance = super().update(instance, validated_data)
return instance
@ -64,7 +73,6 @@ class CustomUserSerializer(BaseUserSerializer):
matching_subjects = Subject.objects.filter(
courses=instance.course, year_levels=instance.year_level, semesters=instance.semester)
# Add the matching subjects to the user's subjects list
print('Mathing subjects', matching_subjects)
instance.subjects.add(*matching_subjects)
# The model from your custom user

View file

@ -84,7 +84,7 @@ def populate_subjects(sender, **kwargs):
reader = csv.reader(csvfile)
next(reader) # Skip the header row
subject_count = 0
for row in reader:
if not any(row):
continue
@ -97,15 +97,18 @@ def populate_subjects(sender, **kwargs):
subject_name = row[2]
# Skip ROTC/NSTP Subjects
if (subject_code is 'NSTP102,ROTC/CWTS/LTS 2'):
if ('NSTP' in subject_code or 'ROTC' in subject_code or 'CWTS' in subject_code or 'LTS' in subject_code):
print('NSTP/ROTC subject', subject_name, 'omitted...')
continue
# Skip Practicum Subjects
if ('PRACTICUM' in subject_name):
if ('PRACTICUM' in subject_name or 'On the Job Training' in subject_name):
print('OJT subject', subject_name, 'omitted...')
continue
# Skip Capstone Subjects
if ('CAPSTONE' in subject_name):
if ('CAPSTONE' in subject_name or 'Capstone' in subject_name):
print('Capstone subject', subject_name, 'omitted...')
continue
course = Course.objects.filter(
@ -124,3 +127,5 @@ def populate_subjects(sender, **kwargs):
SUBJECT[0].courses.set([course])
SUBJECT[0].year_levels.set([year_level])
SUBJECT[0].semesters.set([semester])
subject_count += 1
print('Added', subject_count, 'subjects from', filename)