From d8409a2070804360eec99cee720723ab5b96f747 Mon Sep 17 00:00:00 2001 From: Daniel Greenfeld Date: Thu, 23 May 2013 19:46:03 +0200 Subject: [PATCH] For #134, this moves many independant functions to the utils module and begins conversion of naked ._meta use to using the utils.model_options wrapper --- djadmin2/actions.py | 6 +++--- djadmin2/apiviews.py | 11 +++++++---- djadmin2/core.py | 14 ++++++++------ djadmin2/models.py | 5 +++-- djadmin2/templatetags/admin2_tags.py | 8 +++++--- djadmin2/utils.py | 28 ++++++++++++++++++++++++++++ djadmin2/viewmixins.py | 2 +- 7 files changed, 55 insertions(+), 19 deletions(-) create mode 100644 djadmin2/utils.py diff --git a/djadmin2/actions.py b/djadmin2/actions.py index ae259dd..e2f9391 100644 --- a/djadmin2/actions.py +++ b/djadmin2/actions.py @@ -1,9 +1,9 @@ +from django.contrib import messages from django.core.exceptions import PermissionDenied from django.template.response import TemplateResponse from django.utils.text import capfirst -from django.contrib import messages - +from . import utils def get_description(action): if hasattr(action, 'description'): @@ -24,7 +24,7 @@ def delete_selected(request, queryset): # done. (Hint: I think we can do better.) model = queryset.model - opts = model._meta + opts = utils.model_options(model) permission_name = '%s.delete.%s' \ % (opts.app_label, opts.object_name.lower()) has_permission = request.user.has_perm(permission_name) diff --git a/djadmin2/apiviews.py b/djadmin2/apiviews.py index 6a87fe8..4e84144 100644 --- a/djadmin2/apiviews.py +++ b/djadmin2/apiviews.py @@ -5,6 +5,7 @@ from rest_framework.response import Response from rest_framework.reverse import reverse from rest_framework.views import APIView +from . import utils from .viewmixins import Admin2Mixin API_VERSION = '0.1' @@ -43,19 +44,21 @@ class IndexAPIView(Admin2APIMixin, APIView): def get_model_data(self, model): model_admin = self.registry[model] + model_options = utils.model_options(model) opts = { 'current_app': model_admin.admin.name, - 'app_label': model._meta.app_label, - 'model_name': model._meta.object_name.lower(), + 'app_label': model_options.app_label, + 'model_name': model_options.object_name.lower(), } model_url = reverse( '%(current_app)s:%(app_label)s_%(model_name)s_api-list' % opts, request=self.request, format=self.kwargs.get('format')) + model_options = utils.model_options(model) return { 'url': model_url, - 'verbose_name': force_str(model._meta.verbose_name), - 'verbose_name_plural': force_str(model._meta.verbose_name_plural), + 'verbose_name': force_str(model_options.verbose_name), + 'verbose_name_plural': force_str(model_options.verbose_name_plural), } def get_app_data(self, app_label, models): diff --git a/djadmin2/core.py b/djadmin2/core.py index 55a7b8a..18c0fa3 100644 --- a/djadmin2/core.py +++ b/djadmin2/core.py @@ -10,6 +10,7 @@ from django.utils.importlib import import_module from . import apiviews from . import models +from . import utils from . import views @@ -50,7 +51,7 @@ class Admin2(object): self.registry[model] = model_admin(model, admin=self, **kwargs) # Add the model to the apps registry - app_label = model._meta.app_label + app_label = utils.model_options(model).app_label if app_label in self.apps.keys(): self.apps[app_label][model] = self.registry[model] else: @@ -69,7 +70,7 @@ class Admin2(object): # Remove the model from the apps registry # Get the app label - app_label = model._meta.app_label + app_label = utils.model_options(model).app_label # Delete the model from it's app registry del self.apps[app_label][model] @@ -110,14 +111,15 @@ class Admin2(object): self.api_index_view.as_view(**self.get_api_index_kwargs()), name='api-index'), ) for model, model_admin in self.registry.iteritems(): + model_options = utils.model_options(model) urlpatterns += patterns('', url('^{}/{}/'.format( - model._meta.app_label, - model._meta.object_name.lower()), + model_options.app_label, + model_options.object_name.lower()), include(model_admin.urls)), url('^api/v0/{}/{}/'.format( - model._meta.app_label, - model._meta.object_name.lower()), + model_options.app_label, + model_options.object_name.lower()), include(model_admin.api_urls)), ) return urlpatterns diff --git a/djadmin2/models.py b/djadmin2/models.py index 6c9e92d..660c7ed 100644 --- a/djadmin2/models.py +++ b/djadmin2/models.py @@ -17,7 +17,8 @@ from djadmin2 import apiviews from djadmin2 import constants from djadmin2 import views from djadmin2 import actions -from djadmin2.forms import modelform_factory, floppify_form +from djadmin2 import utils +from djadmin2.forms import modelform_factory class BaseAdmin2(object): @@ -64,7 +65,7 @@ class BaseAdmin2(object): """ if not user.is_authenticated() or not user.is_staff: return False - opts = self.model._meta + opts = utils.model_options(self.model) full_permission_name = '%s.%s_%s' % (opts.app_label, permission_type, opts.object_name.lower()) return user.has_perm(full_permission_name, obj) diff --git a/djadmin2/templatetags/admin2_tags.py b/djadmin2/templatetags/admin2_tags.py index 67e81d8..e518326 100644 --- a/djadmin2/templatetags/admin2_tags.py +++ b/djadmin2/templatetags/admin2_tags.py @@ -2,13 +2,15 @@ from django import template register = template.Library() +from .. import utils + @register.filter def admin2_urlname(view, action): """ Converts the view and the specified action into a valid namespaced URLConf name. """ - return 'admin2:%s_%s_%s' % (view.app_label, view.model_name, action) + return utils.admin2_urlname(view, action) @register.filter @@ -16,7 +18,7 @@ def model_verbose_name(obj): """ Returns the verbose name of a model instance or class. """ - return obj._meta.verbose_name + return utils.model_verbose_name(obj) @register.filter @@ -24,7 +26,7 @@ def model_verbose_name_plural(obj): """ Returns the pluralized verbose name of a model instance or class. """ - return obj._meta.verbose_name_plural + utils.model_verbose_name_plural(obj) @register.filter diff --git a/djadmin2/utils.py b/djadmin2/utils.py new file mode 100644 index 0000000..637f77c --- /dev/null +++ b/djadmin2/utils.py @@ -0,0 +1,28 @@ +def model_options(model): + """ + Wrapper for accessing model._meta. If this access point changes in core + Django, this function allows django-admin2 to address the change with + what should hopefully be less disruption to the rest of the code base. + """ + return model._meta + + +def admin2_urlname(view, action): + """ + Converts the view and the specified action into a valid namespaced URLConf name. + """ + return 'admin2:%s_%s_%s' % (view.app_label, view.model_name, action) + + +def model_verbose_name(obj): + """ + Returns the verbose name of a model instance or class. + """ + return model_options(obj).verbose_name + + +def model_verbose_name_plural(obj): + """ + Returns the pluralized verbose name of a model instance or class. + """ + return model_options(obj).verbose_name_plural diff --git a/djadmin2/viewmixins.py b/djadmin2/viewmixins.py index 1053074..bc2c9ab 100644 --- a/djadmin2/viewmixins.py +++ b/djadmin2/viewmixins.py @@ -8,7 +8,7 @@ from django.forms.models import modelform_factory from braces.views import AccessMixin from . import constants -from .templatetags.admin2_tags import admin2_urlname +from .utils import admin2_urlname class Admin2Mixin(object):