mirror of
https://github.com/pretix/pretix.git
synced 2026-05-08 15:44:02 +00:00
add validation to ItemCategorySerializer
This commit is contained in:
@@ -447,6 +447,18 @@ class ItemCategorySerializer(I18nAwareModelSerializer):
|
||||
'cross_selling_condition', 'cross_selling_match_products'
|
||||
)
|
||||
|
||||
def validate(self, data):
|
||||
data = super().validate(data)
|
||||
|
||||
full_data = self.to_internal_value(self.to_representation(self.instance)) if self.instance else {}
|
||||
full_data.update(data)
|
||||
|
||||
if full_data.get('is_addon') and full_data.get('cross_selling_mode'):
|
||||
raise ValidationError('is_addon and cross_selling_mode are mutually exclusive')
|
||||
|
||||
return data
|
||||
|
||||
|
||||
|
||||
class QuestionOptionSerializer(I18nAwareModelSerializer):
|
||||
identifier = serializers.CharField(allow_null=True)
|
||||
|
||||
@@ -214,6 +214,44 @@ def test_category_update(token_client, organizer, event, team, category):
|
||||
assert ItemCategory.objects.get(pk=category.pk).name == {"en": "Test"}
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_category_update_cross_selling_options(token_client, organizer, event, team, category):
|
||||
resp = token_client.patch(
|
||||
'/api/v1/organizers/{}/events/{}/categories/{}/'.format(organizer.slug, event.slug, category.pk),
|
||||
{
|
||||
"cross_selling_mode": "both",
|
||||
},
|
||||
format='json'
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
with scopes_disabled():
|
||||
assert ItemCategory.objects.get(pk=category.pk).cross_selling_mode == 'both'
|
||||
|
||||
resp = token_client.patch(
|
||||
'/api/v1/organizers/{}/events/{}/categories/{}/'.format(organizer.slug, event.slug, category.pk),
|
||||
{
|
||||
"cross_selling_mode": "something",
|
||||
},
|
||||
format='json'
|
||||
)
|
||||
assert resp.status_code == 400
|
||||
with scopes_disabled():
|
||||
assert ItemCategory.objects.get(pk=category.pk).cross_selling_mode == 'both'
|
||||
|
||||
resp = token_client.patch(
|
||||
'/api/v1/organizers/{}/events/{}/categories/{}/'.format(organizer.slug, event.slug, category.pk),
|
||||
{
|
||||
"is_addon": True,
|
||||
},
|
||||
format='json'
|
||||
)
|
||||
assert resp.status_code == 400
|
||||
assert 'mutually exclusive' in str(resp.data)
|
||||
with scopes_disabled():
|
||||
assert ItemCategory.objects.get(pk=category.pk).cross_selling_mode == 'both'
|
||||
assert ItemCategory.objects.get(pk=category.pk).is_addon is False
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_category_update_wrong_event(token_client, organizer, event2, category):
|
||||
resp = token_client.patch(
|
||||
|
||||
Reference in New Issue
Block a user