コード例 #1
0
    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}
コード例 #2
0
 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
     }