mirror of
https://github.com/django/django.git
synced 2025-07-04 17:59:13 +00:00
newforms-admin: First-draft implementation of AdminSite
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@4581 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
8a70334640
commit
1bedf002a4
@ -0,0 +1,2 @@
|
|||||||
|
from django.contrib.admin.options import ModelAdmin
|
||||||
|
from django.contrib.admin.sites import AdminSite, site
|
43
django/contrib/admin/sites.py
Normal file
43
django/contrib/admin/sites.py
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
from django.contrib.admin import ModelAdmin
|
||||||
|
from django.db.models import Model
|
||||||
|
|
||||||
|
class AlreadyRegistered(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class NotRegistered(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class AdminSite(object):
|
||||||
|
def __init__(self):
|
||||||
|
self._registry = {} # model_class -> admin_class
|
||||||
|
|
||||||
|
def register(model_or_iterable, admin_class=None, **options):
|
||||||
|
"""
|
||||||
|
Registers the given model(s) with the given admin class.
|
||||||
|
|
||||||
|
If an admin class isn't given, it will use ModelAdmin (the default
|
||||||
|
admin options). If keyword arguments are given -- e.g., list_display --
|
||||||
|
they'll be applied as options to the admin class.
|
||||||
|
|
||||||
|
If a model is already registered, this will raise AlreadyRegistered.
|
||||||
|
"""
|
||||||
|
admin_class = admin_class or ModelAdmin
|
||||||
|
# TODO: Handle options
|
||||||
|
if issubclass(model_or_iterable, Model):
|
||||||
|
model_or_iterable = [model_or_iterable]
|
||||||
|
for model in model_or_iterable:
|
||||||
|
if model in self._registry:
|
||||||
|
raise AlreadyRegistered('The model %s is already registered' % model.__class__.__name__)
|
||||||
|
self._registry[model] = admin_class
|
||||||
|
|
||||||
|
def unregister(model_or_iterable):
|
||||||
|
if issubclass(model_or_iterable, Model):
|
||||||
|
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.__class__.__name__)
|
||||||
|
del self._registry[model]
|
||||||
|
|
||||||
|
# This global object represents the default admin site, for the common case.
|
||||||
|
# You can instantiate AdminSite in your own code to create a custom admin site.
|
||||||
|
site = AdminSite()
|
Loading…
x
Reference in New Issue
Block a user