diff --git a/src/pretix/api/serializers/item.py b/src/pretix/api/serializers/item.py index b29c0f34e2..56a923debc 100644 --- a/src/pretix/api/serializers/item.py +++ b/src/pretix/api/serializers/item.py @@ -87,6 +87,9 @@ class ItemSerializer(I18nAwareModelSerializer): def validate(self, data): data = super().validate(data) + if self.instance and ('addons' in data or 'variations' in data): + raise ValidationError(_('Updating add-ons or variations via PATCH/PUT is not supported. Please use the ' + 'dedicated nested endpoint.')) Item.clean_per_order(data.get('min_per_order'), data.get('max_per_order')) Item.clean_available(data.get('available_from'), data.get('available_until')) @@ -101,17 +104,8 @@ class ItemSerializer(I18nAwareModelSerializer): Item.clean_tax_rule(value, self.context['event']) return value - def validate_variations(self, value): - if self.instance is not None: - raise ValidationError(_('Updating variations via PATCH/PUT is not supported. Please use the dedicated' - ' nested endpoint.')) - return value - def validate_addons(self, value): - if self.instance is not None: - raise ValidationError(_('Updating add-ons via PATCH/PUT is not supported. Please use the dedicated' - ' nested endpoint.')) - else: + if not self.instance: for addon_data in value: ItemAddOn.clean_categories(self.context['event'], None, self.instance, addon_data['addon_category']) ItemAddOn.clean_min_count(addon_data['min_count']) diff --git a/src/tests/api/test_items.py b/src/tests/api/test_items.py index 5d245ad60f..d0dd5ac858 100644 --- a/src/tests/api/test_items.py +++ b/src/tests/api/test_items.py @@ -518,6 +518,17 @@ def test_item_create_with_addon(token_client, organizer, event, item, category, @pytest.mark.django_db def test_item_update(token_client, organizer, event, item, category, category2, taxrule2): + resp = token_client.patch( + '/api/v1/organizers/{}/events/{}/items/{}/'.format(organizer.slug, event.slug, item.pk), + { + "min_per_order": 1, + "max_per_order": 2 + }, + format='json' + ) + assert resp.status_code == 200 + assert Item.objects.get(pk=item.pk).max_per_order == 2 + resp = token_client.patch( '/api/v1/organizers/{}/events/{}/items/{}/'.format(organizer.slug, event.slug, item.pk), { @@ -578,7 +589,7 @@ def test_item_update(token_client, organizer, event, item, category, category2, format='json' ) assert resp.status_code == 400 - assert resp.content.decode() == '{"addons":["Updating add-ons via PATCH/PUT is not supported. Please use ' \ + assert resp.content.decode() == '{"non_field_errors":["Updating add-ons or variations via PATCH/PUT is not supported. Please use ' \ 'the dedicated nested endpoint."]}' @@ -604,7 +615,7 @@ def test_item_update_with_variation(token_client, organizer, event, item): format='json' ) assert resp.status_code == 400 - assert resp.content.decode() == '{"variations":["Updating variations via PATCH/PUT is not supported. Please use ' \ + assert resp.content.decode() == '{"non_field_errors":["Updating add-ons or variations via PATCH/PUT is not supported. Please use ' \ 'the dedicated nested endpoint."]}' @@ -626,7 +637,7 @@ def test_item_update_with_addon(token_client, organizer, event, item, category): format='json' ) assert resp.status_code == 400 - assert resp.content.decode() == '{"addons":["Updating add-ons via PATCH/PUT is not supported. Please use ' \ + assert resp.content.decode() == '{"non_field_errors":["Updating add-ons or variations via PATCH/PUT is not supported. Please use ' \ 'the dedicated nested endpoint."]}'