Skip to content

Commit

Permalink
Refs django#9602 -- Moved AlreadyRegistered/NotRegistered exceptions …
Browse files Browse the repository at this point in the history
…django.contrib.admin.exceptions.
  • Loading branch information
felixxm committed Jul 7, 2023
1 parent eed0965 commit c50433a
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 28 deletions.
3 changes: 1 addition & 2 deletions django/contrib/admin/checks.py
Expand Up @@ -3,6 +3,7 @@

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
Expand Down Expand Up @@ -220,8 +221,6 @@ def _check_autocomplete_fields_item(self, obj, field_name, label):
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:
Expand Down
12 changes: 12 additions & 0 deletions django/contrib/admin/exceptions.py
Expand Up @@ -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
3 changes: 1 addition & 2 deletions django/contrib/admin/filters.py
Expand Up @@ -7,6 +7,7 @@
"""
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,
Expand Down Expand Up @@ -257,8 +258,6 @@ def field_admin_ordering(self, field, request, model_admin):
"""
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
Expand Down
6 changes: 1 addition & 5 deletions django/contrib/admin/options.py
Expand Up @@ -14,7 +14,7 @@
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,
Expand Down Expand Up @@ -160,8 +160,6 @@ def formfield_for_dbfield(self, db_field, request, **kwargs):
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:
Expand Down Expand Up @@ -252,8 +250,6 @@ def get_field_queryset(self, db, db_field, request):
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:
Expand Down
9 changes: 1 addition & 8 deletions django/contrib/admin/sites.py
Expand Up @@ -4,6 +4,7 @@
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
Expand All @@ -25,14 +26,6 @@
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
Expand Down
3 changes: 1 addition & 2 deletions django/contrib/admin/views/autocomplete.py
@@ -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
Expand Down Expand Up @@ -74,8 +75,6 @@ def process_request(self, request):
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"]
Expand Down
8 changes: 4 additions & 4 deletions docs/ref/contrib/admin/index.txt
Expand Up @@ -3014,22 +3014,22 @@ 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)

.. 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)

Expand Down
3 changes: 3 additions & 0 deletions docs/releases/5.0.txt
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions tests/admin_registration/tests.py
@@ -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
Expand Down Expand Up @@ -35,7 +36,7 @@ def test_registration_with_model_admin(self):
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):
Expand All @@ -47,12 +48,12 @@ class PersonAdmin(admin.ModelAdmin):
"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):
Expand All @@ -61,7 +62,7 @@ def test_registration_with_star_star_options(self):

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):
Expand Down
2 changes: 1 addition & 1 deletion tests/admin_views/test_autocomplete_view.py
Expand Up @@ -3,7 +3,7 @@
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
Expand Down

0 comments on commit c50433a

Please sign in to comment.