mirror of
https://github.com/wagtail/wagtail.git
synced 2024-12-01 11:41:20 +01:00
Move snippet viewsets registration to register_snippet()
This commit is contained in:
parent
8b32236822
commit
6e1e0240ef
@ -2,11 +2,13 @@ from django.contrib.admin.utils import quote
|
||||
from django.core import checks
|
||||
from django.db.models import ForeignKey
|
||||
from django.urls import reverse
|
||||
from django.utils.module_loading import import_string
|
||||
|
||||
from wagtail.admin.admin_url_finder import register_admin_url_finder
|
||||
from wagtail.admin.checks import check_panels_in_model
|
||||
from wagtail.admin.forms.models import register_form_field_override
|
||||
from wagtail.admin.models import get_object_usage
|
||||
from wagtail.admin.viewsets import viewsets
|
||||
|
||||
from .widgets import AdminSnippetChooser
|
||||
|
||||
@ -40,12 +42,36 @@ class SnippetAdminURLFinder:
|
||||
)
|
||||
|
||||
|
||||
def register_snippet(model):
|
||||
def register_snippet(model, viewset=None):
|
||||
if model not in SNIPPET_MODELS:
|
||||
from wagtail.snippets.views.chooser import SnippetChooserViewSet
|
||||
from wagtail.snippets.views.snippets import SnippetViewSet
|
||||
|
||||
model.get_usage = get_object_usage
|
||||
model.usage_url = get_snippet_usage_url
|
||||
model.get_admin_base_path = get_admin_base_path
|
||||
model.get_admin_url_namespace = get_admin_url_namespace
|
||||
|
||||
if viewset is None:
|
||||
viewset = getattr(model, "admin_viewset", SnippetViewSet)
|
||||
if isinstance(viewset, str):
|
||||
viewset = import_string(viewset)
|
||||
|
||||
admin_viewset = viewset(
|
||||
model.get_admin_url_namespace(),
|
||||
model=model,
|
||||
url_prefix=model.get_admin_base_path(),
|
||||
)
|
||||
|
||||
chooser_viewset = SnippetChooserViewSet(
|
||||
f"wagtailsnippetchoosers_{model._meta.app_label}_{model._meta.model_name}",
|
||||
model=model,
|
||||
url_prefix=f"snippets/choose/{model._meta.app_label}/{model._meta.model_name}",
|
||||
)
|
||||
|
||||
viewsets.register(admin_viewset)
|
||||
viewsets.register(chooser_viewset)
|
||||
|
||||
SNIPPET_MODELS.append(model)
|
||||
SNIPPET_MODELS.sort(key=lambda x: x._meta.verbose_name)
|
||||
|
||||
|
@ -2,7 +2,6 @@ from django.contrib.admin.utils import quote
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.urls import include, path, reverse
|
||||
from django.utils.module_loading import import_string
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from wagtail import hooks
|
||||
@ -14,7 +13,6 @@ from wagtail.snippets.permissions import (
|
||||
user_can_edit_snippet_type,
|
||||
user_can_edit_snippets,
|
||||
)
|
||||
from wagtail.snippets.views import chooser as chooser_views
|
||||
from wagtail.snippets.views import snippets as snippet_views
|
||||
from wagtail.snippets.widgets import SnippetListingButton
|
||||
|
||||
@ -33,31 +31,6 @@ def register_admin_urls():
|
||||
]
|
||||
|
||||
|
||||
@hooks.register("register_admin_viewset")
|
||||
def register_viewsets():
|
||||
viewsets = []
|
||||
for model in get_snippet_models():
|
||||
admin_viewset = (
|
||||
getattr(model, "admin_viewset", None) or snippet_views.SnippetViewSet
|
||||
)
|
||||
if isinstance(admin_viewset, str):
|
||||
admin_viewset = import_string(admin_viewset)
|
||||
|
||||
viewsets += [
|
||||
admin_viewset(
|
||||
model.get_admin_url_namespace(),
|
||||
model=model,
|
||||
url_prefix=model.get_admin_base_path(),
|
||||
),
|
||||
chooser_views.SnippetChooserViewSet(
|
||||
f"wagtailsnippetchoosers_{model._meta.app_label}_{model._meta.model_name}",
|
||||
model=model,
|
||||
url_prefix=f"snippets/choose/{model._meta.app_label}/{model._meta.model_name}",
|
||||
),
|
||||
]
|
||||
return viewsets
|
||||
|
||||
|
||||
class SnippetsMenuItem(MenuItem):
|
||||
def is_shown(self, request):
|
||||
return user_can_edit_snippets(request.user)
|
||||
|
@ -63,10 +63,7 @@ class SearchableSnippet(index.Indexed, models.Model):
|
||||
return self.text
|
||||
|
||||
|
||||
@register_snippet
|
||||
class FilterableSnippet(index.Indexed, models.Model):
|
||||
admin_viewset = "wagtail.test.testapp.wagtail_hooks.FilterableSnippetViewSet"
|
||||
|
||||
class CountryCode(models.TextChoices):
|
||||
INDONESIA = "ID"
|
||||
PHILIPPINES = "PH"
|
||||
|
@ -5,6 +5,9 @@ from django.template.response import TemplateResponse
|
||||
from wagtail.admin import messages
|
||||
from wagtail.admin.auth import user_passes_test
|
||||
from wagtail.contrib.forms.views import SubmissionsListView
|
||||
from wagtail.snippets.views.snippets import List as SnippetListView
|
||||
from wagtail.snippets.views.snippets import SnippetViewSet
|
||||
from wagtail.test.snippets.models import FilterableSnippetFilterSet
|
||||
|
||||
|
||||
def user_is_called_bob(user):
|
||||
@ -34,3 +37,12 @@ class CustomSubmissionsListView(SubmissionsListView):
|
||||
"""Returns the filename for CSV file with page title at start"""
|
||||
filename = super().get_csv_filename()
|
||||
return self.form_page.slug + "-" + filename
|
||||
|
||||
|
||||
class FilterableSnippetViewSet(SnippetViewSet):
|
||||
@property
|
||||
def index_view_class(self):
|
||||
class FilterableSnippetList(SnippetListView):
|
||||
filterset_class = FilterableSnippetFilterSet
|
||||
|
||||
return FilterableSnippetList
|
||||
|
@ -11,9 +11,9 @@ from wagtail.admin.site_summary import SummaryItem
|
||||
from wagtail.admin.ui.components import Component
|
||||
from wagtail.admin.views.account import BaseSettingsPanel
|
||||
from wagtail.admin.widgets import Button
|
||||
from wagtail.snippets.views.snippets import List as SnippetListView
|
||||
from wagtail.snippets.views.snippets import SnippetViewSet
|
||||
from wagtail.test.snippets.models import FilterableSnippetFilterSet
|
||||
from wagtail.snippets.models import register_snippet
|
||||
from wagtail.test.snippets.models import FilterableSnippet
|
||||
from wagtail.test.testapp.views import FilterableSnippetViewSet
|
||||
|
||||
from .forms import FavouriteColourForm
|
||||
|
||||
@ -225,10 +225,4 @@ def add_broken_links_summary_item(request, items):
|
||||
items.append(BrokenLinksSummaryItem(request))
|
||||
|
||||
|
||||
class FilterableSnippetViewSet(SnippetViewSet):
|
||||
@property
|
||||
def index_view_class(self):
|
||||
class FilterableSnippetList(SnippetListView):
|
||||
filterset_class = FilterableSnippetFilterSet
|
||||
|
||||
return FilterableSnippetList
|
||||
register_snippet(FilterableSnippet, viewset=FilterableSnippetViewSet)
|
||||
|
Loading…
Reference in New Issue
Block a user