kopia lustrzana https://github.com/wagtail/wagtail
Add ExcludeFieldsOnEditMixin for edit handlers, disabling specific fields when bound to an existing instance rather than creating a new one
rodzic
c10255b5ac
commit
62961b74bb
wagtail/admin
|
@ -789,19 +789,38 @@ Page.base_form_class = WagtailAdminPageForm
|
|||
#Similarly, set up wagtailcore.Workflow to have edit handlers
|
||||
Workflow.panels = [
|
||||
FieldPanel("name"),
|
||||
FieldPanel("active"),
|
||||
InlinePanel("workflow_tasks", heading="Tasks"),
|
||||
]
|
||||
Task.panels = [
|
||||
FieldPanel("name"),
|
||||
FieldPanel("active"),
|
||||
]
|
||||
GroupApprovalTask.panels = Task.panels + [FieldPanel('group')]
|
||||
GroupApprovalTask.exclude_on_edit = {'group'}
|
||||
|
||||
Workflow.base_form_class = WagtailAdminModelForm
|
||||
Task.base_form_class = WagtailAdminModelForm
|
||||
|
||||
|
||||
class ExcludeFieldsOnEditMixin:
|
||||
"""A mixin for edit handlers, which disables fields listed in a model's 'exclude_on_edit' attribute when binding
|
||||
to an existing instance - editing rather than creating"""
|
||||
|
||||
def bind_to(self, *args, **kwargs):
|
||||
new = super(ExcludeFieldsOnEditMixin, self).bind_to(*args, **kwargs)
|
||||
# when binding to an existing instance - ie editing - set those fields in the form to disabled
|
||||
if new.form and new.instance and hasattr(new.model, 'exclude_on_edit'):
|
||||
for field in new.model.exclude_on_edit:
|
||||
try:
|
||||
new.form.fields[field].disabled = True
|
||||
except KeyError:
|
||||
continue
|
||||
return new
|
||||
|
||||
|
||||
class VaryOnEditObjectList(ExcludeFieldsOnEditMixin, ObjectList):
|
||||
pass
|
||||
|
||||
|
||||
@cached_classmethod
|
||||
def get_simple_edit_handler(cls):
|
||||
"""
|
||||
|
@ -810,7 +829,7 @@ def get_simple_edit_handler(cls):
|
|||
if hasattr(cls, 'edit_handler'):
|
||||
edit_handler = cls.edit_handler
|
||||
else:
|
||||
edit_handler = ObjectList(cls.panels, base_form_class=cls.base_form_class)
|
||||
edit_handler = VaryOnEditObjectList(cls.panels, base_form_class=cls.base_form_class)
|
||||
return edit_handler.bind_to(model=cls)
|
||||
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue