mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-07-16 01:32:41 +02:00
feat: make TransactionRuleAction field and value unique together
This commit is contained in:
@@ -3,6 +3,7 @@ from crispy_forms.bootstrap import FormActions
|
|||||||
from crispy_forms.helper import FormHelper
|
from crispy_forms.helper import FormHelper
|
||||||
from crispy_forms.layout import Layout, Field, Row, Column
|
from crispy_forms.layout import Layout, Field, Row, Column
|
||||||
from django import forms
|
from django import forms
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from apps.rules.models import TransactionRule
|
from apps.rules.models import TransactionRule
|
||||||
@@ -66,6 +67,8 @@ class TransactionRuleActionForm(forms.ModelForm):
|
|||||||
widgets = {"field": TomSelect(clear_button=False)}
|
widgets = {"field": TomSelect(clear_button=False)}
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
self.rule = kwargs.pop("rule", None)
|
||||||
|
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
self.helper = FormHelper()
|
self.helper = FormHelper()
|
||||||
@@ -93,3 +96,22 @@ class TransactionRuleActionForm(forms.ModelForm):
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def clean(self):
|
||||||
|
cleaned_data = super().clean()
|
||||||
|
field = cleaned_data.get("field")
|
||||||
|
|
||||||
|
if field and self.rule:
|
||||||
|
if TransactionRuleAction.objects.filter(
|
||||||
|
rule=self.rule, field=field
|
||||||
|
).exists():
|
||||||
|
raise ValidationError(
|
||||||
|
_("A value for this field already exists in the rule.")
|
||||||
|
)
|
||||||
|
|
||||||
|
def save(self, commit=True):
|
||||||
|
instance = super().save(commit=False)
|
||||||
|
instance.rule = self.rule
|
||||||
|
if commit:
|
||||||
|
instance.save()
|
||||||
|
return instance
|
||||||
|
|||||||
@@ -42,3 +42,6 @@ class TransactionRuleAction(models.Model):
|
|||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.rule} - {self.field} - {self.value}"
|
return f"{self.rule} - {self.field} - {self.value}"
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
unique_together = (("rule", "field"),)
|
||||||
|
|||||||
@@ -142,11 +142,9 @@ def transaction_rule_action_add(request, transaction_rule_id):
|
|||||||
transaction_rule = get_object_or_404(TransactionRule, id=transaction_rule_id)
|
transaction_rule = get_object_or_404(TransactionRule, id=transaction_rule_id)
|
||||||
|
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
form = TransactionRuleActionForm(request.POST)
|
form = TransactionRuleActionForm(request.POST, rule=transaction_rule)
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
action = form.save(commit=False)
|
form.save()
|
||||||
action.rule = transaction_rule
|
|
||||||
action.save()
|
|
||||||
|
|
||||||
return HttpResponse(
|
return HttpResponse(
|
||||||
status=204,
|
status=204,
|
||||||
@@ -155,7 +153,7 @@ def transaction_rule_action_add(request, transaction_rule_id):
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
form = TransactionRuleActionForm()
|
form = TransactionRuleActionForm(rule=transaction_rule)
|
||||||
|
|
||||||
return render(
|
return render(
|
||||||
request,
|
request,
|
||||||
|
|||||||
Reference in New Issue
Block a user