Skip to content

Commit

Permalink
[Fixes #4956] "Original Dataset" Link behavior improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
afabiani committed Oct 17, 2019
1 parent 593716f commit 6336b19
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 53 deletions.
41 changes: 25 additions & 16 deletions geonode/layers/templates/layers/layer_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{% load base_tags %}
{% load guardian_tags %}
{% load client_lib_tags %}
{% load proxy_lib_tags %}

{% block title %}{{ resource.title|default:resource.alternate }} — {{ block.super }}{% endblock %}

Expand Down Expand Up @@ -291,7 +292,7 @@ <h4 class="modal-title" id="myModalLabel">{% trans "Download Layer" %}</h4>
<li class="active">
<ul>
{% for link in links %}
<li><a href="{{ link.url }}" target="_blank">{% trans link.name %}</a></li>
<li><a href="{{ link.url }}" target="_blank">{% trans link.name %}</a></li>
{% endfor %}
</ul>
</li>
Expand Down Expand Up @@ -399,14 +400,22 @@ <h4 class="modal-title" id="myModalLabel">{% trans "Download Layer" %}</h4>

<!-- DOWNLOAD FILTER - END -->

<li>
<h4>{% trans "Pick your download format:" %}</h4>
<ul>
{% for link in links_download %}
<h4>{% trans "Pick your download format:" %}</h4>
<ul>
{% for link in links_download %}
{% if link.name == 'Original Dataset' %}
{% original_link_available resource.id as original_dwn_link_available %}
{% if original_dwn_link_available %}
<li><a href="{{ link.url }}" target="_blank" id="{{ link.name | slugify }}" class="urls">{% trans link.name %}</a></li>
{% endfor %}
</ul>
</li>
{% else %}
<!-- Hide the link instead. Uncomment the lines below to present a warning to the users. -->
<!-- li><a href="javascript:thumbnailFeedbacks('{% trans "No data available for this resource. Please contact a system administrator or a manager." %}', '{% trans "Warning" %}');" id="{{ link.name | slugify }}" class="urls">{% trans link.name %}</a></li -->
{% endif %}
{% else %}
<li><a href="{{ link.url }}" target="_blank" id="{{ link.name | slugify }}" class="urls">{% trans link.name %}</a></li>
{% endif %}
{% endfor %}
</ul>
</div>
{% endif %}
</div>
Expand Down Expand Up @@ -1012,14 +1021,14 @@ <h1>{% trans "Processing..." %}</h1>

{% if GEONODE_SECURITY_ENABLED %}
{% include "_permissions_form_js.html" %}
<script type='text/javascript'>
$(function() {
if (sessionStorage.getItem("security_refresh_trigger")) {
sessionStorage.clear();
$("#security_refresh").trigger('click');
}
});
</script>
<script type='text/javascript'>
$(function() {
if (sessionStorage.getItem("security_refresh_trigger")) {
sessionStorage.clear();
$("#security_refresh").trigger('click');
}
});
</script>
{% endif %}

{% if FAVORITE_ENABLED %}
Expand Down
19 changes: 19 additions & 0 deletions geonode/proxy/templatetags/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
#########################################################################
#
# Copyright (C) 2019 OSGeo
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
59 changes: 59 additions & 0 deletions geonode/proxy/templatetags/proxy_lib_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# -*- coding: utf-8 -*-
#########################################################################
#
# Copyright (C) 2019 OSGeo
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
from django import template
from django.utils.translation import ugettext as _
from django.core.files.storage import default_storage as storage

from geonode.utils import resolve_object
from geonode.layers.models import Layer, LayerFile

register = template.Library()


@register.simple_tag(takes_context=True)
def original_link_available(context, resourceid):

_not_permitted = _("You are not permitted to save or edit this resource.")

request = context['request']
instance = resolve_object(request,
Layer,
{'pk': resourceid},
permission='base.download_resourcebase',
permission_msg=_not_permitted)

if isinstance(instance, Layer):
layer_files = []
try:
upload_session = instance.get_upload_session()
if upload_session:
layer_files = [
item for idx, item in enumerate(LayerFile.objects.filter(upload_session=upload_session))]

if layer_files:
for l in layer_files:
if not storage.exists(l.file):
return False
else:
return False
except BaseException:
return False
return True
return False
87 changes: 57 additions & 30 deletions geonode/proxy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import os
import re
import json
import shutil
import logging
import tempfile
Expand All @@ -35,6 +34,7 @@
from django.http.request import validate_host
from django.views.generic import View
from django.views.decorators.csrf import requires_csrf_token
from django.template import loader
from distutils.version import StrictVersion
from django.utils.translation import ugettext as _
from django.core.files.storage import default_storage as storage
Expand Down Expand Up @@ -218,30 +218,56 @@ def _get_message(text):

def download(request, resourceid, sender=Layer):

_not_authorized = _("You are not authorized to download this resource.")
_not_permitted = _("You are not permitted to save or edit this resource.")
_no_files_found = _("No files have been found for this resource. Please, contact a system administrator.")

instance = resolve_object(request,
sender,
{'pk': resourceid},
permission='base.download_resourcebase',
permission_msg=_("You are not permitted to save or edit this resource."))
permission_msg=_not_permitted)

