Source code for adminlinks.templatetags.adminlinks_buttons

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import logging
from classytags.arguments import Argument, StringArgument, ChoiceArgument
from classytags.core import Options
from django.template.base import Library
from classytags.helpers import InclusionTag
from django.template.defaultfilters import yesno
from adminlinks.templatetags.utils import (context_passes_test,
                                           get_admin_site,
                                           get_registered_modeladmins,
                                           _admin_link_shortcut,
                                           _add_link_to_context,
                                           _add_custom_link_to_context)
register = Library()
logger = logging.getLogger(__name__)



[docs]class Edit(BaseAdminLink, InclusionTag): """ An :class:`~classytags.helpers.InclusionTag` to render a link to the admin change form for an object:: {% render_edit_button my_obj %} {% render_edit_button my_obj "my_custom_admin" %} {% render_edit_button my_obj "my_custom_admin" "a=1&b=2&a=3" %} """ template = 'adminlinks/edit_link.html' # uses :attr:`~adminlinks.templatetags.adminlinks_buttons.BaseAdminLink.base_options` options = Options(*BaseAdminLink.base_options)
register.tag(name='render_edit_button', compile_function=Edit)
[docs]class EditField(BaseAdminLink, InclusionTag): """ An :class:`~classytags.helpers.InclusionTag` to render a link to a customised admin change form for an object, showing only the requested field:: {% render_edit_field_button my_obj "field_name" %} {% render_edit_field_button my_obj "field_name" "my_custom_admin" %} {% render_edit_field_button my_obj "field_name" "my_custom_admin" "a=1&b=2&a=3" %} .. note:: Use of this class requires that the :class:`~django.contrib.admin.ModelAdmin` includes :class:`~adminlinks.admin.AdminlinksMixin` or otherwise creates a named url ending in `change_field`. .. versionchanged:: 0.8.1 The default template, ``adminlinks/edit_field_link.html`` now expects to be able to use ``{% load static %}`` if the field being edited is either a :class:`~django.db.models.BooleanField` or a :class:`~django.db.models.NullBooleanField`, so that it can render an icon. """ template = 'adminlinks/edit_field_link.html' options = Options(BaseAdminLink.base_options[0], # obj StringArgument('fieldname', required=True), *BaseAdminLink.base_options[1:]) # admin_site, querystring
register.tag(name='render_edit_field_button', compile_function=EditField)
[docs]class Delete(BaseAdminLink, InclusionTag): """ An :class:`~classytags.helpers.InclusionTag` to render a link to the delete confirmation form for an object:: {% render_delete_button my_obj %} {% render_delete_button my_obj "my_custom_admin" %} {% render_delete_button my_obj "my_custom_admin" "a=1&b=2&a=3" %} """ template = 'adminlinks/delete_link.html' # uses :attr:`~adminlinks.templatetags.adminlinks_buttons.BaseAdminLink.base_options` options = Options(*BaseAdminLink.base_options)
register.tag(name='render_delete_button', compile_function=Delete)
[docs]class Add(BaseAdminLink, InclusionTag): """ An :class:`~classytags.helpers.InclusionTag` to render a link to the :meth:`~django.contrib.admin.ModelAdmin.add_view` for a :class:`~django.db.models.Model` mounted onto a :class:`~django.contrib.admin.ModelAdmin` on the :class:`~django.contrib.admin.AdminSite`:: {% render_add_button my_class %} {% render_add_button my_class "my_custom_admin" %} {% render_add_button my_class "my_custom_admin" "a=1&b=2&a=3" %} """ template = 'adminlinks/add_link.html' # uses :attr:`~adminlinks.templatetags.adminlinks_buttons.BaseAdminLink.base_options` options = Options(*BaseAdminLink.base_options)
register.tag(name='render_add_button', compile_function=Add)
[docs]class History(BaseAdminLink, InclusionTag): """ An :class:`~classytags.helpers.InclusionTag` to render a link to the object's :meth:`~django.contrib.admin.ModelAdmin.history_view` in a :class:`~django.contrib.admin.ModelAdmin` instance:: {% render_history_button my_obj %} {% render_history_button my_obj "my_custom_admin" %} {% render_history_button my_obj "my_custom_admin" "a=1&b=2&a=3" %} """ #: what gets rendered by this tag. template = 'adminlinks/history_link.html' # uses :attr:`~adminlinks.templatetags.adminlinks_buttons.BaseAdminLink.base_options` options = Options(*BaseAdminLink.base_options)
register.tag(name='render_history_button', compile_function=History)
[docs]class ChangeList(BaseAdminLink, InclusionTag): """ An :class:`~classytags.helpers.InclusionTag` to render a link to the :meth:`~django.contrib.admin.ModelAdmin.changelist_view` (paginated objects) for a :class:`~django.contrib.admin.ModelAdmin` instance:: {% render_changelist_button my_class %} {% render_changelist_button my_class "my_custom_admin" %} {% render_changelist_button my_class "my_custom_admin" "a=1&b=2&a=3" %} """ template = 'adminlinks/changelist_link.html' # This needs to have a different default querystring, because of # https://code.djangoproject.com/ticket/20288#ticket options = Options(BaseAdminLink.base_options[0], # obj BaseAdminLink.base_options[1], # admin_site Argument('querystring', required=False, default=''))
register.tag(name='render_changelist_button', compile_function=ChangeList)
[docs]class Combined(BaseAdminLink, InclusionTag): """ This needs reworking, I think. """ template = 'adminlinks/grouped_link.html' options = Options( Argument('obj', required=True), StringArgument('admin_site', required=False, default='admin'), )
register.tag(name='render_admin_buttons', compile_function=Combined) class AdminRoot(InclusionTag): options = Options(BaseAdminLink.base_options[1], # admin_site BaseAdminLink.base_options[2], # querystring 'for', ChoiceArgument('who', required=False, resolve=False, default='staff', choices=['staff', 'superusers', 'anyone', 'all', 'no-one', 'noone', 'none'])) template = 'adminlinks/admin_root_link.html' def get_context(self, context, *args, **kwargs): """ Entry point for all subsequent tags. Tests the context and bails early if possible. """ who_can_see = kwargs.get('who') if who_can_see in ('no-one', 'noone', 'none'): logger.debug("No-one should see; didn't check context") return {} if (who_can_see not in ('anyone', 'all') and not context_passes_test(context)): logger.debug('Invalid context; button visibility was restricted') return {} if who_can_see == 'staff' and not context['request'].user.is_staff: logger.debug('Valid context, but user is not marked as staff') return {} if (who_can_see == 'superusers' and not context['request'].user.is_superuser): logger.debug('Valid context, but user is not a superuser') return {} return self.get_link_context(context, *args, **kwargs) def get_link_context(self, context, admin_site, querystring, *args, **kwargs): site = get_admin_site(admin_site) if site is None: logger.debug('Invalid admin site ...') return {} index_link = _admin_link_shortcut('%(namespace)s:index' % { 'namespace': site.name, }, params=None, query=querystring) return { 'link': index_link } register.tag(name='render_admin_button', compile_function=AdminRoot) class AdminlinksToggle(AdminRoot): template = 'adminlinks/admin_toggle.html' register.tag(name='render_toggle_button', compile_function=AdminlinksToggle)