Refs #9602 -- Moved AlreadyRegistered/NotRegistered exceptions to django.contrib.admin.exceptions.

This commit is contained in:
Mariusz Felisiak 2023-07-07 13:22:06 +02:00 committed by GitHub
parent eed096574f
commit 2584783f46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 30 additions and 28 deletions

View File

@ -3,6 +3,7 @@ from itertools import chain
from django.apps import apps
from django.conf import settings
from django.contrib.admin.exceptions import NotRegistered
from django.contrib.admin.utils import NotRelationField, flatten, get_fields_from_path
from django.core import checks
from django.core.exceptions import FieldDoesNotExist
@ -220,8 +221,6 @@ class BaseModelAdminChecks:
ManyToManyField and that the item has a related ModelAdmin with
search_fields defined.
"""
from django.contrib.admin.sites import NotRegistered
try:
field = obj.model._meta.get_field(field_name)
except FieldDoesNotExist:

View File

@ -11,3 +11,15 @@ class DisallowedModelAdminToField(SuspiciousOperation):
"""Invalid to_field was passed to admin view via URL query string"""
pass
class AlreadyRegistered(Exception):
"""The model is already registered."""
pass
class NotRegistered(Exception):
"""The model is not registered."""
pass

View File

@ -7,6 +7,7 @@ certain test -- e.g. being a DateField or ForeignKey.
"""
import datetime
from django.contrib.admin.exceptions import NotRegistered
from django.contrib.admin.options import IncorrectLookupParameters
from django.contrib.admin.utils import (
build_q_object_from_lookup_parameters,
@ -257,8 +258,6 @@ class RelatedFieldListFilter(FieldListFilter):
"""
Return the model admin's ordering for related field, if provided.
"""
from django.contrib.admin.sites import NotRegistered
try:
related_admin = model_admin.admin_site.get_model_admin(
field.remote_field.model

View File

@ -14,7 +14,7 @@ from django.contrib.admin.checks import (
InlineModelAdminChecks,
ModelAdminChecks,
)
from django.contrib.admin.exceptions import DisallowedModelAdminToField
from django.contrib.admin.exceptions import DisallowedModelAdminToField, NotRegistered
from django.contrib.admin.templatetags.admin_urls import add_preserved_filters
from django.contrib.admin.utils import (
NestedObjects,
@ -160,8 +160,6 @@ class BaseModelAdmin(metaclass=forms.MediaDefiningClass):
If kwargs are given, they're passed to the form Field's constructor.
"""
from django.contrib.admin.sites import NotRegistered
# If the field specifies choices, we don't need to look for special
# admin widgets - we just need to use a select widget of some kind.
if db_field.choices:
@ -252,8 +250,6 @@ class BaseModelAdmin(metaclass=forms.MediaDefiningClass):
ordering. Otherwise don't specify the queryset, let the field decide
(return None in that case).
"""
from django.contrib.admin.sites import NotRegistered
try:
related_admin = self.admin_site.get_model_admin(db_field.remote_field.model)
except NotRegistered:

View File

@ -4,6 +4,7 @@ from weakref import WeakSet
from django.apps import apps
from django.conf import settings
from django.contrib.admin import ModelAdmin, actions
from django.contrib.admin.exceptions import AlreadyRegistered, NotRegistered
from django.contrib.admin.views.autocomplete import AutocompleteJsonView
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.core.exceptions import ImproperlyConfigured
@ -25,14 +26,6 @@ from django.views.i18n import JavaScriptCatalog
all_sites = WeakSet()
class AlreadyRegistered(Exception):
pass
class NotRegistered(Exception):
pass
class AdminSite:
"""
An AdminSite object encapsulates an instance of the Django admin application, ready

View File

@ -1,4 +1,5 @@
from django.apps import apps
from django.contrib.admin.exceptions import NotRegistered
from django.core.exceptions import FieldDoesNotExist, PermissionDenied
from django.http import Http404, JsonResponse
from django.views.generic.list import BaseListView
@ -74,8 +75,6 @@ class AutocompleteJsonView(BaseListView):
Raise Http404 if the target model admin is not configured properly with
search_fields.
"""
from django.contrib.admin.sites import NotRegistered
term = request.GET.get("term", "")
try:
app_label = request.GET["app_label"]

View File

@ -3014,14 +3014,14 @@ Templates can override or extend base admin templates as described in
as options to the admin class.
Raises :class:`~django.core.exceptions.ImproperlyConfigured` if a model is
abstract. and ``django.contrib.admin.sites.AlreadyRegistered`` if a model
is already registered.
abstract. and ``django.contrib.admin.exceptions.AlreadyRegistered`` if a
model is already registered.
.. method:: AdminSite.unregister(model_or_iterable)
Unregisters the given model class (or iterable of classes).
Raises ``django.contrib.admin.sites.NotRegistered`` if a model isn't
Raises ``django.contrib.admin.exceptions.NotRegistered`` if a model isn't
already registered.
.. method:: AdminSite.get_model_admin(model)
@ -3029,7 +3029,7 @@ Templates can override or extend base admin templates as described in
.. versionadded:: 5.0
Returns an admin class for the given model class. Raises
``django.contrib.admin.sites.NotRegistered`` if a model isn't registered.
``django.contrib.admin.exceptions.NotRegistered`` if a model isn't registered.
.. method:: AdminSite.get_log_entries(request)

View File

@ -487,6 +487,9 @@ Miscellaneous
* The minimum supported version of ``selenium`` is increased from 3.8.0 to
4.8.0.
* The ``AlreadyRegistered`` and ``NotRegistered`` exceptions are moved from
``django.contrib.admin.sites`` to ``django.contrib.admin.exceptions``.
.. _deprecated-features-5.0:
Features deprecated in 5.0

View File

@ -1,5 +1,6 @@
from django.contrib import admin
from django.contrib.admin.decorators import register
from django.contrib.admin.exceptions import AlreadyRegistered, NotRegistered
from django.contrib.admin.sites import site
from django.core.exceptions import ImproperlyConfigured
from django.test import SimpleTestCase
@ -35,7 +36,7 @@ class TestRegistration(SimpleTestCase):
def test_prevent_double_registration(self):
self.site.register(Person)
msg = "The model Person is already registered in app 'admin_registration'."
with self.assertRaisesMessage(admin.sites.AlreadyRegistered, msg):
with self.assertRaisesMessage(AlreadyRegistered, msg):
self.site.register(Person)
def test_prevent_double_registration_for_custom_admin(self):
@ -47,12 +48,12 @@ class TestRegistration(SimpleTestCase):
"The model Person is already registered with "
"'admin_registration.PersonAdmin'."
)
with self.assertRaisesMessage(admin.sites.AlreadyRegistered, msg):
with self.assertRaisesMessage(AlreadyRegistered, msg):
self.site.register(Person, PersonAdmin)
def test_unregister_unregistered_model(self):
msg = "The model Person is not registered"
with self.assertRaisesMessage(admin.sites.NotRegistered, msg):
with self.assertRaisesMessage(NotRegistered, msg):
self.site.unregister(Person)
def test_registration_with_star_star_options(self):
@ -61,7 +62,7 @@ class TestRegistration(SimpleTestCase):
def test_get_model_admin_unregister_model(self):
msg = "The model Person is not registered."
with self.assertRaisesMessage(admin.sites.NotRegistered, msg):
with self.assertRaisesMessage(NotRegistered, msg):
self.site.get_model_admin(Person)
def test_star_star_overrides(self):

View File

@ -3,7 +3,7 @@ import json
from contextlib import contextmanager
from django.contrib import admin
from django.contrib.admin.sites import NotRegistered
from django.contrib.admin.exceptions import NotRegistered
from django.contrib.admin.tests import AdminSeleniumTestCase
from django.contrib.admin.views.autocomplete import AutocompleteJsonView
from django.contrib.auth.models import Permission, User