mirror of
https://github.com/wagtail/wagtail.git
synced 2024-12-01 11:41:20 +01:00
Add legacy URL patterns for ModelViewSet's edit and delete URLs
In case people hardcode the URLs instead of using reverse(). Unify the legacy redirects with SnippetViewSet to use the same _legacy_urlpatterns property.
This commit is contained in:
parent
f77125155f
commit
f6c2f0bd95
@ -718,3 +718,28 @@ class TestBreadcrumbs(WagtailTestUtils, TestCase):
|
||||
breadcrumbs = soup.select_one('[data-controller="w-breadcrumbs"]')
|
||||
# Delete view shouldn't render breadcrumbs
|
||||
self.assertIsNone(breadcrumbs)
|
||||
|
||||
|
||||
class TestLegacyPatterns(WagtailTestUtils, TestCase):
|
||||
def setUp(self):
|
||||
self.user = self.login()
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
cls.object = JSONStreamModel.objects.create(
|
||||
body='[{"type": "text", "value": "foo"}]',
|
||||
)
|
||||
|
||||
def test_legacy_edit(self):
|
||||
edit_url = reverse("streammodel:edit", args=(quote(self.object.pk),))
|
||||
legacy_edit_url = "/admin/streammodel/1/"
|
||||
response = self.client.get(legacy_edit_url)
|
||||
self.assertEqual(edit_url, "/admin/streammodel/edit/1/")
|
||||
self.assertRedirects(response, edit_url, 301)
|
||||
|
||||
def test_legacy_delete(self):
|
||||
delete_url = reverse("streammodel:delete", args=(quote(self.object.pk),))
|
||||
legacy_delete_url = "/admin/streammodel/1/delete/"
|
||||
response = self.client.get(legacy_delete_url)
|
||||
self.assertEqual(delete_url, "/admin/streammodel/delete/1/")
|
||||
self.assertRedirects(response, delete_url, 301)
|
||||
|
@ -1,5 +1,6 @@
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.forms.models import modelform_factory
|
||||
from django.shortcuts import redirect
|
||||
from django.urls import path
|
||||
from django.utils.functional import cached_property
|
||||
|
||||
@ -151,6 +152,20 @@ class ModelViewSet(ViewSet):
|
||||
self.delete_view_class, **self.get_delete_view_kwargs()
|
||||
)
|
||||
|
||||
@property
|
||||
def redirect_to_edit_view(self):
|
||||
def redirect_to_edit(request, pk):
|
||||
return redirect(self.get_url_name("edit"), pk, permanent=True)
|
||||
|
||||
return redirect_to_edit
|
||||
|
||||
@property
|
||||
def redirect_to_delete_view(self):
|
||||
def redirect_to_delete(request, pk):
|
||||
return redirect(self.get_url_name("delete"), pk, permanent=True)
|
||||
|
||||
return redirect_to_delete
|
||||
|
||||
def get_templates(self, name="index", fallback=""):
|
||||
"""
|
||||
Utility function that provides a list of templates to try for a given
|
||||
@ -415,6 +430,13 @@ class ModelViewSet(ViewSet):
|
||||
path("new/", self.add_view, name="add"),
|
||||
path("edit/<str:pk>/", self.edit_view, name="edit"),
|
||||
path("delete/<str:pk>/", self.delete_view, name="delete"),
|
||||
] + self._legacy_urlpatterns
|
||||
|
||||
@cached_property
|
||||
def _legacy_urlpatterns(self):
|
||||
return [
|
||||
path("<int:pk>/", self.redirect_to_edit_view),
|
||||
path("<int:pk>/delete/", self.redirect_to_delete_view),
|
||||
]
|
||||
|
||||
def on_register(self):
|
||||
|
@ -977,20 +977,6 @@ class SnippetViewSet(ModelViewSet):
|
||||
workflow_history_url_name=self.get_url_name("workflow_history"),
|
||||
)
|
||||
|
||||
@property
|
||||
def redirect_to_edit_view(self):
|
||||
def redirect_to_edit(request, pk):
|
||||
return redirect(self.get_url_name("edit"), pk, permanent=True)
|
||||
|
||||
return redirect_to_edit
|
||||
|
||||
@property
|
||||
def redirect_to_delete_view(self):
|
||||
def redirect_to_delete(request, pk):
|
||||
return redirect(self.get_url_name("delete"), pk, permanent=True)
|
||||
|
||||
return redirect_to_delete
|
||||
|
||||
@property
|
||||
def redirect_to_usage_view(self):
|
||||
def redirect_to_usage(request, pk):
|
||||
@ -1357,7 +1343,11 @@ class SnippetViewSet(ModelViewSet):
|
||||
),
|
||||
]
|
||||
|
||||
legacy_redirects = [
|
||||
return urlpatterns + self._legacy_urlpatterns
|
||||
|
||||
@cached_property
|
||||
def _legacy_urlpatterns(self):
|
||||
return [
|
||||
# legacy URLs that could potentially collide if the pk matches one of the reserved names above
|
||||
# ('add', 'edit' etc) - redirect to the unambiguous version
|
||||
path("<str:pk>/", self.redirect_to_edit_view),
|
||||
@ -1365,8 +1355,6 @@ class SnippetViewSet(ModelViewSet):
|
||||
path("<str:pk>/usage/", self.redirect_to_usage_view),
|
||||
]
|
||||
|
||||
return urlpatterns + legacy_redirects
|
||||
|
||||
def get_edit_handler(self):
|
||||
"""
|
||||
Returns the appropriate edit handler for this ``SnippetViewSet`` class.
|
||||
|
Loading…
Reference in New Issue
Block a user