diff options
| author | Adrian Holovaty <adrian@holovaty.com> | 2007-02-25 20:14:38 +0000 |
|---|---|---|
| committer | Adrian Holovaty <adrian@holovaty.com> | 2007-02-25 20:14:38 +0000 |
| commit | 1bedf002a4273180857af63a1c85afd909d83c79 (patch) | |
| tree | 030f4f131e27da924569acb6adca3b0a32447ecb | |
| parent | 8a70334640b31229d53e982bd59d951fe15aecbc (diff) | |
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
| -rw-r--r-- | django/contrib/admin/__init__.py | 2 | ||||
| -rw-r--r-- | django/contrib/admin/sites.py | 43 |
2 files changed, 45 insertions, 0 deletions
diff --git a/django/contrib/admin/__init__.py b/django/contrib/admin/__init__.py index e69de29bb2..13cf13cc4f 100644 --- a/django/contrib/admin/__init__.py +++ b/django/contrib/admin/__init__.py @@ -0,0 +1,2 @@ +from django.contrib.admin.options import ModelAdmin +from django.contrib.admin.sites import AdminSite, site diff --git a/django/contrib/admin/sites.py b/django/contrib/admin/sites.py new file mode 100644 index 0000000000..d6aa66e327 --- /dev/null +++ b/django/contrib/admin/sites.py @@ -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() |
