mirror of
https://github.com/pretix/pretix.git
synced 2026-08-21 12:36:26 +00:00
API: fix tax rules create default handling
This commit is contained in:
@@ -702,8 +702,12 @@ class TaxRuleSerializer(CountryFieldMixin, I18nAwareModelSerializer):
|
||||
return super().save(**kwargs)
|
||||
|
||||
def validate_default(self, value):
|
||||
if not value and self.instance.default:
|
||||
raise ValidationError("You can't remove the default property, instead set it on another tax rule.")
|
||||
if not value:
|
||||
if self.instance:
|
||||
if self.instance.default:
|
||||
raise ValidationError("You can't remove the default property, instead set it on another tax rule.")
|
||||
elif not self.context["event"].tax_rules.exists():
|
||||
raise ValidationError("You can't remove the default property as there is only one tax rule.")
|
||||
return value
|
||||
|
||||
|
||||
|
||||
@@ -61,17 +61,21 @@ def test_rule_detail(token_client, organizer, event, taxrule):
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_rule_create(token_client, organizer, event):
|
||||
resp = token_client.post(
|
||||
'/api/v1/organizers/{}/events/{}/taxrules/'.format(organizer.slug, event.slug),
|
||||
{
|
||||
"name": {"en": "VAT", "de": "MwSt"},
|
||||
"rate": "19.00",
|
||||
"price_includes_tax": True,
|
||||
"eu_reverse_charge": False,
|
||||
"home_country": "DE"
|
||||
},
|
||||
format='json'
|
||||
)
|
||||
url = '/api/v1/organizers/{}/events/{}/taxrules/'.format(organizer.slug, event.slug)
|
||||
payload = {
|
||||
"name": {"en": "VAT", "de": "MwSt"},
|
||||
"rate": "19.00",
|
||||
"price_includes_tax": True,
|
||||
"eu_reverse_charge": False,
|
||||
"home_country": "DE",
|
||||
"default": False,
|
||||
}
|
||||
# fail as only one rule exists and must be default
|
||||
resp = token_client.post(url, payload, format='json')
|
||||
assert resp.status_code == 400
|
||||
|
||||
del payload["default"]
|
||||
resp = token_client.post(url, payload, format='json')
|
||||
assert resp.status_code == 201
|
||||
rule = TaxRule.objects.get(pk=resp.data['id'])
|
||||
assert rule.name.data == {"en": "VAT", "de": "MwSt"}
|
||||
@@ -79,6 +83,7 @@ def test_rule_create(token_client, organizer, event):
|
||||
assert rule.price_includes_tax is True
|
||||
assert rule.eu_reverse_charge is False
|
||||
assert str(rule.home_country) == "DE"
|
||||
assert rule.default == True
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
|
||||
Reference in New Issue
Block a user