only overwrite final representation not the serializer

This commit is contained in:
Lukas Bockstaller
2026-04-15 12:16:18 +02:00
parent 958e318b4c
commit 23de795b3f
2 changed files with 7 additions and 21 deletions

View File

@@ -117,8 +117,8 @@ class ReusableMediaSerializer(I18nAwareModelSerializer):
return data return data
def to_representation(self, instance): def to_representation(self, instance):
r = super().to_representation(instance)
request = self.context.get('request') request = self.context.get('request')
# late permission evaluations for checks that depend on the actual linked events # late permission evaluations for checks that depend on the actual linked events
if 'linked_orderposition' in self.context['request'].query_params.getlist('expand'): if 'linked_orderposition' in self.context['request'].query_params.getlist('expand'):
if instance.linked_orderposition is not None: if instance.linked_orderposition is not None:
@@ -126,11 +126,7 @@ class ReusableMediaSerializer(I18nAwareModelSerializer):
if not ( if not (
request.user if request.user and request.user.is_authenticated else request.auth request.user if request.user and request.user.is_authenticated else request.auth
).has_event_permission(organizer=event.organizer, event=event, perm_name='event.orders:read', request=request): ).has_event_permission(organizer=event.organizer, event=event, perm_name='event.orders:read', request=request):
pos_serializer = self.fields['linked_orderposition'] r['linked_orderposition'] = {'id': instance.linked_orderposition.id}
allowed = {'id'}
for field_name in list(pos_serializer.fields.keys()):
if field_name not in allowed:
pos_serializer.fields.pop(field_name)
if 'linked_giftcard.owner_ticket' in self.context['request'].query_params.getlist('expand'): if 'linked_giftcard.owner_ticket' in self.context['request'].query_params.getlist('expand'):
gc = instance.linked_giftcard gc = instance.linked_giftcard
@@ -139,14 +135,9 @@ class ReusableMediaSerializer(I18nAwareModelSerializer):
if not ( if not (
request.user if request.user and request.user.is_authenticated else request.auth request.user if request.user and request.user.is_authenticated else request.auth
).has_event_permission(organizer=event.organizer, event=event, perm_name='event.orders:read', request=request): ).has_event_permission(organizer=event.organizer, event=event, perm_name='event.orders:read', request=request):
ticket_serializer = self.fields['linked_giftcard'].fields['owner_ticket'] r['linked_giftcard']['owner_ticket'] = {'id': instance.linked_giftcard.owner_ticket.id}
allowed = {'id'}
for field_name in list(ticket_serializer.fields.keys()):
if field_name not in allowed:
ticket_serializer.fields.pop(field_name)
return r
return super().to_representation(instance)

View File

@@ -287,8 +287,8 @@ class GiftCardSerializer(I18nAwareModelSerializer):
return data return data
def to_representation(self, instance): def to_representation(self, instance):
r = super().to_representation(instance)
request = self.context.get('request') request = self.context.get('request')
# late permission evaluations for checks that depend on the actual linked events # late permission evaluations for checks that depend on the actual linked events
if 'owner_ticket' in self.context['request'].query_params.getlist('expand'): if 'owner_ticket' in self.context['request'].query_params.getlist('expand'):
owner_ticket = instance.owner_ticket owner_ticket = instance.owner_ticket
@@ -297,13 +297,8 @@ class GiftCardSerializer(I18nAwareModelSerializer):
if not ( if not (
request.user if request.user and request.user.is_authenticated else request.auth request.user if request.user and request.user.is_authenticated else request.auth
).has_event_permission(organizer=event.organizer, event=event, perm_name='event.orders:read', request=request): ).has_event_permission(organizer=event.organizer, event=event, perm_name='event.orders:read', request=request):
ticket_serializer = self.fields['owner_ticket'].read r['owner_ticket'] = {'id': instance.owner_ticket.id}
allowed = {'id'} return r
for field_name in list(ticket_serializer.fields.keys()):
if field_name not in allowed:
ticket_serializer.fields.pop(field_name)
return super().to_representation(instance)
class Meta: class Meta:
model = GiftCard model = GiftCard