if isinstance(instance, Layer):
layer_files = []
try:
upload_session = instance.get_upload_session()
layer_files = [item for idx, item in enumerate(LayerFile.objects.filter(upload_session=upload_session))]

# Create Target Folder
dirpath = tempfile.mkdtemp()
dir_time_suffix = get_dir_time_suffix()
target_folder = os.path.join(dirpath, dir_time_suffix)
if not os.path.exists(target_folder):
os.makedirs(target_folder)

# Copy all Layer related files into a temporary folder
for l in layer_files:
if storage.exists(l.file):
geonode_layer_path = storage.path(l.file)
base_filename, original_ext = os.path.splitext(geonode_layer_path)
shutil.copy2(geonode_layer_path, target_folder)
if upload_session:
layer_files = [
item for idx, item in enumerate(LayerFile.objects.filter(upload_session=upload_session))]

if layer_files:
# Create Target Folder
dirpath = tempfile.mkdtemp()
dir_time_suffix = get_dir_time_suffix()
target_folder = os.path.join(dirpath, dir_time_suffix)
if not os.path.exists(target_folder):
os.makedirs(target_folder)

# Copy all Layer related files into a temporary folder
for l in layer_files:
if storage.exists(l.file):
geonode_layer_path = storage.path(l.file)
base_filename, original_ext = os.path.splitext(geonode_layer_path)
shutil.copy2(geonode_layer_path, target_folder)
else:
return HttpResponse(
loader.render_to_string(
'401.html',
context={
'error_title': _("No files found."),
'error_message': _no_files_found
},
request=request), status=404)
else:
return HttpResponse(
loader.render_to_string(
'401.html',
context={
'error_title': _("No files found."),
'error_message': _no_files_found
},
request=request), status=404)

# Let's check for associated SLD files (if any)
try:
Expand Down Expand Up @@ -337,20 +363,21 @@ def download(request, resourceid, sender=Layer):
tb = traceback.format_exc()
logger.debug(tb)
return HttpResponse(
json.dumps({
'error': 'file_not_found'
}),
status=404,
content_type="application/json"
)

loader.render_to_string(
'401.html',
context={
'error_title': _("No files found."),
'error_message': _no_files_found
},
request=request), status=404)
return HttpResponse(
json.dumps({
'error': 'unauthorized_request'
}),
status=403,
content_type="application/json"
)
loader.render_to_string(
'401.html',
context={
'error_title': _("Not Authorized"),
'error_message': _not_authorized
},
request=request), status=403)


class OWSListView(View):
Expand Down
16 changes: 9 additions & 7 deletions geonode/templates/401.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ <h3>
{% endif %}
</h3>
</div>
<p>
{% if error_message %}
<p>
{{ error_message }}
</p>
{% else %}
<p>
{% blocktrans %}
You are not allowed to perform this operation.
{% endblocktrans %}
{% endif %}
</p>
<p>
{% if not request.user.is_anonymous %}
{% blocktrans %}Please verify that you are logged in as the correct user.{%endblocktrans%}
{% else %}
{% blocktrans %}Please login or register and retry.{% endblocktrans %}
{% endif %}
{% if not request.user.is_anonymous %}
{% blocktrans %}Please verify that you are logged in as the correct user.{%endblocktrans%}
{% else %}
{% blocktrans %}Please login or register and retry.{% endblocktrans %}
{% endif %}
</p>
{% endif %}
{% endblock %}
14 changes: 14 additions & 0 deletions geonode/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,20 @@
var staticUrl = '{% static '' %}';
</script>

<script type="text/javascript">
function thumbnailFeedbacks(data, status) {
try {
$("#_thumbnail_feedbacks").find('.modal-title').text(status);
$("#_thumbnail_feedbacks").find('.modal-body').text(data);
$("#_thumbnail_feedbacks").modal("show");
} catch (err) {
console.log(err);
} finally {
return true;
}
}
</script>

</head>

<body class="{% block body_class %}{% endblock %}">
Expand Down

0 comments on commit 6336b19

Please sign in to comment.