Ad

How To Hide A Button And How To Have A New Tab Redirection On It?

- 1 answer

I'm working as an intern for a company on GeoNode 2.8. I have to hide the Download Layer button if the user is not in the permission list. The previous intern made it in an older version of Geonode by adding a {% get_obj_perms request.user for resource.layer as "perms" %} before the button display function. But when I add it on my version of GeoNode, it didn't change anything and the button still there.

{% if resource.storeType != "remoteStore" %}
    {% get_obj_perms request.user for resource.layer as "layer_perms" %}
    <li class="list-group-item">
    {% if links or links_download %}
       <button class="btn btn-primary btn-md btn-block" data-toggle="modal" data-target="#download-layer">{% trans "Download Layer" %}</button>
    {% else %}
      {% if request.user.is_authenticated %}
          <button class="btn btn-primary btn-md btn-block" id="request-download">{% trans "Request Download" %}</button>
      {% endif %}
    {% endif %}
    </li>
{% endif %}

Second part, I have a button in a navbar with a redirection like this <li><a target="_blank" rel="nofollow noreferrer" href="{% url "help" %}">{% trans "Help" %}</a></li>

My issue is the next one, how can I have a redirection but in a new tab, I thought I could use target = _blank but it's django and it doesn't work. like this: <li><a target= "_blank" target="_blank" rel="nofollow noreferrer" href="{% url "help" %}">{% trans "Help" %}</a></li>

I think it's something to change in the urls.py but I'm new in it so I don't know how to add this redirection.

    url(r'^help/$',
        TemplateView.as_view(template_name='help.html'),
        name='help'),
Ad

Answer

You have fetched the permissions of the user on the specified object as

{% get_obj_perms request.user for resource.layer as "layer_perms" %}

But you have not used the permissions obtained from this line. So you need to check if the user has a permission (e.g. can_view, can_download, etc.) on the layer object.

You can change your code as so:

{% if resource.storeType != "remoteStore" %}
    {% get_obj_perms request.user for resource.layer as "layer_perms" %}
    <li class="list-group-item">

    {% if links or links_download %}
       {% if "can_download" in layer_perms %} # change here, check if user has required permissions
           <button class="btn btn-primary btn-md btn-block" data-toggle="modal" data-target="#download-layer">{% trans "Download Layer" %}</button>
       {% endif %} # close the condition
    {% else %}
      {% if request.user.is_authenticated %}
          <button class="btn btn-primary btn-md btn-block" id="request-download">{% trans "Request Download" %}</button>
      {% endif %}
    {% endif %}
    </li>
{% endif %}

Make sure you change your code as per your requirement.

For further reference, you can have a look at guardian-template-tags

Ad
source: stackoverflow.com
Ad