Repository: xchem/fragalysis-backend

Issue:
Download links not working
DRF passes back http links for file downloads. These should be https links, or chrome won't allow download. Might be some nginx/django settings that need adjusting

Error: ```Mixed Content: The site at ‘<URL>’ was loaded over a secure connection, but the file at ‘<URL>’ was redirected through an insecure connection. This file should be served over HTTPS. This download has been blocked. See <URL> for more details.```


Discussion:

Resolving patch:
diff --git a/fragalysis/settings.py b/fragalysis/settings.py
--- a/fragalysis/settings.py
+++ b/fragalysis/settings.py
@@ -18,7 +18,7 @@
 from sentry_sdk.integrations.redis import RedisIntegration
 
 # SECURITY WARNING: don't run with debug turned on in production!
-DEBUG = False
+DEBUG = True
 
 # These flags are used in the upload_tset form as follows.
 # Proposal Supported | Proposal Required | Proposal / View fields
@@ -33,7 +33,7 @@
 AUTHENTICATE_UPLOAD = True
 
 if DEBUG == False:
-# By default only call sentry in staging/production
+    # By default only call sentry in staging/production
     sentry_sdk.init(
         dsn="https://27fa0675f555431aa02ca552e93d8cfb@o194333.ingest.sentry.io/1298290",
         integrations=[DjangoIntegration(), CeleryIntegration(), RedisIntegration()],
diff --git a/viewer/serializers.py b/viewer/serializers.py
--- a/viewer/serializers.py
+++ b/viewer/serializers.py
@@ -1,7 +1,8 @@
 import os
 from frag.network.decorate import get_3d_vects_for_mol, get_vect_indices_for_mol
 from frag.network.query import get_full_graph
-from rest_framework import serializers
+#L+
+from urllib.parse import urljoin
 
 from api.utils import draw_mol
 
@@ -26,6 +27,8 @@
 )
 
 from django.contrib.auth.models import User
+#L+
+from django.conf import settings
 
 from rest_framework import serializers
 
@@ -35,9 +38,10 @@ class Meta:
         fields = "__all__"
 
 
-
 class TargetSerializer(serializers.ModelSerializer):
     template_protein = serializers.SerializerMethodField()
+    zip_archive = serializers.SerializerMethodField()
+    metadata = serializers.SerializerMethodField()
 
     def get_template_protein(self, obj):
         proteins = obj.protein_set.filter()
@@ -46,6 +50,24 @@ def get_template_protein(self, obj):
                 return protein.pdb_info.url
         return "NOT AVAILABLE"
 
+    def get_zip_archive(self, obj):
+        request = self.context["request"]
+        # This is to map links to HTTPS to avoid Mixed Content warnings from Chrome browsers
+        # SECURE_PROXY_SSL_HEADER is referenced because it is used in redirecting URLs - if it is changed
+        # it make affect this code.
+        # Using relative links will probably also work, but This workaround allows both the 'download structures'
+        # button and the DRF API call to work.
+        # Note that this link will not work on local
+        https_host = settings.SECURE_PROXY_SSL_HEADER[1]+'://'+request.get_host()
+        return urljoin(https_host, obj.zip_archive.url)
+
+    def get_metadata(self, obj):
+        request = self.context["request"]
+        # This is to map links to HTTPS to avoid Mixed Content warnings from Chrome browsers
+        # Note that this link will not work on local - see above.
+        https_host = settings.SECURE_PROXY_SSL_HEADER[1]+'://'+request.get_host()
+        return urljoin(https_host, obj.metadata.url)
+
     class Meta:
         model = Target
         fields = ("id", "title", "project_id", "protein_set", "template_protein", "metadata", "zip_archive")


Test patch: