Repository: mytardis/mytardis

Issue:
No Datasets or Datafiles were selected for downloaded
**Steps to reproduce:**

From the Dataset view, e.g. `/dataset/1`
Don't select any files, and click the "Download Selected Files" button.

**Expected result:**

An info bar or popup message on the same page advising the user to select one or more files to download.

**Actual result:**

The user is redirected to a different page with the grammatically incorrect message:

`No Datasets or Datafiles were selected for downloaded`

![screen shot 2018-07-25 at 1 50 59 pm](https://user-images.githubusercontent.com/2209245/43178660-d019d492-9011-11e8-8880-11f45dc97e49.png)

![screen shot 2018-07-24 at 12 55 18 pm](https://user-images.githubusercontent.com/2209245/43114142-e26b9152-8f40-11e8-9239-22099dad7456.png)



Discussion:

Resolving patch:
diff --git a/tardis/tardis_portal/download.py b/tardis/tardis_portal/download.py
--- a/tardis/tardis_portal/download.py
+++ b/tardis/tardis_portal/download.py
@@ -28,7 +28,7 @@
 import io
 from wsgiref.util import FileWrapper
 
-from django.http import HttpResponseRedirect, StreamingHttpResponse
+from django.http import StreamingHttpResponse
 from django.conf import settings
 from django.utils.dateformat import format as dateformatter
 from django.core.exceptions import ImproperlyConfigured
@@ -43,6 +43,7 @@
 from .auth.decorators import dataset_download_required
 from .shortcuts import render_error_message
 from .shortcuts import return_response_not_found, return_response_error
+from .shortcuts import redirect_back_with_error
 from .util import (get_filesystem_safe_dataset_name,
                    get_filesystem_safe_experiment_name)
 
@@ -100,16 +101,12 @@ def _create_download_response(request, datafile_id, disposition='attachment'):
         # If we can't read the file, return not found
         return return_response_not_found(request)
     except ValueError:  # raised when replica not verified TODO: custom excptn
-        redirect = request.META.get('HTTP_REFERER',
-                                    'http://%s/' %
-                                    request.META.get('HTTP_HOST'))
         message = """The file you are trying to access has not yet been
                      verified. Verification is an automated background process.
                      Please try again later or contact the system
                      administrator if the issue persists."""
         message = ' '.join(message.split())  # removes spaces
-        redirect = redirect + '#error:' + message
-        return HttpResponseRedirect(redirect)
+        return redirect_back_with_error(request, message)
 
 
 def view_datafile(request, datafile_id):
@@ -413,17 +410,13 @@ def _streaming_downloader(request, datafiles, rootdir, filename,
             ua=request.META.get('HTTP_USER_AGENT', None))
         return tfs.get_response(tracker_data)
     except ValueError:  # raised when replica not verified TODO: custom excptn
-        redirect = request.META.get('HTTP_REFERER',
-                                    'http://%s/' %
-                                    request.META.get('HTTP_HOST'))
         message = """The experiment you are trying to access has not yet been
                      verified completely.
                      Verification is an automated background process.
                      Please try again later or contact the system
                      administrator if the issue persists."""
         message = ' '.join(message.split())  # removes spaces
-        redirect = redirect + '#error:' + message
-        return HttpResponseRedirect(redirect)
+        return redirect_back_with_error(request, message)
 
 
 @experiment_download_required
@@ -499,7 +492,7 @@ def get_datafile(dfid):
         else:
             return render_error_message(
                 request,
-                'No Datasets or Datafiles were selected for downloaded',
+                'No datasets or files were selected for download',
                 status=404)
 
     elif 'url' in request.POST:
@@ -520,18 +513,15 @@ def get_datafile(dfid):
                                             datafile_id=datafile.id):
                 df_set = set([datafile])
     else:
-        return render_error_message(
-            request, 'No Datasets or Datafiles were selected for downloaded',
-            status=404)
+        message = "No datasets or datafiles were selected for download"
+        return redirect_back_with_error(request, message)
 
     logger.info('Files for archive command: %s' % df_set)
 
     if not df_set:
-        return render_error_message(
-            request,
-            'You do not have download access for any of the '
-            'selected Datasets or Datafiles ',
-            status=403)
+        message = ("No verified files were accessible to download in the "
+                   "selected dataset(s)")
+        return redirect_back_with_error(request, message)
 
     try:
         expid = request.POST['expid']
diff --git a/tardis/tardis_portal/shortcuts.py b/tardis/tardis_portal/shortcuts.py
--- a/tardis/tardis_portal/shortcuts.py
+++ b/tardis/tardis_portal/shortcuts.py
@@ -6,6 +6,7 @@
 from django.urls import reverse
 from django.shortcuts import render
 from django.http import HttpResponse
+from django.http import HttpResponseRedirect
 from django.template.loader import render_to_string
 from django.contrib.sites.models import Site
 
@@ -62,6 +63,14 @@ def return_response_error(request):
     return render_response_index(request, '403.html', {}, status=403)
 
 
+def redirect_back_with_error(request, message):
+    redirect = request.META.get(
+        'HTTP_REFERER',
+        '{0}://{1}/'.format(request.scheme, request.get_host()))
+    redirect = redirect + '#error:' + message
+    return HttpResponseRedirect(redirect)
+
+
 def get_experiment_referer(request, dataset_id):
     from .auth.decorators import get_accessible_experiments_for_dataset
 


Test patch:
diff --git a/tardis/tardis_portal/tests/test_download.py b/tardis/tardis_portal/tests/test_download.py
--- a/tardis/tardis_portal/tests/test_download.py
+++ b/tardis/tardis_portal/tests/test_download.py
@@ -276,7 +276,7 @@ def testDownload(self):
                                {'expid': self.experiment2.id,
                                 'dataset': [self.dataset2.id],
                                 'datafile': []})
-        self.assertEqual(response.status_code, 403)
+        self.assertEqual(response.status_code, 302)
 
         # check datafile1 download via POST
         response = client.post('/download/datafiles/',
@@ -293,7 +293,7 @@ def testDownload(self):
                                {'expid': self.experiment2.id,
                                 'dataset': [],
                                 'datafile': [self.datafile2.id]})
-        self.assertEqual(response.status_code, 403)
+        self.assertEqual(response.status_code, 302)
 
         # Check datafile2 download with second experiment to "metadata only"
         self.experiment2.public_access = Experiment.PUBLIC_ACCESS_METADATA