Improved subjects serializer

This commit is contained in:
Keannu Bernasol 2023-07-19 15:36:06 +08:00
parent 3f136f8cef
commit 1c35e95f21
2 changed files with 5 additions and 10 deletions

View file

@ -94,6 +94,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 subject_count = 0
updated_subjects = 0
for row in reader: for row in reader:
if not any(row): if not any(row):
continue continue
@ -127,8 +128,6 @@ def populate_subjects(sender, **kwargs):
name=subject_semester).first() name=subject_semester).first()
# Create the subject instance or get if it already exists # Create the subject instance or get if it already exists
if (Subject.objects.filter(name=subject_name).exists()): if (Subject.objects.filter(name=subject_name).exists()):
print('Updating existing subject',
subject_name, subject_code)
SUBJECT = Subject.objects.filter(name=subject_name SUBJECT = Subject.objects.filter(name=subject_name
).first() ).first()
SUBJECT.courses.add(course) SUBJECT.courses.add(course)
@ -137,6 +136,7 @@ def populate_subjects(sender, **kwargs):
SUBJECT_CODE = SubjectCode.objects.get_or_create( SUBJECT_CODE = SubjectCode.objects.get_or_create(
code=subject_code) code=subject_code)
SUBJECT.codes.add(SUBJECT_CODE[0]) SUBJECT.codes.add(SUBJECT_CODE[0])
updated_subjects += 1
else: else:
SUBJECT = Subject.objects.get_or_create( SUBJECT = Subject.objects.get_or_create(
@ -151,4 +151,5 @@ def populate_subjects(sender, **kwargs):
subject_count += 1 subject_count += 1
# Set the course, year level, and semester of the subject # Set the course, year level, and semester of the subject
print('Added', subject_count, 'subjects from', filename) print('Added', subject_count, 'subjects from', filename,)
print('Updated', updated_subjects, 'subjects from', filename)

View file

@ -18,10 +18,4 @@ class SubjectSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Subject model = Subject
fields = ('id', 'name', 'code', 'courses', 'year_levels', 'semesters') fields = ('id', 'name', 'codes', 'courses', 'year_levels', 'semesters')
def get_year_level(self, obj):
return obj.year_level.name
def get_semester(self, obj):
return obj.semester.name