django-admin2/djadmin2/core.py

210 lines
7 KiB
Python
Raw Normal View History

2013-07-07 14:13:48 +00:00
# -*- coding: utf-8 -*-:
2013-05-19 22:50:41 +00:00
"""
2013-07-18 23:36:31 +00:00
WARNING: This file about to undergo major refactoring by @pydanny per
Issue #99.
2013-05-19 22:50:41 +00:00
"""
2013-07-07 14:13:48 +00:00
from __future__ import division, absolute_import, unicode_literals
2013-05-19 22:50:41 +00:00
from importlib import import_module
2016-05-07 21:57:53 +00:00
from django.conf import settings
from django.conf.urls import include, url
from django.core.exceptions import ImproperlyConfigured
2013-05-19 09:45:44 +00:00
from . import apiviews
from . import types
from . import utils
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.
2013-07-18 23:36:31 +00:00
It also provides an index view that serves as an entry point to the
admin site.
2013-05-18 19:45:43 +00:00
"""
index_view = views.IndexView
login_view = views.LoginView
app_index_view = views.AppIndexView
2013-05-19 09:45:44 +00:00
api_index_view = apiviews.IndexAPIView
2013-05-19 07:47:42 +00:00
def __init__(self, name='admin2'):
self.registry = {}
self.apps = {}
self.app_verbose_names = {}
self.name = name
def register(self, model, model_admin=None, **kwargs):
2013-05-18 19:45:43 +00:00
"""
2013-05-19 12:38:27 +00:00
Registers the given model with the given admin class. 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
If no model_admin is passed, it will use ModelAdmin2. If keyword
2013-05-18 19:45:43 +00:00
arguments are given they will be passed to the admin class on
instantiation.
If a model is already registered, this will raise ImproperlyConfigured.
"""
2013-05-18 16:28:31 +00:00
if model in self.registry:
2013-07-18 23:36:31 +00:00
raise ImproperlyConfigured(
'%s is already registered in django-admin2' % model)
if not model_admin:
model_admin = types.ModelAdmin2
self.registry[model] = model_admin(model, admin=self, **kwargs)
# Add the model to the apps registry
app_label = utils.model_options(model).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-19 12:38:27 +00:00
Deregisters the given model. Remove the model from the self.app as well
2013-05-18 19:45:43 +00:00
2013-07-18 23:36:31 +00:00
If the model is not already registered, this will raise
ImproperlyConfigured.
2013-05-18 19:45:43 +00:00
"""
2013-05-18 16:28:31 +00:00
try:
del self.registry[model]
except KeyError:
2013-07-18 23:36:31 +00:00
raise ImproperlyConfigured(
'%s was never registered in django-admin2' % model)
2013-05-19 12:38:27 +00:00
# Remove the model from the apps registry
# Get the app label
app_label = utils.model_options(model).app_label
2013-05-19 12:38:27 +00:00
# Delete the model from it's app registry
del self.apps[app_label][model]
# if no more models in an app's registry
# then delete the app from the apps.
if self.apps[app_label] is {}:
del self.apps[app_label] # no
def register_app_verbose_name(self, app_label, app_verbose_name):
"""
Registers the given app label with the given app verbose name.
If a app_label is already registered, this will raise
ImproperlyConfigured.
"""
if app_label in self.app_verbose_names:
raise ImproperlyConfigured(
'%s is already registered in django-admin2' % app_label)
self.app_verbose_names[app_label] = app_verbose_name
def deregister_app_verbose_name(self, app_label):
"""
Deregisters the given app label. Remove the app label from the
self.app_verbose_names as well.
If the app label is not already registered, this will raise
ImproperlyConfigured.
"""
try:
del self.app_verbose_names[app_label]
except KeyError:
raise ImproperlyConfigured(
'%s app label was never registered in django-admin2' % app_label)
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:
2014-09-22 03:01:24 +00:00
if str(e).startswith("No module named") and 'admin2' in str(e):
continue
raise e
def get_admin_by_name(self, name):
"""
Returns the admin instance that was registered with the passed in
name.
"""
for object_admin in self.registry.values():
if object_admin.name == name:
return object_admin
2013-07-18 23:36:31 +00:00
raise ValueError(
u'No object admin found with name {}'.format(repr(name)))
def get_index_kwargs(self):
return {
'registry': self.registry,
'app_verbose_names': self.app_verbose_names,
2013-05-19 11:03:25 +00:00
'apps': self.apps,
'login_view': self.login_view,
}
def get_app_index_kwargs(self):
return {
'registry': self.registry,
'app_verbose_names': self.app_verbose_names,
'apps': self.apps,
}
2013-05-19 09:45:44 +00:00
def get_api_index_kwargs(self):
return {
'registry': self.registry,
'app_verbose_names': self.app_verbose_names,
'apps': self.apps,
}
def get_urls(self):
2016-05-07 21:57:53 +00:00
urlpatterns = [
url(regex=r'^$',
view=self.index_view.as_view(**self.get_index_kwargs()),
name='dashboard'
2013-07-06 07:37:25 +00:00
),
url(regex='^auth/user/(?P<pk>\d+)/update/password/$',
2013-05-30 23:55:34 +00:00
view=views.PasswordChangeView.as_view(),
2013-05-31 01:25:45 +00:00
name='password_change'
2013-05-30 23:55:34 +00:00
),
url(regex='^password_change_done/$',
view=views.PasswordChangeDoneView.as_view(),
2013-05-31 01:25:45 +00:00
name='password_change_done'
2013-05-30 23:55:34 +00:00
),
url(regex='^logout/$',
view=views.LogoutView.as_view(),
name='logout'
),
2013-07-06 07:37:25 +00:00
url(regex=r'^(?P<app_label>\w+)/$',
2013-07-18 23:36:31 +00:00
view=self.app_index_view.as_view(
**self.get_app_index_kwargs()),
2013-05-28 12:24:18 +00:00
name='app_index'
2013-07-06 07:37:25 +00:00
),
url(regex=r'^api/v0/$',
2013-07-18 23:36:31 +00:00
view=self.api_index_view.as_view(
**self.get_api_index_kwargs()),
name='api_index'
2013-07-06 07:37:25 +00:00
),
2016-05-07 21:57:53 +00:00
]
for model, model_admin in self.registry.items():
model_options = utils.model_options(model)
2016-05-07 21:57:53 +00:00
urlpatterns += [
url('^{}/{}/'.format(
model_options.app_label,
model_options.object_name.lower()),
2018-09-03 07:37:47 +00:00
model_admin.urls),
url('^api/v0/{}/{}/'.format(
model_options.app_label,
model_options.object_name.lower()),
2018-09-03 07:37:47 +00:00
model_admin.api_urls),
2016-05-07 21:57:53 +00:00
]
return urlpatterns
@property
def urls(self):
# We set the application and instance namespace here
2013-05-19 07:47:42 +00:00
return self.get_urls(), self.name, self.name