0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Raise a custom exception when attempting to edit a page who's model class cannot be found in the codebase

This commit is contained in:
Andy Babic 2020-07-07 16:30:53 +01:00 committed by Matt Westcott
parent dd680ae4d4
commit 10312cb4da
2 changed files with 23 additions and 3 deletions

View File

@ -28,6 +28,7 @@ from wagtail.admin.forms.search import SearchForm
from wagtail.admin.mail import send_notification
from wagtail.admin.navigation import get_explorable_root_page
from wagtail.core import hooks
from wagtail.core.exceptions import PageClassNotFoundError
from wagtail.core.models import Page, PageRevision, UserPagePermissionsProxy
from wagtail.search.query import MATCH_ALL
from wagtail.search.utils import parse_query_string
@ -343,12 +344,23 @@ def create(request, content_type_app_name, content_type_model_name, parent_page_
def edit(request, page_id):
real_page_record = get_object_or_404(Page, id=page_id)
latest_revision = real_page_record.get_latest_revision()
content_type = real_page_record.cached_content_type
page_class = real_page_record.specific_class
if page_class is None:
raise PageClassNotFoundError(
f"The page '{real_page_record}' cannot be edited because the "
f"model class used to create it ({content_type.app_label}."
f"{content_type.model}) can no longer be found in the codebase. "
"This usually happens as a result of switching between git "
"branches without running migrations to trigger the removal of "
"unused ContentTypes. To edit the page, you will need to switch "
"back to a branch where the model class is still present."
)
page = real_page_record.get_latest_revision_as_page()
parent = page.get_parent()
content_type = ContentType.objects.get_for_model(page)
page_class = content_type.model_class()
page_perms = page.permissions_for_user(request.user)
if not page_perms.can_edit():
raise PermissionDenied

View File

@ -0,0 +1,8 @@
class PageClassNotFoundError(ImportError):
"""
Raised when a model class referenced by a page object's ``content_type``
value cannot be found in the codebase. Usually, this is as a result of
switching to a different git branch without first running/reverting
migrations.
"""
pass