Skip to content

Commit

Permalink
Update serializers to validate mod settings correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
jb3 committed May 26, 2024
1 parent 6eec364 commit 369bdb6
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions pydis_site/apps/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,8 @@ def to_representation(self, instance: User) -> dict:
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']
if 'mod_settings' in ret:
del ret['mod_settings']

return ret

Expand Down Expand Up @@ -752,11 +753,21 @@ def get_alts(self, user: User) -> list[dict]:
class UserModSettingsSerializer(ModelSerializer):
"""A class to serialize the moderator settings for a user."""

def validate(self, data: dict) -> dict:
"""Validate the moderator settings contain the necessary fields."""
if data.get("pings_schedule_start") and not data.get("pings_schedule_end"):
raise ValidationError("missing pings_schedule_end")

if data.get("pings_schedule_end") and not data.get("pings_schedule_start"):
raise ValidationError("missing pings_schedule_start")

return data

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

model = UserModSettings
fields = ('pings_disabled_until', 'pings_schedule_start', 'pings_schedule_end')
fields = ('moderator', '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 369bdb6

Please sign in to comment.