mirror of
https://github.com/jazzband/django-admin2.git
synced 2026-04-03 14:50:36 +00:00
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
This commit is contained in:
parent
638ae91756
commit
d8409a2070
7 changed files with 55 additions and 19 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
28
djadmin2/utils.py
Normal file
28
djadmin2/utils.py
Normal file
|
|
@ -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
|
||||
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Reference in a new issue