Skip to content

Commit

Permalink
return partial objects (organization members done)
Browse files Browse the repository at this point in the history
  • Loading branch information
wither-rose authored Dec 3, 2023
1 parent dd3f154 commit 1ef6664
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions core/api/serializers/custom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Meta:

@staticmethod
def get_gravatar_url(obj: User):
return gravatar_url(str(obj))
return gravatar_url(obj.email)


class OrganizationSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -153,22 +153,25 @@ def __init__(self, **kwargs):
super().__init__(**kwargs)
self.default_error_messages.update(default_error_messages)


class MembersField(serializers.Field):
def to_representation(self, value):
return MembersSerializer(value).data if value else None
return MembersSerializer(value, many=True).data

def to_internal_value(self, data):
if data is None:
return None
if isinstance(data, str):
json_string_data = data.replace("'", '"')
data = json.loads(json_string_data)
if isinstance(data, dict):
data = data.get("id")
try:
return User.objects.get(pk=data)
except User.DoesNotExist:
self.fail("does_not_exist", value=data)
if not isinstance(data, list):
raise serializers.ValidationError("Expected a list of user IDs.")

users = User.objects.filter(id__in=data).values_list("id", flat=True)
if len(users) != len(data):
missing_ids = set(data) - set(users)
for missing_id in missing_ids:
self.fail(
"invalid", message=f"User with ID {missing_id} does not exist."
)
return User.objects.filter(id__in=data)

@staticmethod
def get_queryset():
Expand All @@ -178,7 +181,7 @@ def __init__(self, **kwargs):
default_error_messages = {
"does_not_exist": "User with ID {value} does not exist.",
}
kwargs["help_text"] = "The User ID of the author of this object."
kwargs["help_text"] = "The User ID of the member of this object."
super().__init__(**kwargs)
self.default_error_messages.update(default_error_messages)

Expand Down Expand Up @@ -259,4 +262,4 @@ def __init__(self, **kwargs):
super().__init__(**kwargs)

def to_representation(self, obj):
return CommentSerializer(obj, many=True).data
return CommentSerializer(obj, many=True).data

0 comments on commit 1ef6664

Please sign in to comment.