Implement asset rules as Asset Group children objects

This commit is contained in:
2023-02-13 20:45:23 +00:00
parent b48af50620
commit 6ff5f718ba
18 changed files with 565 additions and 349 deletions

View File

@@ -7,6 +7,7 @@ from mixins.restrictions import RestrictedFormMixin
from .models import ( # AssetRestriction,
Account,
AssetGroup,
AssetRule,
Hook,
NotificationSettings,
RiskModel,
@@ -308,85 +309,38 @@ class AssetGroupForm(RestrictedFormMixin, ModelForm):
fields = (
"name",
"description",
"aggregation",
"trigger_below",
"trigger_above",
"when_no_data",
"when_no_match",
"when_no_aggregation",
"when_not_in_bounds",
"when_bullish",
"when_bearish",
)
help_texts = {
"name": "Name of the asset group. Informational only.",
"description": "Description of the asset group. Informational only.",
"aggregation": "The aggregation method to use for this asset group.",
"trigger_below": "Trigger when the aggregation is below this value.",
"trigger_above": "Trigger when the aggregation is above this value.",
"when_no_data": "The action to take when no webhooks have been received for an asset.",
"when_no_match": "The action to take when there were no matches last callback for an asset.",
"when_no_aggregation": "The action to take when there is no defined aggregations for the asset.",
"when_not_in_bounds": "The action to take when the aggregation is not breaching either bound.",
"when_bullish": "The action to take when the asset is bullish.",
"when_bearish": "The action to take when the asset is bearish.",
}
def clean(self):
cleaned_data = super(AssetGroupForm, self).clean()
if "aggregation" in cleaned_data:
if cleaned_data["aggregation"] == "none":
if "trigger_below" in cleaned_data and cleaned_data["trigger_below"]:
self.add_error(
"trigger_below",
"You cannot specify a trigger below value when aggregation is set to none.",
)
return
if "trigger_above" in cleaned_data and cleaned_data["trigger_above"]:
self.add_error(
"trigger_above",
"You cannot specify a trigger above value when aggregation is set to none.",
)
return
else:
# Check if either trigger_below or trigger_above has been set
if not any(
[cleaned_data["trigger_below"], cleaned_data["trigger_above"]]
):
self.add_error(
"trigger_below",
"You must specify a trigger below and/or trigger above value when aggregation is set to anything other than none.",
)
self.add_error(
"trigger_above",
"You must specify a trigger below and/or trigger above value when aggregation is set to anything other than none.",
)
return
return cleaned_data
class AssetRuleForm(RestrictedFormMixin, ModelForm):
def __init__(self, *args, **kwargs):
super(AssetRuleForm, self).__init__(*args, **kwargs)
self.fields["value"].disabled = True
self.fields["aggregation"].disabled = True
# class AssetRestrictionForm(RestrictedFormMixin, ModelForm):
# class Meta:
# model = AssetRestriction
# fields = (
# "name",
# "description",
# "pairs",
# "pairs_parsed",
# )
# help_texts = {
# "name": "Name of the asset restriction group. Informational only.",
# "description": "Description of the asset restriction group. Informational only.",
# "pairs": "Comma-separated list of pairs to restrict when a webhook is received. This does nothing on its own.",
# }
# pairs_parsed = forms.BooleanField(widget=forms.HiddenInput, required=False)
# def clean(self):
# cleaned_data = super(AssetRestrictionForm, self).clean()
# if "pairs" in cleaned_data and cleaned_data["pairs"]:
# new_pairs = []
# pair_split = cleaned_data["pairs"].split(",")
# if not pair_split:
# self.add_error("pairs", "You must specify at least one pair.")
# return
# for pair in pair_split:
# if pair:
# new_pairs.append(pair.strip())
# else:
# self.add_error("pairs", f"You cannot have an empty pair: {pair}")
# return
# cleaned_data["pairs_parsed"] = new_pairs
# else:
# cleaned_data["pairs_parsed"] = {}
# return cleaned_data
class Meta:
model = AssetRule
fields = (
"asset",
"aggregation",
"value",
"status",
"trigger_below",
"trigger_above",
)