0

Working with Grappelli(2.11.1) in Django(1.11.10). I crafted small custom dashboard and look now for some method to variate appearance of elements. In particular I need to show some url-element

self.children.append(modules.LinkList(
    _('e-mail'),
    column=2,
    css_class=('grp-collapse grp-closed'),
    children=[
        {
            'title': _('gmail'),
            'url': 'gmail.com',
            'external': True,
        },
    ]
))

only to users of specified group and to hide it to others. In regular way I would try to use auth.get_user(), but this depends on 'request'. Which is not available, or at least visible, for me. Any way to make this feature? Thanks anyway.

Naaim Iss
  • 181
  • 1
  • 9

1 Answers1

0

One possible workaround for this (though there are almost certainly better ways):

First, add 'django.template.context_processors.request' to settings.TEMPLATES['OPTIONS']['context_processors'] if it isn't there already. That'll add requests to the template context.

Next, do your changes to the custom dashboard within the init_with_context method, where you now have access to the request (and therefore request.user)

from grappelli.dashboard import modules, Dashboard

class CustomDashboard(Dashboard):

    def init_with_context(self, context):
      request = context['request']
      user = request.user
      groups = user.groups.all()
      if groups == "whatever condition you want":
        self.children.append(
            ...
        )
Robert Townley
  • 3,414
  • 3
  • 28
  • 54