Remove asset restrictions and make asset groups smarter

This commit is contained in:
2023-02-13 07:20:40 +00:00
parent 287facbab2
commit dcfb963be6
10 changed files with 383 additions and 172 deletions

View File

@@ -4,10 +4,9 @@ from django.core.exceptions import FieldDoesNotExist
from django.forms import ModelForm
from mixins.restrictions import RestrictedFormMixin
from .models import (
from .models import ( # AssetRestriction,
Account,
AssetGroup,
AssetRestriction,
Hook,
NotificationSettings,
RiskModel,
@@ -309,47 +308,85 @@ class AssetGroupForm(RestrictedFormMixin, ModelForm):
fields = (
"name",
"description",
"aggregation",
"trigger_below",
"trigger_above",
)
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.",
}
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}")
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
cleaned_data["pairs_parsed"] = new_pairs
else:
cleaned_data["pairs_parsed"] = {}
return cleaned_data
# 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