Ensure an account only has one strategy with active management

This commit is contained in:
Mark Veidemanis 2023-02-18 21:42:56 +00:00
parent ea0a6f21ce
commit db58fb34eb
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
1 changed files with 15 additions and 0 deletions

View File

@ -217,6 +217,21 @@ class StrategyForm(RestrictedFormMixin, ModelForm):
]
)
if cleaned_data.get("active_management_enabled"):
# Ensure that no other strategy with this account has active management enabled
if (
Strategy.objects.filter(
account=cleaned_data.get("account"),
active_management_enabled=True,
enabled=True,
)
.exclude(id=self.instance.id)
.exists()
):
self.add_error(
"active_management_enabled",
"You cannot have more than one strategy with active management enabled for the same account.",
)
return
if not cleaned_data.get("active_management_policy"):
self.add_error(
"active_management_policy",