Registering and making the app verbose names accessible into the views.

This commit is contained in:
Douglas Miranda 2013-08-05 03:35:03 -04:00
parent fa86f85afc
commit 7f830c8aeb
4 changed files with 41 additions and 2 deletions

View file

@ -46,6 +46,8 @@ class Admin2APIMixin(Admin2Mixin):
class IndexAPIView(Admin2APIMixin, APIView):
apps = None
registry = None
app_verbose_names = None
app_verbose_name = None
def get_model_data(self, model):
model_admin = self.registry[model]
@ -73,6 +75,7 @@ class IndexAPIView(Admin2APIMixin, APIView):
return {
'app_label': app_label,
'models': model_data,
'app_verbose_name': unicode(self.app_verbose_names.get(app_label))
}
def get(self, request):

View file

@ -33,6 +33,7 @@ class Admin2(object):
def __init__(self, name='admin2'):
self.registry = {}
self.apps = {}
self.app_verbose_names = {}
self.name = name
def register(self, model, model_admin=None, **kwargs):
@ -85,6 +86,33 @@ class Admin2(object):
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):
"""
Autodiscovers all admin2.py modules for apps in INSTALLED_APPS by
@ -112,18 +140,21 @@ class Admin2(object):
def get_index_kwargs(self):
return {
'registry': self.registry,
'app_verbose_names': self.app_verbose_names,
'apps': self.apps,
}
def get_app_index_kwargs(self):
return {
'registry': self.registry,
'app_verbose_names': self.app_verbose_names,
'apps': self.apps,
}
def get_api_index_kwargs(self):
return {
'registry': self.registry,
'app_verbose_names': self.app_verbose_names,
'apps': self.apps,
}

View file

@ -116,8 +116,10 @@ class AdminModel2Mixin(Admin2Mixin):
context = super(AdminModel2Mixin, self).get_context_data(**kwargs)
model = self.get_model()
model_meta = model_options(model)
app_verbose_names = self.model_admin.admin.app_verbose_names
context.update({
'app_label': model_meta.app_label,
'app_verbose_name': app_verbose_names.get(model_meta.app_label),
'model_name': model_meta.verbose_name,
'model_name_pluralized': model_meta.verbose_name_plural
})

View file

@ -56,11 +56,13 @@ class IndexView(Admin2Mixin, generic.TemplateView):
default_template_name = "index.html"
registry = None
apps = None
app_verbose_names = None
def get_context_data(self, **kwargs):
data = super(IndexView, self).get_context_data(**kwargs)
data.update({
'apps': self.apps,
'app_verbose_names': self.app_verbose_names,
})
return data
@ -69,15 +71,16 @@ class AppIndexView(Admin2Mixin, generic.TemplateView):
default_template_name = "app_index.html"
registry = None
apps = None
app_verbose_names = None
def get_context_data(self, **kwargs):
data = super(AppIndexView, self).get_context_data(**kwargs)
app_label = self.kwargs['app_label']
registry = self.apps[app_label]
data.update({
'app_label': app_label,
'registry': registry,
'app_verbose_names': self.app_verbose_names,
})
return data
@ -104,7 +107,7 @@ class ModelListView(AdminModel2Mixin, generic.ListView):
# If action_callable is a class subclassing from
# actions.BaseListAction then we generate the callable object.
if hasattr(action_callable, "process_queryset"):
response = action_callable.as_view(queryset=queryset)(request)
response = action_callable.as_view(queryset=queryset, model_admin=self.model_admin)(request)
else:
# generate the reponse if a function.
response = action_callable(request, queryset)