Skip to content

Commit

Permalink
Merge pull request #237 from ImperialCollegeLondon/admin-download-files
Browse files Browse the repository at this point in the history
Admin download files
  • Loading branch information
dandavies99 authored Dec 22, 2022
2 parents 307ed52 + 2acc4c8 commit 4ce3c47
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
38 changes: 34 additions & 4 deletions battDB/admin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import mptt
from django.contrib import admin
from django.contrib.admin.widgets import AdminFileWidget
from django.db.models import FileField
from django.utils.safestring import mark_safe

import common
from common.admin import BaseAdmin
from management.custom_azure import generate_sas_token, get_blob_url

from .models import (
Batch,
Expand Down Expand Up @@ -155,7 +158,32 @@ class DeviceDataInline(common.admin.TabularInline):
extra = 0


class AdminMediaWidget(AdminFileWidget):
"""
Override the default AdminFileWidget to add a download link that uses a SAS token.
"""

def render(self, name, value, attrs=None, renderer=None):
output = []
if value and getattr(value, "url", None):
blob_name = value.name
blob_url = get_blob_url(value)
sas_token = generate_sas_token(blob_name)
output.append(f"<a href={blob_url}?{sas_token}>Download File</a>")

# Put the usual output at the end
output.append(super(AdminFileWidget, self).render(name, value, attrs))
# Remove the current url from the output that is being replaced
if "Currently" in output[-1]:
output[-1] = "<br>" + output[-1].split("<br>")[-1]

return mark_safe("".join(output))


class DataFileInline(admin.StackedInline):
formfield_overrides = {
FileField: {"widget": AdminMediaWidget},
}
readonly_fields = ["size", "local_date", "exists", "hash"]
max_num = 1
model = UploadedFile
Expand Down Expand Up @@ -204,6 +232,10 @@ class DataAdmin(BaseAdmin):
This should be done with a JOIN instead.
"""

formfield_overrides = {
FileField: {"widget": AdminMediaWidget},
}

inlines = [
DataFileInline,
# DeviceDataInline, # TODO: Removed until DeviceColumn properly implemented
Expand Down Expand Up @@ -257,11 +289,9 @@ def get_experiment_link(self, obj):

def get_file_link(self, obj):
if hasattr(obj, "raw_data_file") and obj.raw_data_file is not None:
size = obj.raw_data_file.size()
return mark_safe(
"""
<button type="button"> <a href="%s">\u2193%s</a> </button>
"""
% (obj.raw_data_file.file.url, obj.raw_data_file.size())
f'<button type="button"> <a href={reverse("battDB:Download File", kwargs={"pk": obj.id})}>{size}</a> </button>'
)
else:
return "N/A"
Expand Down
2 changes: 1 addition & 1 deletion battDB/migrations/0035_experimentdatafile_time_recorded.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.1.3 on 2022-12-19 13:28
# Generated by Django 4.1.3 on 2022-12-19 14:12

from django.db import migrations, models

Expand Down

0 comments on commit 4ce3c47

Please sign in to comment.