django-admin2/djadmin2/core.py

101 lines
3.1 KiB
Python
Raw Normal View History

from django.conf.urls import patterns, include, url
from django.conf import settings
2013-05-18 16:28:31 +00:00
from django.core.exceptions import ImproperlyConfigured
from django.utils.importlib import import_module
from . import models
from . import views
class Admin2(object):
2013-05-18 19:45:43 +00:00
"""
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
2013-05-19 07:47:42 +00:00
def __init__(self, name='admin2'):
self.registry = {}
self.apps = {}
self.name = name
def register(self, model, modeladmin=None, **kwargs):
2013-05-18 19:45:43 +00:00
"""
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.
Once a model is registered in self.registry, we also add it to app registries
in self.apps.
2013-05-18 19:45:43 +00:00
"""
2013-05-18 16:28:31 +00:00
if model in self.registry:
raise ImproperlyConfigured('%s is already registered in django-admin2' % model)
if not modeladmin:
modeladmin = models.ModelAdmin2
self.registry[model] = modeladmin(model, **kwargs)
# Add the model to the apps registry
app_label = model._meta.app_label
if app_label in self.apps.keys():
self.apps[app_label][model] = self.registry[model]
else:
self.apps[app_label] = {model: self.registry[model]}
def deregister(self, model):
2013-05-18 19:45:43 +00:00
"""
2013-05-18 21:32:17 +00:00
Deregisters the given model.
2013-05-18 19:45:43 +00:00
2013-05-18 20:40:35 +00:00
If the model is not already registered, this will raise ImproperlyConfigured.
TODO: Remove the model from the self.app as well
2013-05-18 19:45:43 +00:00
"""
2013-05-18 16:28:31 +00:00
try:
del self.registry[model]
except KeyError:
raise ImproperlyConfigured
def autodiscover(self):
2013-05-18 19:45:43 +00:00
"""
2013-05-18 21:32:17 +00:00
Autodiscovers all admin2.py modules for apps in INSTALLED_APPS by
2013-05-18 19:45:43 +00:00
trying to import them.
"""
for app_name in [x for x in settings.INSTALLED_APPS]:
try:
import_module("%s.admin2" % app_name)
except ImportError as e:
if str(e) == "No module named admin2":
continue
raise e
def get_index_kwargs(self):
return {
'registry': self.registry,
2013-05-19 11:03:25 +00:00
'apps': self.apps,
}
def get_urls(self):
urlpatterns = patterns('',
2013-05-19 07:53:37 +00:00
url(r'^$', self.index_view.as_view(**self.get_index_kwargs()), name='dashboard'),
)
for model, modeladmin in self.registry.iteritems():
app_label = model._meta.app_label
2013-05-19 08:09:12 +00:00
model_name = model._meta.object_name.lower()
urlpatterns += patterns('',
url('^{}/{}/'.format(app_label, model_name),
include(modeladmin.urls)),
)
return urlpatterns
@property
def urls(self):
2013-05-19 07:47:42 +00:00
return self.get_urls(), self.name, self.name