1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
"""
The parent admin displays the list view of the base model.
"""
from django.contrib import admin
from django.contrib.admin.helpers import AdminErrorList, AdminForm
from django.contrib.admin.templatetags.admin_urls import add_preserved_filters
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ImproperlyConfigured, PermissionDenied
from django.db import models
from django.http import Http404, HttpResponseRedirect
from django.template.response import TemplateResponse
from django.urls import URLResolver
from django.utils.encoding import force_str
from django.utils.http import urlencode
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
from polymorphic.utils import get_base_polymorphic_model
from .forms import PolymorphicModelChoiceForm
class RegistrationClosed(RuntimeError):
"The admin model can't be registered anymore at this point."
class ChildAdminNotRegistered(RuntimeError):
"The admin site for the model is not registered."
class PolymorphicParentModelAdmin(admin.ModelAdmin):
"""
A admin interface that can displays different change/delete pages, depending on the polymorphic model.
To use this class, one attribute need to be defined:
* :attr:`child_models` should be a list models.
Alternatively, the following methods can be implemented:
* :func:`get_child_models` should return a list of models.
* optionally, :func:`get_child_type_choices` can be overwritten to refine the choices for the add dialog.
This class needs to be inherited by the model admin base class that is registered in the site.
The derived models should *not* register the ModelAdmin, but instead it should be returned by :func:`get_child_models`.
"""
#: The base model that the class uses (auto-detected if not set explicitly)
base_model = None
#: The child models that should be displayed
child_models = None
#: Whether the list should be polymorphic too, leave to ``False`` to optimize
polymorphic_list = False
add_type_template = None
add_type_form = PolymorphicModelChoiceForm
#: The regular expression to filter the primary key in the URL.
#: This accepts only numbers as defensive measure against catch-all URLs.
#: If your primary key consists of string values, update this regular expression.
pk_regex = r"(\d+|__fk__)"
def __init__(self, model, admin_site, *args, **kwargs):
super().__init__(model, admin_site, *args, **kwargs)
self._is_setup = False
if self.base_model is None:
self.base_model = get_base_polymorphic_model(model)
def _lazy_setup(self):
if self._is_setup:
return
self._child_models = self.get_child_models()
# Make absolutely sure that the child models don't use the old 0.9 format,
# as of polymorphic 1.4 this deprecated configuration is no longer supported.
# Instead, register the child models in the admin too.
if self._child_models and not issubclass(self._child_models[0], models.Model):
raise ImproperlyConfigured(
"Since django-polymorphic 1.4, the `child_models` attribute "
"and `get_child_models()` method should be a list of models only.\n"
"The model-admin class should be registered in the regular Django admin."
)
self._child_admin_site = self.admin_site
self._is_setup = True
def register_child(self, model, model_admin):
"""
Register a model with admin to display.
"""
# After the get_urls() is called, the URLs of the child model can't be exposed anymore to the Django URLconf,
# which also means that a "Save and continue editing" button won't work.
if self._is_setup:
raise RegistrationClosed("The admin model can't be registered anymore at this point.")
if not issubclass(model, self.base_model):
raise TypeError(f"{model.__name__} should be a subclass of {self.base_model.__name__}")
if not issubclass(model_admin, admin.ModelAdmin):
raise TypeError(
f"{model_admin.__name__} should be a subclass of {admin.ModelAdmin.__name__}"
)
self._child_admin_site.register(model, model_admin)
def get_child_models(self):
"""
Return the derived model classes which this admin should handle.
This should return a list of tuples, exactly like :attr:`child_models` is.
The model classes can be retrieved as ``base_model.__subclasses__()``,
a setting in a config file, or a query of a plugin registration system at your option
"""
if self.child_models is None:
raise NotImplementedError("Implement get_child_models() or child_models")
return self.child_models
def get_child_type_choices(self, request, action):
"""
Return a list of polymorphic types for which the user has the permission to perform the given action.
"""
self._lazy_setup()
choices = []
content_types = ContentType.objects.get_for_models(
*self.get_child_models(), for_concrete_models=False
)
for model, ct in content_types.items():
perm_function_name = f"has_{action}_permission"
model_admin = self._get_real_admin_by_model(model)
perm_function = getattr(model_admin, perm_function_name)
if not perm_function(request):
continue
choices.append((ct.id, model._meta.verbose_name))
return choices
def _get_real_admin(self, object_id, super_if_self=True):
try:
obj = (
self.model.objects.non_polymorphic().values("polymorphic_ctype").get(pk=object_id)
)
except self.model.DoesNotExist:
raise Http404
return self._get_real_admin_by_ct(obj["polymorphic_ctype"], super_if_self=super_if_self)
def _get_real_admin_by_ct(self, ct_id, super_if_self=True):
try:
ct = ContentType.objects.get_for_id(ct_id)
except ContentType.DoesNotExist as e:
raise Http404(e) # Handle invalid GET parameters
model_class = ct.model_class()
if not model_class:
# Handle model deletion
app_label, model = ct.natural_key()
raise Http404(f"No model found for '{app_label}.{model}'.")
return self._get_real_admin_by_model(model_class, super_if_self=super_if_self)
def _get_real_admin_by_model(self, model_class, super_if_self=True):
# In case of a ?ct_id=### parameter, the view is already checked for permissions.
# Hence, make sure this is a derived object, or risk exposing other admin interfaces.
if model_class not in self._child_models:
raise PermissionDenied(
f"Invalid model '{model_class}', it must be registered as child model."
)
try:
# HACK: the only way to get the instance of an model admin,
# is to read the registry of the AdminSite.
real_admin = self._child_admin_site._registry[model_class]
except KeyError:
raise ChildAdminNotRegistered(
f"No child admin site was registered for a '{model_class}' model."
)
if super_if_self and real_admin is self:
return super()
else:
return real_admin
def get_queryset(self, request):
# optimize the list display.
qs = super().get_queryset(request)
if not self.polymorphic_list:
qs = qs.non_polymorphic()
return qs
def add_view(self, request, form_url="", extra_context=None):
"""Redirect the add view to the real admin."""
ct_id = int(request.GET.get("ct_id", 0))
if not ct_id:
# Display choices
return self.add_type_view(request)
else:
real_admin = self._get_real_admin_by_ct(ct_id)
# rebuild form_url, otherwise libraries below will override it.
form_url = add_preserved_filters(
{
"preserved_filters": urlencode({"ct_id": ct_id}),
"opts": self.model._meta,
},
form_url,
)
return real_admin.add_view(request, form_url, extra_context)
def change_view(self, request, object_id, *args, **kwargs):
"""Redirect the change view to the real admin."""
real_admin = self._get_real_admin(object_id)
return real_admin.change_view(request, object_id, *args, **kwargs)
def changeform_view(self, request, object_id=None, *args, **kwargs):
# The `changeform_view` is available as of Django 1.7, combining the add_view and change_view.
# As it's directly called by django-reversion, this method is also overwritten to make sure it
# also redirects to the child admin.
if object_id:
real_admin = self._get_real_admin(object_id)
return real_admin.changeform_view(request, object_id, *args, **kwargs)
else:
# Add view. As it should already be handled via `add_view`, this means something custom is done here!
return super().changeform_view(request, object_id, *args, **kwargs)
def history_view(self, request, object_id, extra_context=None):
"""Redirect the history view to the real admin."""
real_admin = self._get_real_admin(object_id)
return real_admin.history_view(request, object_id, extra_context=extra_context)
def delete_view(self, request, object_id, extra_context=None):
"""Redirect the delete view to the real admin."""
real_admin = self._get_real_admin(object_id)
return real_admin.delete_view(request, object_id, extra_context)
def get_preserved_filters(self, request):
if "_changelist_filters" in request.GET:
request.GET = request.GET.copy()
filters = request.GET.get("_changelist_filters")
f = filters.split("&")
for x in f:
c = x.split("=")
request.GET[c[0]] = c[1]
del request.GET["_changelist_filters"]
return super().get_preserved_filters(request)
def get_urls(self):
"""
Expose the custom URLs for the subclasses and the URL resolver.
"""
urls = super().get_urls()
# At this point. all admin code needs to be known.
self._lazy_setup()
return urls
def subclass_view(self, request, path):
"""
Forward any request to a custom view of the real admin.
"""
ct_id = int(request.GET.get("ct_id", 0))
if not ct_id:
# See if the path started with an ID.
try:
pos = path.find("/")
if pos == -1:
object_id = int(path)
else:
object_id = int(path[0:pos])
except ValueError:
raise Http404(
f"No ct_id parameter, unable to find admin subclass for path '{path}'."
)
ct_id = self.model.objects.values_list("polymorphic_ctype_id", flat=True).get(
pk=object_id
)
real_admin = self._get_real_admin_by_ct(ct_id)
resolver = URLResolver("^", real_admin.urls)
resolvermatch = resolver.resolve(path) # May raise Resolver404
if not resolvermatch:
raise Http404(f"No match for path '{path}' in admin subclass.")
return resolvermatch.func(request, *resolvermatch.args, **resolvermatch.kwargs)
def add_type_view(self, request, form_url=""):
"""
Display a choice form to select which page type to add.
"""
if not self.has_add_permission(request):
raise PermissionDenied
extra_qs = ""
if request.META["QUERY_STRING"]:
# QUERY_STRING is bytes in Python 3, using force_str() to decode it as string.
# See QueryDict how Django deals with that.
# TODO: should this use a Django method instead of manipulating the string directly?
extra_qs = f"&{force_str(request.META['QUERY_STRING'])}"
choices = self.get_child_type_choices(request, "add")
if len(choices) == 0:
raise PermissionDenied
if len(choices) == 1:
return HttpResponseRedirect(f"?ct_id={choices[0][0]}{extra_qs}")
# Create form
form = self.add_type_form(
data=request.POST if request.method == "POST" else None,
initial={"ct_id": choices[0][0]},
)
form.fields["ct_id"].choices = choices
if form.is_valid():
return HttpResponseRedirect(f"?ct_id={form.cleaned_data['ct_id']}{extra_qs}")
# Wrap in all admin layout
fieldsets = ((None, {"fields": ("ct_id",)}),)
adminForm = AdminForm(form, fieldsets, {}, model_admin=self)
media = self.media + adminForm.media
opts = self.model._meta
context = {
"title": _("Add %s") % force_str(opts.verbose_name),
"adminform": adminForm,
"is_popup": ("_popup" in request.POST or "_popup" in request.GET),
"media": mark_safe(media),
"errors": AdminErrorList(form, ()),
"app_label": opts.app_label,
}
return self.render_add_type_form(request, context, form_url)
def render_add_type_form(self, request, context, form_url=""):
"""
Render the page type choice form.
"""
opts = self.model._meta
app_label = opts.app_label
context.update(
{
"has_change_permission": self.has_change_permission(request),
"form_url": mark_safe(form_url),
"opts": opts,
"add": True,
"save_on_top": self.save_on_top,
}
)
templates = self.add_type_template or [
f"admin/{app_label}/{opts.object_name.lower()}/add_type_form.html",
f"admin/{app_label}/add_type_form.html",
"admin/polymorphic/add_type_form.html", # added default here
"admin/add_type_form.html",
]
request.current_app = self.admin_site.name
return TemplateResponse(request, templates, context)
@property
def change_list_template(self):
opts = self.model._meta
app_label = opts.app_label
# Pass the base options
base_opts = self.base_model._meta
base_app_label = base_opts.app_label
return [
f"admin/{app_label}/{opts.object_name.lower()}/change_list.html",
f"admin/{app_label}/change_list.html",
# Added base class:
f"admin/{base_app_label}/{base_opts.object_name.lower()}/change_list.html",
f"admin/{base_app_label}/change_list.html",
"admin/change_list.html",
]
|