mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2024-11-17 06:19:24 +08:00
Improved code for uupdating course year level and semester for student and for postmigration generation of subjects per course
This commit is contained in:
parent
33e8218e51
commit
41b2747495
2 changed files with 25 additions and 12 deletions
|
@ -48,14 +48,23 @@ class CustomUserSerializer(BaseUserSerializer):
|
||||||
|
|
||||||
def update(self, instance, validated_data):
|
def update(self, instance, validated_data):
|
||||||
# First, we'll remove all the existing subjects from the user
|
# First, we'll remove all the existing subjects from the user
|
||||||
instance.subjects.clear()
|
print(validated_data)
|
||||||
print('Clearing user subjects')
|
# 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
|
# Clear all subjects
|
||||||
instance = super().update(instance, validated_data)
|
instance.subjects.clear()
|
||||||
print('Subjects:', instance.subjects)
|
# Update the user instance with the validated data
|
||||||
# Next, we'll add new subjects based on the matching criteria
|
instance = super().update(instance, validated_data)
|
||||||
self.add_subjects(instance)
|
# 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
|
return instance
|
||||||
|
|
||||||
|
@ -64,7 +73,6 @@ class CustomUserSerializer(BaseUserSerializer):
|
||||||
matching_subjects = Subject.objects.filter(
|
matching_subjects = Subject.objects.filter(
|
||||||
courses=instance.course, year_levels=instance.year_level, semesters=instance.semester)
|
courses=instance.course, year_levels=instance.year_level, semesters=instance.semester)
|
||||||
# Add the matching subjects to the user's subjects list
|
# Add the matching subjects to the user's subjects list
|
||||||
print('Mathing subjects', matching_subjects)
|
|
||||||
instance.subjects.add(*matching_subjects)
|
instance.subjects.add(*matching_subjects)
|
||||||
|
|
||||||
# The model from your custom user
|
# The model from your custom user
|
||||||
|
|
|
@ -84,7 +84,7 @@ def populate_subjects(sender, **kwargs):
|
||||||
|
|
||||||
reader = csv.reader(csvfile)
|
reader = csv.reader(csvfile)
|
||||||
next(reader) # Skip the header row
|
next(reader) # Skip the header row
|
||||||
|
subject_count = 0
|
||||||
for row in reader:
|
for row in reader:
|
||||||
if not any(row):
|
if not any(row):
|
||||||
continue
|
continue
|
||||||
|
@ -97,15 +97,18 @@ def populate_subjects(sender, **kwargs):
|
||||||
subject_name = row[2]
|
subject_name = row[2]
|
||||||
|
|
||||||
# Skip ROTC/NSTP Subjects
|
# 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
|
continue
|
||||||
|
|
||||||
# Skip Practicum Subjects
|
# 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
|
continue
|
||||||
|
|
||||||
# Skip Capstone Subjects
|
# 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
|
continue
|
||||||
|
|
||||||
course = Course.objects.filter(
|
course = Course.objects.filter(
|
||||||
|
@ -124,3 +127,5 @@ def populate_subjects(sender, **kwargs):
|
||||||
SUBJECT[0].courses.set([course])
|
SUBJECT[0].courses.set([course])
|
||||||
SUBJECT[0].year_levels.set([year_level])
|
SUBJECT[0].year_levels.set([year_level])
|
||||||
SUBJECT[0].semesters.set([semester])
|
SUBJECT[0].semesters.set([semester])
|
||||||
|
subject_count += 1
|
||||||
|
print('Added', subject_count, 'subjects from', filename)
|
||||||
|
|
Loading…
Reference in a new issue