From 41b2747495c8faa72d5599cb8297feab8d05c92f Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Tue, 18 Jul 2023 22:13:31 +0800 Subject: [PATCH] Improved code for uupdating course year level and semester for student and for postmigration generation of subjects per course --- stude/accounts/serializers.py | 24 ++++++++++++++++-------- stude/subjects/models.py | 13 +++++++++---- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/stude/accounts/serializers.py b/stude/accounts/serializers.py index 4a9ae36..2341c11 100644 --- a/stude/accounts/serializers.py +++ b/stude/accounts/serializers.py @@ -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 diff --git a/stude/subjects/models.py b/stude/subjects/models.py index de3a294..71ed920 100644 --- a/stude/subjects/models.py +++ b/stude/subjects/models.py @@ -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)