Merge branch 'master' of github.com:pydanny/django-admin2

This commit is contained in:
Daniel Greenfeld 2013-05-19 10:02:17 +02:00
commit 701304224e
2 changed files with 40 additions and 11 deletions

View file

@ -23,20 +23,25 @@ Our goal is to make this API work:
.. code-block:: python
# myapp/admin2.py
# Import your custom models
from .models import Post, Comment
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.models import User
# Import the Admin2 base class
from admin2.models import Admin2
import djadmin2
from djadmin2.models import ModelAdmin2
# Import your custom models
from blog.models import Post
# Instantiate the Admin2 class
# Then attach the admin2 object to your model
Post.admin2 = Admin2()
class UserAdmin2(ModelAdmin2):
create_form_class = UserCreationForm
update_form_class = UserChangeForm
# Register each model with the admin
djadmin2.default.register(Post)
djadmin2.default.register(Comment)
djadmin2.default.register(User, UserAdmin2)
.. note:: You will notice a difference between how and django.contrib.admin and django-admin2 do configuration. The former associates the configuration class with the model object via a registration utility, and the latter does so by adding the configuration class as an attribute of the model object.
Themes
========

View file

@ -9,6 +9,13 @@ from . import views
class Admin2(object):
"""
The base Admin2 object.
It keeps a registry of all registered Models and collects the urls of their
related ModelAdmin2 instances.
It also provides an index view that serves as an entry point to the admin site.
"""
index_view = views.IndexView
def __init__(self, name='admin2'):
@ -16,6 +23,15 @@ class Admin2(object):
self.name = name
def register(self, model, modeladmin=None, **kwargs):
"""
Registers the given model with the given admin class.
If no modeladmin is passed, it will use ModelAdmin2. If keyword
arguments are given they will be passed to the admin class on
instantiation.
If a model is already registered, this will raise ImproperlyConfigured.
"""
if model in self.registry:
raise ImproperlyConfigured
if not modeladmin:
@ -23,13 +39,21 @@ class Admin2(object):
self.registry[model] = modeladmin(model, **kwargs)
def deregister(self, model):
"""
Deregisters the given model.
If the model is not already registered, this will raise ImproperlyConfigured.
"""
try:
del self.registry[model]
except KeyError:
raise ImproperlyConfigured
def autodiscover(self):
apps = []
"""
Autodiscovers all admin2.py modules for apps in INSTALLED_APPS by
trying to import them.
"""
for app_name in [x for x in settings.INSTALLED_APPS]:
try:
import_module("%s.admin2" % app_name)