aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/api/serializers.py
diff options
context:
space:
mode:
authorGravatar jchristgit <[email protected]>2024-05-23 22:49:34 +0200
committerGravatar GitHub <[email protected]>2024-05-23 21:49:34 +0100
commit089b7f4504c8d1316c5ad97f019735a090848582 (patch)
tree0364567ae31d79b6fd866a6f82cd89bfa4d2178e /pydis_site/apps/api/serializers.py
parentMerge pull request #1325 from python-discord/dependabot/pip/ruff-0.4.5 (diff)
Add alternate accounts to the user model
Introduce a way to store alternate accounts on the user, and add the `PATCH /bot/users/<id:str>/alts` endpoint, which allows updating the user's alt accounts to the alt accounts in the request..
Diffstat (limited to 'pydis_site/apps/api/serializers.py')
-rw-r--r--pydis_site/apps/api/serializers.py56
1 files changed, 54 insertions, 2 deletions
diff --git a/pydis_site/apps/api/serializers.py b/pydis_site/apps/api/serializers.py
index 60d3637c..2b4bf395 100644
--- a/pydis_site/apps/api/serializers.py
+++ b/pydis_site/apps/api/serializers.py
@@ -11,6 +11,7 @@ from rest_framework.serializers import (
ListSerializer,
ModelSerializer,
PrimaryKeyRelatedField,
+ SerializerMethodField,
ValidationError
)
from rest_framework.settings import api_settings
@@ -35,7 +36,8 @@ from .models import (
OffensiveMessage,
Reminder,
Role,
- User
+ User,
+ UserAltRelationship
)
class FrozenFieldsMixin:
@@ -507,7 +509,7 @@ class ExpandedInfractionSerializer(InfractionSerializer):
"""Return the dictionary representation of this infraction."""
ret = super().to_representation(instance)
- ret['user'] = UserSerializer(instance.user).data
+ ret['user'] = UserWithAltsSerializer(instance.user).data
ret['actor'] = UserSerializer(instance.actor).data
return ret
@@ -663,6 +665,36 @@ class UserListSerializer(ListSerializer):
return updated
+class UserAltRelationshipSerializer(FrozenFieldsMixin, ModelSerializer):
+ """A class providing (de-)serialization of `UserAltRelationship` instances."""
+
+ actor = PrimaryKeyRelatedField(queryset=User.objects.all())
+ source = PrimaryKeyRelatedField(queryset=User.objects.all())
+ target = PrimaryKeyRelatedField(queryset=User.objects.all())
+
+ class Meta:
+ """Metadata defined for the Django REST Framework."""
+
+ model = UserAltRelationship
+ fields = ('source', 'target', 'actor', 'context', 'created_at', 'updated_at')
+ frozen_fields = ('source', 'target', 'actor')
+ depth = 1
+
+ def to_representation(self, instance: UserAltRelationship) -> dict:
+ """Add the alts of the target to the representation."""
+ representation = super().to_representation(instance)
+ representation['alts'] = tuple(
+ user_id
+ for (user_id,) in (
+ UserAltRelationship.objects
+ .filter(source=instance.target)
+ .values_list('target_id')
+ )
+ )
+ return representation
+
+
+
class UserSerializer(ModelSerializer):
"""A class providing (de-)serialization of `User` instances."""
@@ -685,6 +717,26 @@ class UserSerializer(ModelSerializer):
raise ValidationError({"id": ["User with ID already present."]})
+class UserWithAltsSerializer(FrozenFieldsMixin, UserSerializer):
+ """A class providing (de-)serialization of `User` instances, expanding their alternate accounts."""
+
+ alts = SerializerMethodField()
+
+ class Meta:
+ """Metadata defined for the Django REST Framework."""
+
+ model = User
+ fields = ('id', 'name', 'display_name', 'discriminator', 'roles', 'in_guild', 'alts')
+ frozen_fields = ('alts',)
+
+ def get_alts(self, user: User) -> list[dict]:
+ """Retrieve the alts with all additional data on them."""
+ return [
+ UserAltRelationshipSerializer(alt).data
+ for alt in user.alts.through.objects.filter(source=user)
+ ]
+
+
class NominationEntrySerializer(ModelSerializer):
"""A class providing (de-)serialization of `NominationEntry` instances."""