Skip to content

Commit

Permalink
Use checkboxes for the target types in forms
Browse files Browse the repository at this point in the history
  • Loading branch information
np5 committed Sep 17, 2024
1 parent 9c18525 commit 8ab7b80
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tests/santa/test_setup_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def test_post_update_configuration_view(self, post_event):
"remount_usb_mode": "rdonly, noexec",
"voting_realm": realm.pk,
"banned_threshold": -50,
"default_ballot_target_types": "METABUNDLE,SIGNINGID",
"default_ballot_target_types": ["METABUNDLE", "SIGNINGID"],
"default_voting_weight": 1,
"globally_allowlisted_threshold": 500,
"partially_allowlisted_threshold": 100,
Expand Down
14 changes: 13 additions & 1 deletion zentral/contrib/santa/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,22 @@
logger = logging.getLogger("zentral.contrib.santa.forms")


class TargetTypesWidget(forms.CheckboxSelectMultiple):
def __init__(self, attrs=None, choices=()):
super().__init__(attrs, choices=Target.Type.choices)

def format_value(self, value):
if isinstance(value, str) and value:
value = [v.strip() for v in value.split(",")]
return super().format_value(value)


class ConfigurationForm(forms.ModelForm):
class Meta:
model = Configuration
fields = '__all__'
widgets = {
"default_ballot_target_types": TargetTypesWidget,
"event_detail_url": forms.Textarea(attrs={"cols": "40", "rows": "3"}),
"allowed_path_regex": forms.Textarea(attrs={"cols": "40", "rows": "3"}),
"blocked_path_regex": forms.Textarea(attrs={"cols": "40", "rows": "3"})
Expand Down Expand Up @@ -73,6 +84,7 @@ class Meta:
"ballot_target_types",
"voting_weight"
)
widgets = {"ballot_target_types": TargetTypesWidget}

def __init__(self, *args, **kwargs):
self.configuration = kwargs.pop("configuration")
Expand Down Expand Up @@ -1023,7 +1035,7 @@ def results(self, current_username, current_email, offset, limit):

for key, display_str in Target.objects.get_targets_display_strings(targets.keys()).items():
for idx in targets[key]:
results[idx][f"target_display_str"] = display_str
results[idx]["target_display_str"] = display_str

return results

Expand Down

0 comments on commit 8ab7b80

Please sign in to comment.