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 d9ddde5
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 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 All @@ -717,6 +730,7 @@ def create(self, validated_data: dict) -> User:
raise ValidationError({"id": ["User with ID already present."]})


<<<<<<< HEAD

Check failure on line 733 in pydis_site/apps/api/serializers.py

View workflow job for this annotation

GitHub Actions / lint-test / lint-test

Ruff (E999)

pydis_site/apps/api/serializers.py:733:1: E999 SyntaxError: Expected a statement
class UserWithAltsSerializer(FrozenFieldsMixin, UserSerializer):
"""A class providing (de-)serialization of `User` instances, expanding their alternate accounts."""

Expand All @@ -736,6 +750,16 @@ 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')
>>>>>>> d672a0ab (Add serializer for mod_settings response key on User objects)

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

0 comments on commit d9ddde5

Please sign in to comment.