Fixed custom user serializer

This commit is contained in:
Keannu Bernasol 2023-08-10 17:11:34 +08:00
parent f6ebd91f85
commit f6f87415d5

View file

@ -69,40 +69,55 @@ class CustomUserSerializer(BaseUserSerializer):
return instance.semester.shortname if instance.semester else None return instance.semester.shortname if instance.semester else None
def update(self, instance, validated_data): def update(self, instance, validated_data):
# First, we'll remove all the existing subjects from the user # If irregular is updated and changed in PATCH request
print(validated_data) if 'irregular' in validated_data and validated_data['irregular'] is not instance.irregular:
# If course, year_level, or semester is changed # Record the changes into the DB
if any(field in validated_data for field in ['course', 'year_level', 'semester']): super().update(instance, validated_data)
if ( # Then check if irregular is False
instance.course is not validated_data['course'] or if (instance.irregular is False):
instance.year_level is not validated_data['year_level'] or # And if so, remove all current subjects
instance.semester is not validated_data['semester'] or
instance.irregular is not validated_data['irregular']):
# Clear all subjects
instance.subjects.clear() instance.subjects.clear()
# Update the user instance with the validated data # And add the ones matching the required criteria
instance = super().update(instance, validated_data)
# Then add new subjects matching the new criteria
self.add_subjects(instance) self.add_subjects(instance)
# This is what I want you to do. This comment below! Ignore any other comments # If year_level is updated and changed in PATCH request
# Add another condition here to check if the user recently changed his/her irregular field from true to false. And if it has changed to false from being true, run the same if statement above elif 'year_level' in validated_data and validated_data['year_level'] is not instance.year_level or instance.year_level is None:
# Record the changes into the DB
else:
# Else update as usual
super().update(instance, validated_data) super().update(instance, validated_data)
return instance # If so, remove all current subjects
instance.subjects.clear()
# And add the ones matching the required criteria
self.add_subjects(instance)
# If semester is updated and changed in PATCH request
elif 'semester' in validated_data and validated_data['semester'] is not instance.semester or instance.semester is None:
# Record the changes into the DB
super().update(instance, validated_data)
# If so, remove all current subjects
instance.subjects.clear()
# And add the ones matching the required criteria
self.add_subjects(instance)
# If course is updated and changed in PATCH request
elif 'course' in validated_data and validated_data['course'] is not instance.course or instance.course is None:
# Record the changes into the DB
super().update(instance, validated_data)
# If so, remove all current subjects
instance.subjects.clear()
# And add the ones matching the required criteria
self.add_subjects(instance)
# Finally, update the instance with the validated data and return it
return super().update(instance, validated_data)
def add_subjects(self, instance): def add_subjects(self, instance):
# Get the matching subjects based on the user's course, year level, and semester # Get the matching subjects based on the user's course, year level, and semester
matching_subjects = Subject.objects.filter( matching_subjects = Subject.objects.filter(
course=instance.course, year_level=instance.year_level, semester=instance.semester) course=instance.course, year_level=instance.year_level, semester=instance.semester)
# Add the matching subjects to the user's subjects list # Add the matching subjects to the user's subjects list
print(matching_subjects)
instance.subjects.add(*matching_subjects) instance.subjects.add(*matching_subjects)
# The model from your custom user
class UserRegistrationSerializer(serializers.ModelSerializer): class UserRegistrationSerializer(serializers.ModelSerializer):
email = serializers.EmailField(required=True) email = serializers.EmailField(required=True)