Skip to content

Commit

Permalink
Fix serialization typing for CollectionAbuseReportSerializer collecti…
Browse files Browse the repository at this point in the history
…on field
  • Loading branch information
KevinMind committed Sep 20, 2024
1 parent aad06d1 commit ad351f0
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/olympia/abuse/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import TypedDict

from django.http import Http404
from django.utils.translation import gettext_lazy as _

Expand All @@ -11,6 +12,7 @@
from olympia.api.exceptions import UnavailableForLegalReasons
from olympia.api.fields import ReverseChoiceField
from olympia.api.serializers import AMOModelSerializer
from olympia.bandwagon.serializers import CollectionSerializer
from olympia.constants.abuse import (
ILLEGAL_CATEGORIES,
ILLEGAL_SUBCATEGORIES,
Expand Down Expand Up @@ -357,21 +359,22 @@ def get_rating(self, obj):


class CollectionAbuseReportSerializer(BaseAbuseReportSerializer):
collection = serializers.SerializerMethodField()
class CollectionIdSerializer(CollectionSerializer):
class Meta(CollectionSerializer.Meta):
fields = ('id',)

class Meta:
model = AbuseReport
collection = CollectionIdSerializer(read_only=True)

class Meta(BaseAbuseReportSerializer.Meta):
fields = BaseAbuseReportSerializer.Meta.fields + ('collection',)

def to_internal_value(self, data):
self.validate_target(data, 'collection')
view = self.context.get('view')
output = {'collection': view.get_target_object()}
collection = view.get_target_object()
# Pop 'collection' before passing it to super(), we already have the
# output value and did the validation above.
data.pop('collection')
output.update(super().to_internal_value(data))
return output

def get_collection(self, obj):
return {'id': obj.collection.pk}
data.pop('collection', None)
validated_data = super().to_internal_value(data)
validated_data['collection'] = collection
return validated_data

0 comments on commit ad351f0

Please sign in to comment.