Skip to content

Commit

Permalink
Ensure all generic create / edit view forms receive for_user
Browse files Browse the repository at this point in the history
Co-authored-by: Sage Abdullah <sage.abdullah@torchbox.com>
  • Loading branch information
2 people authored and gasman committed May 1, 2024
1 parent 006bd91 commit e1728f6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
26 changes: 26 additions & 0 deletions wagtail/admin/views/generic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from wagtail.actions.unpublish import UnpublishAction
from wagtail.admin import messages
from wagtail.admin.filters import WagtailFilterSet
from wagtail.admin.forms.models import WagtailAdminModelForm
from wagtail.admin.forms.search import SearchForm
from wagtail.admin.panels import get_edit_handler
from wagtail.admin.ui.components import Component, MediaContainer
Expand Down Expand Up @@ -616,6 +617,24 @@ def get_translations(self):
for locale in Locale.objects.all().exclude(id=self.locale.id)
]

def get_initial_form_instance(self):
if self.locale:
instance = self.model()
instance.locale = self.locale
return instance

def get_form_kwargs(self):
if instance := self.get_initial_form_instance():
# super().get_form_kwargs() will use self.object as the instance kwarg
self.object = instance
kwargs = super().get_form_kwargs()

form_class = self.get_form_class()
# Add for_user support for PermissionedForm
if issubclass(form_class, WagtailAdminModelForm):
kwargs["for_user"] = self.request.user
return kwargs

def save_instance(self):
"""
Called after the form is successfully validated - saves the object to the db
Expand Down Expand Up @@ -786,6 +805,13 @@ def get_translations(self):
for translation in self.object.get_translations().select_related("locale")
]

def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
form_class = self.get_form_class()
if issubclass(form_class, WagtailAdminModelForm):
kwargs["for_user"] = self.request.user
return kwargs

def save_instance(self):
"""
Called after the form is successfully validated - saves the object to the db.
Expand Down
4 changes: 0 additions & 4 deletions wagtail/contrib/settings/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ def get_object(self, queryset=None):
def get_form_class(self):
return get_setting_edit_handler(self.model).get_form_class()

def get_form_kwargs(self):
# Pass the current user, which is needed for permission-restricted fields
return {**super().get_form_kwargs(), "for_user": self.request.user}

def get_edit_url(self):
return reverse(
"wagtailsettings:edit",
Expand Down
19 changes: 0 additions & 19 deletions wagtail/snippets/views/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,22 +235,6 @@ def run_after_hook(self):
def _get_action_menu(self):
return SnippetActionMenu(self.request, view=self.view_name, model=self.model)

def _get_initial_form_instance(self):
instance = self.model()

# Set locale of the new instance
if self.locale:
instance.locale = self.locale

return instance

def get_form_kwargs(self):
return {
**super().get_form_kwargs(),
"instance": self._get_initial_form_instance(),
"for_user": self.request.user,
}

def get_side_panels(self):
side_panels = [
SnippetStatusSidePanel(
Expand Down Expand Up @@ -310,9 +294,6 @@ def _get_action_menu(self):
locked_for_user=self.locked_for_user,
)

def get_form_kwargs(self):
return {**super().get_form_kwargs(), "for_user": self.request.user}

def get_side_panels(self):
side_panels = [
SnippetStatusSidePanel(
Expand Down

0 comments on commit e1728f6

Please sign in to comment.