Настройка разрешений между режимами редактирования и просмотра только в wagtail
В моем проекте Wagtail у меня есть класс, который наследуется от EditView(ModelAdmin). В рамках этого класса я переопределяю метод get_edit_handler, чтобы динамически устанавливать поля как доступные только для чтения на основе пользовательских разрешений. Однако я столкнулся с проблемой с полями RichTextField: настройки разрешений вступают в силу только после перезапуска приложения. Например, если я настрою для встроенной панели значение "только для чтения", все экземпляры InlinePanel, которые включают поля RichTextField в модели, также станут доступны только для чтения, независимо от их индивидуальных настроек разрешений.
class JournalEditView(EditView):
def form_valid(self, form):
self.object = form.save_all(self.request.user)
return HttpResponseRedirect(self.get_success_url())
def get_edit_handler(self):
"""
Overrides the get_edit_handler method from EditView.
It checks whether the user has permission to edit each field.
If not, the fields for which the user lacks the journal.can_edit_{field_name}
permission in their user_permissions will be set as read-only.
This applies to FieldPanel, AutocompletePanel, and InlinePanel.
"""
edit_handler = super().get_edit_handler()
user_permissions = self.request.user.get_all_permissions()
if not self.request.user.is_superuser:
for object_list in edit_handler.children:
for field in object_list.children:
if isinstance(field, FieldPanel) and f"journal.can_edit_{field.field_name}" not in user_permissions:
field.read_only = True
elif isinstance(field, InlinePanel) and f"journal.can_edit_{field.relation_name}" not in user_permissions:
field.classname += ' read-only-inline-panel'
for inline_field in field.panel_definitions:
inline_field.read_only = True
return edit_handler
class JournalAdmin(ModelAdmin):
model = models.Journal
inspect_view_enabled = True
menu_label = _("Journals")
create_view_class = JournalCreateView
edit_view_class = JournalEditView