def is_valid(self, context, obj, *args, **kwargs): """ Performs some basic tests against the parameters passed to it to ensure that work should carry on afterwards. :param context: a :class:`~django.template.Context` subclass, or dictionary-like object which fulfils certain criteria. Usually a :class:`~django.template.RequestContext`. :param obj: the :class:`~django.db.models.Model`, either as a class or an instance. Or, more specifically, anything which as a :class:`~django.db.models.Options` object stored under the `_meta` attribute. :return: whether or not the context and object pair are valid. :rtype: :data:`True` or :data:`False` .. seealso:: :func:`~adminlinks.templatetags.utils.context_passes_test` """ if not hasattr(obj, '_meta'): logger.debug('Object has no _meta attribute') return False if not context_passes_test(context): logger.debug('Invalid context') return False return True
def get_context(self, context): """ Tests and updates the existing context. :param context: a :class:`~django.template.Context` which is checked via :meth:`~adminlinks.templatetags.utils.context_passes_test`, and the result **always** put into the context. :return: the context, possibly modified with a new layer. :rtype: :class:`~django.template.RequestContext` or other context/ dictionary-like object. """ result = context_passes_test(context) context.update({'should_load_assets': result}) return context
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
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)