2019-03-24 01:10:06 +08:00
|
|
|
import re
|
2011-03-28 09:40:43 +08:00
|
|
|
from functools import update_wrapper
|
2017-01-03 05:42:27 +08:00
|
|
|
from weakref import WeakSet
|
2015-01-28 20:35:27 +08:00
|
|
|
|
|
|
|
from django.apps import apps
|
2021-01-12 21:37:56 +08:00
|
|
|
from django.conf import settings
|
2010-12-02 08:44:35 +08:00
|
|
|
from django.contrib.admin import ModelAdmin, actions
|
2021-01-12 18:37:38 +08:00
|
|
|
from django.contrib.admin.views.autocomplete import AutocompleteJsonView
|
2014-02-05 07:45:18 +08:00
|
|
|
from django.contrib.auth import REDIRECT_FIELD_NAME
|
2016-08-02 06:07:50 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db.models.base import ModelBase
|
2021-01-12 21:37:56 +08:00
|
|
|
from django.http import Http404, HttpResponsePermanentRedirect, HttpResponseRedirect
|
2011-04-23 02:17:16 +08:00
|
|
|
from django.template.response import TemplateResponse
|
2021-01-12 21:37:56 +08:00
|
|
|
from django.urls import NoReverseMatch, Resolver404, resolve, reverse
|
2021-02-24 16:17:38 +08:00
|
|
|
from django.utils.decorators import method_decorator
|
2016-12-30 01:51:26 +08:00
|
|
|
from django.utils.functional import LazyObject
|
|
|
|
from django.utils.module_loading import import_string
|
2008-07-19 07:54:34 +08:00
|
|
|
from django.utils.text import capfirst
|
2017-01-27 03:58:33 +08:00
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
from django.utils.translation import gettext_lazy
|
2008-07-19 07:54:34 +08:00
|
|
|
from django.views.decorators.cache import never_cache
|
2021-01-12 21:37:56 +08:00
|
|
|
from django.views.decorators.common import no_append_slash
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.views.decorators.csrf import csrf_protect
|
2016-03-08 04:52:08 +08:00
|
|
|
from django.views.i18n import JavaScriptCatalog
|
2008-07-19 07:54:34 +08:00
|
|
|
|
2017-01-03 05:42:27 +08:00
|
|
|
all_sites = WeakSet()
|
2014-12-14 11:22:37 +08:00
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
class AlreadyRegistered(Exception):
|
|
|
|
pass
|
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
class NotRegistered(Exception):
|
|
|
|
pass
|
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class AdminSite:
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
|
|
|
An AdminSite object encapsulates an instance of the Django admin application, ready
|
2009-04-01 00:07:07 +08:00
|
|
|
to be hooked in to your URLconf. Models are registered with the AdminSite using the
|
2010-10-14 09:24:20 +08:00
|
|
|
register() method, and the get_urls() method can then be used to access Django view
|
|
|
|
functions that present a full admin interface for the collection of registered
|
|
|
|
models.
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2013-09-07 01:31:44 +08:00
|
|
|
|
|
|
|
# Text to put at the end of each page's <title>.
|
2017-01-27 03:58:33 +08:00
|
|
|
site_title = gettext_lazy("Django site admin")
|
2013-09-07 01:31:44 +08:00
|
|
|
|
|
|
|
# Text to put in each page's <h1>.
|
2017-01-27 03:58:33 +08:00
|
|
|
site_header = gettext_lazy("Django administration")
|
2013-09-07 01:31:44 +08:00
|
|
|
|
|
|
|
# Text to put at the top of the admin index page.
|
2017-01-27 03:58:33 +08:00
|
|
|
index_title = gettext_lazy("Site administration")
|
2013-09-07 01:31:44 +08:00
|
|
|
|
2014-08-26 19:10:30 +08:00
|
|
|
# URL for the "View site" link at the top of each admin page.
|
|
|
|
site_url = "/"
|
|
|
|
|
2020-05-06 16:29:51 +08:00
|
|
|
enable_nav_sidebar = True
|
|
|
|
|
2020-11-14 14:24:07 +08:00
|
|
|
empty_value_display = "-"
|
2015-03-13 18:08:03 +08:00
|
|
|
|
2010-12-02 08:44:35 +08:00
|
|
|
login_form = None
|
2008-07-19 07:54:34 +08:00
|
|
|
index_template = None
|
2008-08-24 00:27:12 +08:00
|
|
|
app_index_template = None
|
2010-01-13 07:34:46 +08:00
|
|
|
login_template = None
|
|
|
|
logout_template = None
|
|
|
|
password_change_template = None
|
|
|
|
password_change_done_template = None
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2021-01-12 21:37:56 +08:00
|
|
|
final_catch_all_view = True
|
|
|
|
|
2014-08-14 03:23:21 +08:00
|
|
|
def __init__(self, name="admin"):
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
self._registry = {} # model_class class -> admin_class instance
|
2011-07-31 03:22:43 +08:00
|
|
|
self.name = name
|
2009-04-07 04:23:33 +08:00
|
|
|
self._actions = {"delete_selected": actions.delete_selected}
|
|
|
|
self._global_actions = self._actions.copy()
|
2017-01-03 05:42:27 +08:00
|
|
|
all_sites.add(self)
|
|
|
|
|
2021-07-05 13:07:10 +08:00
|
|
|
def __repr__(self):
|
|
|
|
return f"{self.__class__.__name__}(name={self.name!r})"
|
|
|
|
|
2017-01-03 05:42:27 +08:00
|
|
|
def check(self, app_configs):
|
|
|
|
"""
|
|
|
|
Run the system checks on all ModelAdmins, except if they aren't
|
|
|
|
customized at all.
|
|
|
|
"""
|
|
|
|
if app_configs is None:
|
|
|
|
app_configs = apps.get_app_configs()
|
|
|
|
app_configs = set(app_configs) # Speed up lookups below
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
modeladmins = (
|
|
|
|
o for o in self._registry.values() if o.__class__ is not ModelAdmin
|
|
|
|
)
|
|
|
|
for modeladmin in modeladmins:
|
|
|
|
if modeladmin.model._meta.app_config in app_configs:
|
|
|
|
errors.extend(modeladmin.check())
|
|
|
|
return errors
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def register(self, model_or_iterable, admin_class=None, **options):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Register the given model(s) with the given admin class.
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
The model(s) should be Model classes, not instances.
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2017-01-25 04:31:57 +08:00
|
|
|
If an admin class isn't given, use ModelAdmin (the default admin
|
|
|
|
options). If keyword arguments are given -- e.g., list_display --
|
|
|
|
apply them as options to the admin class.
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2017-01-25 04:31:57 +08:00
|
|
|
If a model is already registered, raise AlreadyRegistered.
|
2011-02-23 21:43:21 +08:00
|
|
|
|
2017-01-25 04:31:57 +08:00
|
|
|
If a model is abstract, raise ImproperlyConfigured.
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2018-01-04 07:52:12 +08:00
|
|
|
admin_class = admin_class or ModelAdmin
|
2008-07-19 07:54:34 +08:00
|
|
|
if isinstance(model_or_iterable, ModelBase):
|
|
|
|
model_or_iterable = [model_or_iterable]
|
|
|
|
for model in model_or_iterable:
|
2011-02-23 21:43:21 +08:00
|
|
|
if model._meta.abstract:
|
2016-03-29 06:33:29 +08:00
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"The model %s is abstract, so it cannot be registered with admin."
|
|
|
|
% model.__name__
|
|
|
|
)
|
2011-02-23 21:43:21 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
if model in self._registry:
|
2019-03-24 01:10:06 +08:00
|
|
|
registered_admin = str(self._registry[model])
|
|
|
|
msg = "The model %s is already registered " % model.__name__
|
|
|
|
if registered_admin.endswith(".ModelAdmin"):
|
|
|
|
# Most likely registered without a ModelAdmin subclass.
|
|
|
|
msg += "in app %r." % re.sub(r"\.ModelAdmin$", "", registered_admin)
|
|
|
|
else:
|
|
|
|
msg += "with %r." % registered_admin
|
|
|
|
raise AlreadyRegistered(msg)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
# Ignore the registration if the model has been
|
|
|
|
# swapped out.
|
|
|
|
if not model._meta.swapped:
|
|
|
|
# If we got **options then dynamically construct a subclass of
|
|
|
|
# admin_class with those **options.
|
|
|
|
if options:
|
|
|
|
# For reasons I don't quite understand, without a __module__
|
|
|
|
# the created class appears to "live" in the wrong place,
|
|
|
|
# which causes issues later on.
|
|
|
|
options["__module__"] = __name__
|
|
|
|
admin_class = type(
|
|
|
|
"%sAdmin" % model.__name__, (admin_class,), options
|
|
|
|
)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2015-09-10 21:05:31 +08:00
|
|
|
# Instantiate the admin class to save in the registry
|
2017-01-03 05:42:27 +08:00
|
|
|
self._registry[model] = admin_class(model, self)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def unregister(self, model_or_iterable):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Unregister the given model(s).
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2017-01-25 04:31:57 +08:00
|
|
|
If a model isn't already registered, raise NotRegistered.
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
|
|
|
if isinstance(model_or_iterable, ModelBase):
|
|
|
|
model_or_iterable = [model_or_iterable]
|
|
|
|
for model in model_or_iterable:
|
|
|
|
if model not in self._registry:
|
|
|
|
raise NotRegistered("The model %s is not registered" % model.__name__)
|
|
|
|
del self._registry[model]
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2014-07-25 20:07:04 +08:00
|
|
|
def is_registered(self, model):
|
|
|
|
"""
|
|
|
|
Check if a model class is registered with this `AdminSite`.
|
|
|
|
"""
|
|
|
|
return model in self._registry
|
|
|
|
|
2009-04-07 04:23:33 +08:00
|
|
|
def add_action(self, action, name=None):
|
|
|
|
"""
|
|
|
|
Register an action to be available globally.
|
|
|
|
"""
|
|
|
|
name = name or action.__name__
|
|
|
|
self._actions[name] = action
|
|
|
|
self._global_actions[name] = action
|
2009-07-13 21:46:31 +08:00
|
|
|
|
2009-04-07 04:23:33 +08:00
|
|
|
def disable_action(self, name):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Disable a globally-registered action. Raise KeyError for invalid names.
|
2009-04-07 04:23:33 +08:00
|
|
|
"""
|
|
|
|
del self._actions[name]
|
2009-07-13 21:46:31 +08:00
|
|
|
|
2009-04-07 04:23:33 +08:00
|
|
|
def get_action(self, name):
|
|
|
|
"""
|
2013-03-29 01:16:53 +08:00
|
|
|
Explicitly get a registered global action whether it's enabled or
|
2017-01-25 04:31:57 +08:00
|
|
|
not. Raise KeyError for invalid names.
|
2009-04-07 04:23:33 +08:00
|
|
|
"""
|
|
|
|
return self._global_actions[name]
|
2009-07-13 21:46:31 +08:00
|
|
|
|
2010-12-02 08:44:35 +08:00
|
|
|
@property
|
2009-04-07 04:23:33 +08:00
|
|
|
def actions(self):
|
|
|
|
"""
|
|
|
|
Get all the enabled actions as an iterable of (name, func).
|
|
|
|
"""
|
2017-08-24 04:48:29 +08:00
|
|
|
return self._actions.items()
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def has_permission(self, request):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return True if the given HttpRequest has permission to view
|
2008-07-19 07:54:34 +08:00
|
|
|
*at least one* page in the admin site.
|
|
|
|
"""
|
2010-01-11 00:51:13 +08:00
|
|
|
return request.user.is_active and request.user.is_staff
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2009-07-13 21:46:31 +08:00
|
|
|
def admin_view(self, view, cacheable=False):
|
2008-08-02 13:56:57 +08:00
|
|
|
"""
|
2009-07-13 21:46:31 +08:00
|
|
|
Decorator to create an admin view attached to this ``AdminSite``. This
|
2009-01-15 04:22:25 +08:00
|
|
|
wraps the view and provides permission checking by calling
|
|
|
|
``self.has_permission``.
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2009-01-15 04:22:25 +08:00
|
|
|
You'll want to use this from within ``AdminSite.get_urls()``:
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2009-01-15 04:22:25 +08:00
|
|
|
class MyAdminSite(AdminSite):
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2009-01-15 04:22:25 +08:00
|
|
|
def get_urls(self):
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import path
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2017-01-21 21:13:44 +08:00
|
|
|
urls = super().get_urls()
|
2014-04-02 08:46:34 +08:00
|
|
|
urls += [
|
2016-10-21 01:29:04 +08:00
|
|
|
path('my_view/', self.admin_view(some_view))
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2009-01-15 04:22:25 +08:00
|
|
|
return urls
|
2009-07-13 21:46:31 +08:00
|
|
|
|
|
|
|
By default, admin_views are marked non-cacheable using the
|
|
|
|
``never_cache`` decorator. If the view can be safely cached, set
|
|
|
|
cacheable=True.
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2009-01-15 04:22:25 +08:00
|
|
|
def inner(request, *args, **kwargs):
|
|
|
|
if not self.has_permission(request):
|
2013-09-07 01:15:09 +08:00
|
|
|
if request.path == reverse("admin:logout", current_app=self.name):
|
2012-02-10 02:56:23 +08:00
|
|
|
index_path = reverse("admin:index", current_app=self.name)
|
|
|
|
return HttpResponseRedirect(index_path)
|
2014-03-10 03:29:00 +08:00
|
|
|
# Inner import to prevent django.contrib.admin (app) from
|
|
|
|
# importing django.contrib.auth.models.User (unrelated model).
|
|
|
|
from django.contrib.auth.views import redirect_to_login
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2014-01-31 22:18:55 +08:00
|
|
|
return redirect_to_login(
|
|
|
|
request.get_full_path(),
|
|
|
|
reverse("admin:login", current_app=self.name),
|
|
|
|
)
|
2009-01-15 04:22:25 +08:00
|
|
|
return view(request, *args, **kwargs)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2009-07-13 21:46:31 +08:00
|
|
|
if not cacheable:
|
|
|
|
inner = never_cache(inner)
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
# We add csrf_protect here so this function can be used as a utility
|
|
|
|
# function for any view, without having to repeat 'csrf_protect'.
|
2010-02-28 05:08:30 +08:00
|
|
|
if not getattr(view, "csrf_exempt", False):
|
|
|
|
inner = csrf_protect(inner)
|
2009-01-15 04:22:25 +08:00
|
|
|
return update_wrapper(inner, view)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2009-01-15 04:22:25 +08:00
|
|
|
def get_urls(self):
|
2014-02-02 02:30:54 +08:00
|
|
|
# Since this module gets imported in the application's root package,
|
|
|
|
# it cannot import models from other applications at the module level,
|
|
|
|
# and django.contrib.contenttypes.views imports ContentType.
|
|
|
|
from django.contrib.contenttypes import views as contenttype_views
|
2020-07-24 14:25:47 +08:00
|
|
|
from django.urls import include, path, re_path
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2009-07-13 21:46:31 +08:00
|
|
|
def wrap(view, cacheable=False):
|
2009-01-15 04:22:25 +08:00
|
|
|
def wrapper(*args, **kwargs):
|
2009-07-13 21:46:31 +08:00
|
|
|
return self.admin_view(view, cacheable)(*args, **kwargs)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2014-12-27 15:16:53 +08:00
|
|
|
wrapper.admin_site = self
|
2009-01-15 04:22:25 +08:00
|
|
|
return update_wrapper(wrapper, view)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2009-01-15 04:22:25 +08:00
|
|
|
# Admin-site-wide views.
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2016-10-21 01:29:04 +08:00
|
|
|
path("", wrap(self.index), name="index"),
|
|
|
|
path("login/", self.login, name="login"),
|
|
|
|
path("logout/", wrap(self.logout), name="logout"),
|
|
|
|
path(
|
|
|
|
"password_change/",
|
|
|
|
wrap(self.password_change, cacheable=True),
|
|
|
|
name="password_change",
|
|
|
|
),
|
|
|
|
path(
|
|
|
|
"password_change/done/",
|
|
|
|
wrap(self.password_change_done, cacheable=True),
|
|
|
|
name="password_change_done",
|
|
|
|
),
|
2021-01-12 18:37:38 +08:00
|
|
|
path("autocomplete/", wrap(self.autocomplete_view), name="autocomplete"),
|
2016-10-21 01:29:04 +08:00
|
|
|
path("jsi18n/", wrap(self.i18n_javascript, cacheable=True), name="jsi18n"),
|
|
|
|
path(
|
|
|
|
"r/<int:content_type_id>/<path:object_id>/",
|
|
|
|
wrap(contenttype_views.shortcut),
|
|
|
|
name="view_on_site",
|
|
|
|
),
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2013-09-07 21:20:04 +08:00
|
|
|
# Add in each model's views, and create a list of valid URLS for the
|
|
|
|
# app_index
|
|
|
|
valid_app_labels = []
|
2015-04-17 22:25:11 +08:00
|
|
|
for model, model_admin in self._registry.items():
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns += [
|
2016-10-21 01:29:04 +08:00
|
|
|
path(
|
|
|
|
"%s/%s/" % (model._meta.app_label, model._meta.model_name),
|
|
|
|
include(model_admin.urls),
|
2022-02-04 03:24:19 +08:00
|
|
|
),
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2013-09-07 21:20:04 +08:00
|
|
|
if model._meta.app_label not in valid_app_labels:
|
|
|
|
valid_app_labels.append(model._meta.app_label)
|
|
|
|
|
|
|
|
# If there were ModelAdmins registered, we should have a list of app
|
|
|
|
# labels for which we need to allow access to the app_index view,
|
|
|
|
if valid_app_labels:
|
|
|
|
regex = r"^(?P<app_label>" + "|".join(valid_app_labels) + ")/$"
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns += [
|
2016-10-21 01:29:04 +08:00
|
|
|
re_path(regex, wrap(self.app_index), name="app_list"),
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2021-01-12 21:37:56 +08:00
|
|
|
|
|
|
|
if self.final_catch_all_view:
|
|
|
|
urlpatterns.append(re_path(r"(?P<url>.*)$", wrap(self.catch_all_view)))
|
|
|
|
|
2009-01-15 04:22:25 +08:00
|
|
|
return urlpatterns
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2010-12-02 08:44:35 +08:00
|
|
|
@property
|
2009-01-15 04:22:25 +08:00
|
|
|
def urls(self):
|
2014-08-14 03:23:21 +08:00
|
|
|
return self.get_urls(), "admin", self.name
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2015-01-01 05:25:00 +08:00
|
|
|
def each_context(self, request):
|
2013-09-07 01:31:44 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return a dictionary of variables to put in the template context for
|
2013-09-07 01:31:44 +08:00
|
|
|
*every* page in the admin site.
|
2015-10-13 14:52:49 +08:00
|
|
|
|
|
|
|
For sites running on a subpath, use the SCRIPT_NAME value if site_url
|
|
|
|
hasn't been customized.
|
2013-09-07 01:31:44 +08:00
|
|
|
"""
|
2015-10-13 14:52:49 +08:00
|
|
|
script_name = request.META["SCRIPT_NAME"]
|
|
|
|
site_url = (
|
|
|
|
script_name if self.site_url == "/" and script_name else self.site_url
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2013-09-07 01:31:44 +08:00
|
|
|
return {
|
|
|
|
"site_title": self.site_title,
|
|
|
|
"site_header": self.site_header,
|
2015-10-13 14:52:49 +08:00
|
|
|
"site_url": site_url,
|
2015-01-01 05:25:00 +08:00
|
|
|
"has_permission": self.has_permission(request),
|
2015-03-30 18:54:25 +08:00
|
|
|
"available_apps": self.get_app_list(request),
|
2017-08-25 17:47:59 +08:00
|
|
|
"is_popup": False,
|
2020-05-06 16:29:51 +08:00
|
|
|
"is_nav_sidebar_enabled": self.enable_nav_sidebar,
|
2013-09-07 01:31:44 +08:00
|
|
|
}
|
|
|
|
|
2014-11-29 14:19:59 +08:00
|
|
|
def password_change(self, request, extra_context=None):
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Handle the "change password" task -- both form display and validation.
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2014-03-24 20:49:05 +08:00
|
|
|
from django.contrib.admin.forms import AdminPasswordChangeForm
|
2013-04-10 05:31:58 +08:00
|
|
|
from django.contrib.auth.views import PasswordChangeView
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-07-31 03:09:23 +08:00
|
|
|
url = reverse("admin:password_change_done", current_app=self.name)
|
2010-01-13 07:34:46 +08:00
|
|
|
defaults = {
|
2013-04-10 05:31:58 +08:00
|
|
|
"form_class": AdminPasswordChangeForm,
|
|
|
|
"success_url": url,
|
2017-12-11 20:08:45 +08:00
|
|
|
"extra_context": {**self.each_context(request), **(extra_context or {})},
|
2010-01-13 07:34:46 +08:00
|
|
|
}
|
|
|
|
if self.password_change_template is not None:
|
|
|
|
defaults["template_name"] = self.password_change_template
|
2015-06-05 23:42:05 +08:00
|
|
|
request.current_app = self.name
|
2013-04-10 05:31:58 +08:00
|
|
|
return PasswordChangeView.as_view(**defaults)(request)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2010-12-02 08:44:35 +08:00
|
|
|
def password_change_done(self, request, extra_context=None):
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Display the "success" page after a password change.
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2013-04-10 05:31:58 +08:00
|
|
|
from django.contrib.auth.views import PasswordChangeDoneView
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2010-12-02 08:44:35 +08:00
|
|
|
defaults = {
|
2017-12-11 20:08:45 +08:00
|
|
|
"extra_context": {**self.each_context(request), **(extra_context or {})},
|
2010-12-02 08:44:35 +08:00
|
|
|
}
|
2010-01-13 07:34:46 +08:00
|
|
|
if self.password_change_done_template is not None:
|
|
|
|
defaults["template_name"] = self.password_change_done_template
|
2015-06-05 23:42:05 +08:00
|
|
|
request.current_app = self.name
|
2013-04-10 05:31:58 +08:00
|
|
|
return PasswordChangeDoneView.as_view(**defaults)(request)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2016-06-19 18:51:58 +08:00
|
|
|
def i18n_javascript(self, request, extra_context=None):
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Display the i18n JavaScript that the Django admin requires.
|
2016-06-19 18:51:58 +08:00
|
|
|
|
|
|
|
`extra_context` is unused but present for consistency with the other
|
|
|
|
admin views.
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2016-03-08 04:52:08 +08:00
|
|
|
return JavaScriptCatalog.as_view(packages=["django.contrib.admin"])(request)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2010-12-02 08:44:35 +08:00
|
|
|
def logout(self, request, extra_context=None):
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Log out the user for the given HttpRequest.
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
This should *not* assume the user is already logged in.
|
|
|
|
"""
|
2016-05-15 23:28:00 +08:00
|
|
|
from django.contrib.auth.views import LogoutView
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2010-12-02 08:44:35 +08:00
|
|
|
defaults = {
|
2017-12-11 20:08:45 +08:00
|
|
|
"extra_context": {
|
|
|
|
**self.each_context(request),
|
2016-01-05 19:29:09 +08:00
|
|
|
# Since the user isn't logged out at this point, the value of
|
|
|
|
# has_permission must be overridden.
|
2017-12-11 20:08:45 +08:00
|
|
|
"has_permission": False,
|
2016-01-05 19:29:09 +08:00
|
|
|
**(extra_context or {}),
|
2017-12-11 20:08:45 +08:00
|
|
|
},
|
2010-12-02 08:44:35 +08:00
|
|
|
}
|
2010-01-13 07:34:46 +08:00
|
|
|
if self.logout_template is not None:
|
|
|
|
defaults["template_name"] = self.logout_template
|
2015-06-05 23:42:05 +08:00
|
|
|
request.current_app = self.name
|
2016-05-15 23:28:00 +08:00
|
|
|
return LogoutView.as_view(**defaults)(request)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2021-02-24 16:17:38 +08:00
|
|
|
@method_decorator(never_cache)
|
2010-12-02 08:44:35 +08:00
|
|
|
def login(self, request, extra_context=None):
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Display the login form for the given HttpRequest.
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2014-01-31 22:18:55 +08:00
|
|
|
if request.method == "GET" and self.has_permission(request):
|
|
|
|
# Already logged-in, redirect to admin index
|
|
|
|
index_path = reverse("admin:index", current_app=self.name)
|
|
|
|
return HttpResponseRedirect(index_path)
|
|
|
|
|
2014-02-02 02:30:54 +08:00
|
|
|
# Since this module gets imported in the application's root package,
|
|
|
|
# it cannot import models from other applications at the module level,
|
|
|
|
# and django.contrib.admin.forms eventually imports User.
|
|
|
|
from django.contrib.admin.forms import AdminAuthenticationForm
|
2020-07-24 14:25:47 +08:00
|
|
|
from django.contrib.auth.views import LoginView
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2017-12-11 20:08:45 +08:00
|
|
|
context = {
|
|
|
|
**self.each_context(request),
|
|
|
|
"title": _("Log in"),
|
2022-01-13 17:10:48 +08:00
|
|
|
"subtitle": None,
|
2017-12-11 20:08:45 +08:00
|
|
|
"app_path": request.get_full_path(),
|
|
|
|
"username": request.user.get_username(),
|
|
|
|
}
|
2014-01-31 18:59:27 +08:00
|
|
|
if (
|
|
|
|
REDIRECT_FIELD_NAME not in request.GET
|
|
|
|
and REDIRECT_FIELD_NAME not in request.POST
|
|
|
|
):
|
2015-06-27 03:56:51 +08:00
|
|
|
context[REDIRECT_FIELD_NAME] = reverse("admin:index", current_app=self.name)
|
2010-12-02 08:44:35 +08:00
|
|
|
context.update(extra_context or {})
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2010-12-02 08:44:35 +08:00
|
|
|
defaults = {
|
|
|
|
"extra_context": context,
|
|
|
|
"authentication_form": self.login_form or AdminAuthenticationForm,
|
|
|
|
"template_name": self.login_template or "admin/login.html",
|
|
|
|
}
|
2015-06-05 23:42:05 +08:00
|
|
|
request.current_app = self.name
|
2016-05-15 23:28:00 +08:00
|
|
|
return LoginView.as_view(**defaults)(request)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2021-01-12 18:37:38 +08:00
|
|
|
def autocomplete_view(self, request):
|
|
|
|
return AutocompleteJsonView.as_view(admin_site=self)(request)
|
|
|
|
|
2021-01-12 21:37:56 +08:00
|
|
|
@no_append_slash
|
|
|
|
def catch_all_view(self, request, url):
|
|
|
|
if settings.APPEND_SLASH and not url.endswith("/"):
|
|
|
|
urlconf = getattr(request, "urlconf", None)
|
|
|
|
try:
|
2021-05-17 19:21:50 +08:00
|
|
|
match = resolve("%s/" % request.path_info, urlconf)
|
2021-01-12 21:37:56 +08:00
|
|
|
except Resolver404:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if getattr(match.func, "should_append_slash", True):
|
2021-05-17 19:21:50 +08:00
|
|
|
return HttpResponsePermanentRedirect("%s/" % request.path)
|
2021-01-12 21:37:56 +08:00
|
|
|
raise Http404
|
|
|
|
|
2015-03-13 19:07:05 +08:00
|
|
|
def _build_app_dict(self, request, label=None):
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Build the app dictionary. The optional `label` parameter filters models
|
|
|
|
of a specific app.
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
|
|
|
app_dict = {}
|
2015-03-13 19:07:05 +08:00
|
|
|
|
|
|
|
if label:
|
|
|
|
models = {
|
|
|
|
m: m_a
|
|
|
|
for m, m_a in self._registry.items()
|
|
|
|
if m._meta.app_label == label
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
models = self._registry
|
|
|
|
|
|
|
|
for model, model_admin in models.items():
|
2008-07-19 07:54:34 +08:00
|
|
|
app_label = model._meta.app_label
|
2015-03-13 19:07:05 +08:00
|
|
|
|
2014-05-29 00:07:27 +08:00
|
|
|
has_module_perms = model_admin.has_module_permission(request)
|
2015-03-13 19:07:05 +08:00
|
|
|
if not has_module_perms:
|
|
|
|
continue
|
|
|
|
|
|
|
|
perms = model_admin.get_model_perms(request)
|
|
|
|
|
|
|
|
# Check whether user has any perm for this module.
|
|
|
|
# If so, add the module to the model_list.
|
|
|
|
if True not in perms.values():
|
|
|
|
continue
|
|
|
|
|
|
|
|
info = (app_label, model._meta.model_name)
|
|
|
|
model_dict = {
|
2020-09-22 16:46:27 +08:00
|
|
|
"model": model,
|
2015-03-13 19:07:05 +08:00
|
|
|
"name": capfirst(model._meta.verbose_name_plural),
|
|
|
|
"object_name": model._meta.object_name,
|
|
|
|
"perms": perms,
|
2017-08-25 17:47:59 +08:00
|
|
|
"admin_url": None,
|
|
|
|
"add_url": None,
|
2015-03-13 19:07:05 +08:00
|
|
|
}
|
2018-05-02 16:39:12 +08:00
|
|
|
if perms.get("change") or perms.get("view"):
|
|
|
|
model_dict["view_only"] = not perms.get("change")
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2015-03-13 19:07:05 +08:00
|
|
|
model_dict["admin_url"] = reverse(
|
|
|
|
"admin:%s_%s_changelist" % info, current_app=self.name
|
|
|
|
)
|
2017-09-07 20:16:21 +08:00
|
|
|
except NoReverseMatch:
|
|
|
|
pass
|
2015-03-13 19:07:05 +08:00
|
|
|
if perms.get("add"):
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2015-03-13 19:07:05 +08:00
|
|
|
model_dict["add_url"] = reverse(
|
|
|
|
"admin:%s_%s_add" % info, current_app=self.name
|
|
|
|
)
|
2017-09-07 20:16:21 +08:00
|
|
|
except NoReverseMatch:
|
|
|
|
pass
|
2015-03-13 19:07:05 +08:00
|
|
|
|
|
|
|
if app_label in app_dict:
|
|
|
|
app_dict[app_label]["models"].append(model_dict)
|
|
|
|
else:
|
|
|
|
app_dict[app_label] = {
|
|
|
|
"name": apps.get_app_config(app_label).verbose_name,
|
|
|
|
"app_label": app_label,
|
|
|
|
"app_url": reverse(
|
|
|
|
"admin:app_list",
|
|
|
|
kwargs={"app_label": app_label},
|
|
|
|
current_app=self.name,
|
|
|
|
),
|
|
|
|
"has_module_perms": has_module_perms,
|
|
|
|
"models": [model_dict],
|
|
|
|
}
|
|
|
|
|
|
|
|
return app_dict
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2022-03-06 00:09:42 +08:00
|
|
|
def get_app_list(self, request, app_label=None):
|
2015-03-13 19:07:05 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return a sorted list of all the installed apps that have been
|
2015-03-30 18:54:25 +08:00
|
|
|
registered in this site.
|
2015-03-13 19:07:05 +08:00
|
|
|
"""
|
2022-03-06 00:09:42 +08:00
|
|
|
app_dict = self._build_app_dict(request, app_label)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# Sort the apps alphabetically.
|
2015-04-17 22:25:11 +08:00
|
|
|
app_list = sorted(app_dict.values(), key=lambda x: x["name"].lower())
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# Sort the models alphabetically within each app.
|
|
|
|
for app in app_list:
|
2010-08-07 00:31:44 +08:00
|
|
|
app["models"].sort(key=lambda x: x["name"])
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2015-03-30 18:54:25 +08:00
|
|
|
return app_list
|
|
|
|
|
|
|
|
def index(self, request, extra_context=None):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Display the main admin index page, which lists all of the installed
|
2015-03-30 18:54:25 +08:00
|
|
|
apps that have been registered in this site.
|
|
|
|
"""
|
|
|
|
app_list = self.get_app_list(request)
|
|
|
|
|
2017-12-11 20:08:45 +08:00
|
|
|
context = {
|
|
|
|
**self.each_context(request),
|
|
|
|
"title": self.index_title,
|
2021-04-24 08:50:27 +08:00
|
|
|
"subtitle": None,
|
2017-12-11 20:08:45 +08:00
|
|
|
"app_list": app_list,
|
|
|
|
**(extra_context or {}),
|
|
|
|
}
|
2014-12-15 00:48:51 +08:00
|
|
|
|
|
|
|
request.current_app = self.name
|
|
|
|
|
2016-03-29 06:33:29 +08:00
|
|
|
return TemplateResponse(
|
|
|
|
request, self.index_template or "admin/index.html", context
|
|
|
|
)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-08-25 11:55:47 +08:00
|
|
|
def app_index(self, request, app_label, extra_context=None):
|
2022-03-06 00:09:42 +08:00
|
|
|
app_list = self.get_app_list(request, app_label)
|
|
|
|
|
|
|
|
if not app_list:
|
2012-02-10 02:56:23 +08:00
|
|
|
raise Http404("The requested admin page does not exist.")
|
2022-03-06 00:09:42 +08:00
|
|
|
|
2017-12-11 20:08:45 +08:00
|
|
|
context = {
|
|
|
|
**self.each_context(request),
|
2022-03-06 00:09:42 +08:00
|
|
|
"title": _("%(app)s administration") % {"app": app_list[0]["name"]},
|
2021-04-24 08:50:27 +08:00
|
|
|
"subtitle": None,
|
2022-03-06 00:09:42 +08:00
|
|
|
"app_list": app_list,
|
2017-12-11 20:08:45 +08:00
|
|
|
"app_label": app_label,
|
|
|
|
**(extra_context or {}),
|
|
|
|
}
|
2011-04-23 02:17:16 +08:00
|
|
|
|
2014-12-15 00:48:51 +08:00
|
|
|
request.current_app = self.name
|
|
|
|
|
2011-04-23 02:17:16 +08:00
|
|
|
return TemplateResponse(
|
|
|
|
request,
|
|
|
|
self.app_index_template
|
|
|
|
or ["admin/%s/app_index.html" % app_label, "admin/app_index.html"],
|
2014-12-15 00:48:51 +08:00
|
|
|
context,
|
|
|
|
)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2016-12-30 01:51:26 +08:00
|
|
|
class DefaultAdminSite(LazyObject):
|
|
|
|
def _setup(self):
|
|
|
|
AdminSiteClass = import_string(apps.get_app_config("admin").default_site)
|
|
|
|
self._wrapped = AdminSiteClass()
|
|
|
|
|
2021-07-05 13:07:10 +08:00
|
|
|
def __repr__(self):
|
|
|
|
return repr(self._wrapped)
|
|
|
|
|
2016-12-30 01:51:26 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# This global object represents the default admin site, for the common case.
|
2016-12-30 01:51:26 +08:00
|
|
|
# You can provide your own AdminSite using the (Simple)AdminConfig.default_site
|
|
|
|
# attribute. You can also instantiate AdminSite in your own code to create a
|
|
|
|
# custom admin site.
|
|
|
|
site = DefaultAdminSite()
|