Remove Pre-1.0 compatibility hacks

fix_request_path_info
Jerome Leclanche 2017-06-04 01:20:25 +03:00
parent f8852c1281
commit 4dc20a0213
4 changed files with 5 additions and 79 deletions

View File

@ -75,39 +75,9 @@ class PolymorphicParentModelAdmin(admin.ModelAdmin):
if self._is_setup: if self._is_setup:
return return
# By not having this in __init__() there is less stress on import dependencies as well, self._child_models = self.get_child_models()
# considering an advanced use cases where a plugin system scans for the child models.
child_models = self.get_child_models()
# Check if get_child_models() returns an iterable of models (new format) or an iterable
# of (Model, Admin) (legacy format). When iterable is empty, assume the new format.
self._compat_mode = len(child_models) and isinstance(child_models[0], (list, tuple))
if not self._compat_mode:
self._child_models = child_models
self._child_admin_site = self.admin_site self._child_admin_site = self.admin_site
self._is_setup = True self._is_setup = True
return
# Continue only if in compatibility mode
warnings.warn("Using tuples of (Model, ModelAdmin) in PolymorphicParentModelAdmin.child_models is "
"deprecated; instead child_models should be iterable of child models eg. "
"(Model1, Model2, ..) and child admins should be registered to default admin site",
DeprecationWarning)
self._child_admin_site = self.admin_site.__class__(name=self.admin_site.name)
self._child_admin_site.get_app_list = lambda request: () # HACK: workaround for Django 1.9
for Model, Admin in child_models:
self.register_child(Model, Admin)
self._child_models = dict(child_models)
# This is needed to deal with the improved ForeignKeyRawIdWidget in Django 1.4 and perhaps other widgets too.
# The ForeignKeyRawIdWidget checks whether the referenced model is registered in the admin, otherwise it displays itself as a textfield.
# As simple solution, just make sure all parent admin models are also know in the child admin site.
# This should be done after all parent models are registered off course.
complete_registry = self.admin_site._registry.copy()
complete_registry.update(self._child_admin_site._registry)
self._child_admin_site._registry = complete_registry
self._is_setup = True
def register_child(self, model, model_admin): def register_child(self, model, model_admin):
""" """
@ -144,11 +114,7 @@ class PolymorphicParentModelAdmin(admin.ModelAdmin):
""" """
self._lazy_setup() self._lazy_setup()
choices = [] choices = []
for child_model_desc in self.get_child_models(): for model in self.get_child_models():
if self._compat_mode:
model = child_model_desc[0]
else:
model = child_model_desc
perm_function_name = 'has_{0}_permission'.format(action) perm_function_name = 'has_{0}_permission'.format(action)
model_admin = self._get_real_admin_by_model(model) model_admin = self._get_real_admin_by_model(model)
perm_function = getattr(model_admin, perm_function_name) perm_function = getattr(model_admin, perm_function_name)
@ -265,29 +231,8 @@ class PolymorphicParentModelAdmin(admin.ModelAdmin):
# At this point. all admin code needs to be known. # At this point. all admin code needs to be known.
self._lazy_setup() self._lazy_setup()
# Continue only if in compatibility mode
if not self._compat_mode:
return urls return urls
# The redirect at the end acts as catch all.
# The custom urls need to be inserted before that.
redirect_urls = [pat for pat in urls if not pat.name] # redirect URL has no name.
urls = [pat for pat in urls if pat.name]
# Define the catch-all for custom views
custom_urls = [
url(r'^(?P<path>.+)$', self.admin_site.admin_view(self.subclass_view))
]
# Add reverse names for all polymorphic models, so the delete button and "save and add" just work.
# These definitions are masked by the definition above, since it needs special handling (and a ct_id parameter).
dummy_urls = []
for model, _ in self.get_child_models():
admin = self._get_real_admin_by_model(model)
dummy_urls += admin.get_urls()
return urls + custom_urls + dummy_urls + redirect_urls
def subclass_view(self, request, path): def subclass_view(self, request, path):
""" """
Forward any request to a custom view of the real admin. Forward any request to a custom view of the real admin.

View File

@ -1,2 +0,0 @@
# For compatibility with pre 0.8 versions
from .managers import PolymorphicQuerySet, PolymorphicManager # noqa

View File

@ -3,7 +3,7 @@
The manager class for use in the models. The manager class for use in the models.
""" """
from __future__ import unicode_literals from __future__ import unicode_literals
import warnings
from django.db import models from django.db import models
from django.utils.six import python_2_unicode_compatible from django.utils.six import python_2_unicode_compatible
from polymorphic.query import PolymorphicQuerySet from polymorphic.query import PolymorphicQuerySet
@ -33,17 +33,6 @@ class PolymorphicManager(models.Manager):
manager.queryset_class = queryset_class # also set our version, Django uses _queryset_class manager.queryset_class = queryset_class # also set our version, Django uses _queryset_class
return manager return manager
def __init__(self, queryset_class=None, *args, **kwrags):
# Up till polymorphic 0.4, the queryset class could be specified as parameter to __init__.
# However, this doesn't work for related managers which instantiate a new version of this class.
# Hence, for custom managers the new default is using the 'queryset_class' attribute at class level instead.
if queryset_class:
warnings.warn("Using PolymorphicManager(queryset_class=..) is deprecated; override the queryset_class attribute instead", DeprecationWarning)
# For backwards compatibility, still allow the parameter:
self.queryset_class = queryset_class
super(PolymorphicManager, self).__init__(*args, **kwrags)
def get_queryset(self): def get_queryset(self):
qs = self.queryset_class(self.model, using=self._db, hints=self._hints) qs = self.queryset_class(self.model, using=self._db, hints=self._hints)
if self.model._meta.proxy: if self.model._meta.proxy:

View File

@ -169,9 +169,3 @@ class ShowFieldTypeAndContent(ShowFieldBase):
""" model mixin, like ShowFieldContent, but also show field types """ """ model mixin, like ShowFieldContent, but also show field types """
polymorphic_showfield_type = True polymorphic_showfield_type = True
polymorphic_showfield_content = True polymorphic_showfield_content = True
# compatibility with old class names
ShowFieldTypes = ShowFieldType
ShowFields = ShowFieldContent
ShowFieldsAndTypes = ShowFieldTypeAndContent