def get_link_context(self, context, obj, admin_site, querystring):
        """
        ..todo:: document

        Always returns the existing context.
        """
        site = get_admin_site(admin_site)
        if site is None:
            logger.debug('Invalid admin site')
            return {}

        content_type = get_content_type(obj)
        new_querystring = QueryDict(querystring, mutable=True)
        new_querystring.update({'content_type': content_type.pk,
                                'content_id': obj.pk})
        link = _add_custom_link_to_context(admin_site, context['request'],
                                           opts=EditRegionChunk._meta,
                                           permname='change',
                                           viewname='changelist',
                                           url_params=None,
                                           query=new_querystring.urlencode())

        if link['verbose_name']:
            logger.debug('link created successfully, swapping out the '
                         '`verbose_name` available to the context')
            link['verbose_name'] = EditRegionChunk._meta.verbose_name_plural
            link['obj_name'] = obj._meta.verbose_name
        return link
 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
     }
    def get_link_context(self, context, obj, admin_site):
        """
        Wraps all the other adminlink template tags into one.

        :param context: Hopefully, a :class:`~django.template.RequestContext`
                        otherwise :meth:`~adminlinks.templatetags.adminlinks_buttons.BaseAdminLink.is_valid`
                        is unlikely to be :data:`True`
        :param obj: the :class:`~django.db.models.Model` class to link to.
                    Must have :class:`~django.db.models.Options`
                    from which we can retrieve a
                    :attr:`~django.db.models.Field.verbose_name`
        :param admin_site: name of the admin site to use; defaults to **"admin"**
        :return: the link values.
        :rtype: dictionary.
        """
        opts = obj._meta
        site = get_admin_site(admin_site)
        if site is None:
            logger.debug('Invalid admin site')
            return context

        admins = get_registered_modeladmins(context['request'], site)
        app_key = opts.app_label
        if hasattr(opts, 'model_name'):
            model_key = opts.model_name
        else:
            model_key = opts.module_name
        lookup = (app_key.lower(), model_key.lower())

        if not lookup in admins:
            logger.debug('%s:%s not in admin' % lookup)
            return context

        modeladmin_links = admins[lookup]
        links = {
            'add': _admin_link_shortcut(
                modeladmin_links.get('add', '')
            ),
            'change': _admin_link_shortcut(
                modeladmin_links.get('change', ''), [obj.pk]
            ),
            'history': _admin_link_shortcut(
                modeladmin_links.get('history', ''), [obj.pk]
            ),
            'delete': _admin_link_shortcut(
                modeladmin_links.get('delete', ''), [obj.pk]
            ),
            'changelist': _admin_link_shortcut(
                modeladmin_links.get('changelist', '')
            ),
        }
        return {'links': links, 'verbose_name': opts.verbose_name,
                'verbose_name_plural': opts.verbose_name_plural}
Пример #4
0
def get_modeladmin(obj, admin_namespace='admin'):
    """
    convienience function around finding the modeladmin we want.
    Allows us to provide a class or instance and get back the modeladmin.

    .. testcase:: GetModelAdminTestCase
    """
    model = get_model_class(obj)
    admin = get_admin_site(admin_namespace)
    try:
        return admin._registry[model]
    except KeyError as e:
        if settings.DEBUG:
            msg = '{key} not found in {admin}'.format(key=e.args[0],
                                                      admin=admin.__class__)
            raise ImproperlyConfigured(msg)
        raise
    def get_context(self, context, with_labels, admin_site):
        """
        Updates the *existing* context by putting a list of applicable
        modeladmins into `app_list` assuming the argument `admin_site`
        resolved into an AdminSite instance.

        Always returns the existing context.
        """
        site = get_admin_site(admin_site)

        if context_passes_test(context) and site is not None:
            modeladmins = get_registered_modeladmins(context['request'], site)
            context.update({
                'should_display_toolbar': True,
                'should_display_apps': with_labels,
                'app_list': _resort_modeladmins(modeladmins),
            })
        return context