Skip to content

Commit

Permalink
Add serializer for mod_settings response key on User objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jb3 committed May 24, 2024
1 parent b16a2ad commit 7f9844a
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions pydis_site/apps/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
Reminder,
Role,
User,
UserAltRelationship
UserAltRelationship,
UserModSettings
)

class FrozenFieldsMixin:
Expand Down Expand Up @@ -701,11 +702,23 @@ class UserSerializer(ModelSerializer):
# ID field must be explicitly set as the default id field is read-only.
id = IntegerField(min_value=0)

def to_representation(self, instance: User) -> dict:
"""Serialize the user to a dictionary, serializing the moderator settings."""
ret = super().to_representation(instance)

if hasattr(instance, 'mod_settings') and instance.mod_settings is not None:
ret['mod_settings'] = UserModSettingsSerializer(instance.mod_settings).data
else:
del ret['mod_settings']

return ret

class Meta:
"""Metadata defined for the Django REST Framework."""

model = User
fields = ('id', 'name', 'display_name', 'discriminator', 'roles', 'in_guild')
fields = ('id', 'name', 'display_name', 'discriminator', 'roles',
'in_guild', 'mod_settings')
depth = 1
list_serializer_class = UserListSerializer

Expand Down Expand Up @@ -736,6 +749,14 @@ def get_alts(self, user: User) -> list[dict]:
for alt in user.alts.through.objects.filter(source=user)
]

class UserModSettingsSerializer(ModelSerializer):
"""A class to serialize the moderator settings for a user."""

class Meta:
"""Meta settings for the user moderator settings serializer."""

model = UserModSettings
fields = ('pings_disabled_until', 'pings_schedule_start', 'pings_schedule_end')

class NominationEntrySerializer(ModelSerializer):
"""A class providing (de-)serialization of `NominationEntry` instances."""
Expand Down

0 comments on commit 7f9844a

Please sign in to comment.