Skip to content
Snippets Groups Projects
Verified Commit c461eb29 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Move action form permission check to clean_ method

parent 087790b3
No related branches found
No related tags found
1 merge request!996Improve actionform
......@@ -722,6 +722,26 @@ class ActionForm(forms.Form):
self.fields["selected_objects"].queryset = self.queryset
self.fields["action"].choices = self._get_action_choices()
def clean_action(self):
action = self._get_actions_dict().get(self.cleaned_data["action"], None)
if not action:
raise ValidationError(_("The selected action does not exist."))
return action
def clean_selected_objects(self):
action = self.cleaned_data["action"]
if hasattr(action, "permission"):
selected_objects = queryset_rules_filter(
self.request, self.cleaned_data["selected_objects"], action.permission
)
if selected_objects.count() < self.cleaned_data["selected_objects"].count():
raise ValidationError(
_("You do not have permission to run {} on all selected objects.").format(
getattr(value, "short_description", value.__name__)
)
)
return self.cleaned_data["selected_objects"]
def execute(self) -> bool:
"""Execute the selected action on all selected objects.
......@@ -729,11 +749,7 @@ class ActionForm(forms.Form):
"""
if self.is_valid():
data = self.cleaned_data["selected_objects"]
action = self._get_actions_dict()[self.cleaned_data["action"]]
if hasattr(action, "permission"):
data = queryset_rules_filter(self.request, data, action.permission)
action = self.cleaned_data["action"]
action(None, self.request, data)
return True
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment