Skip to content

Commit

Permalink
Report file name and on frontend (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
meomancer authored Aug 25, 2024
1 parent 571ba30 commit ff9b4fc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
9 changes: 8 additions & 1 deletion models/upload_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,14 @@ def create_report_excel(self):
"""Created excel that will contain reports."""

_file = ods_to_xlsx(self.upload_file.path)
_report_file = _file.replace('.xls', '.report.xls')

# Create file report name
ext = os.path.splitext(_file)[1]
_report_file = _file.replace(ext, f'.report{ext}')

# If file report equals file, skip create report
if _report_file == _file:
return
if os.path.exists(_report_file):
os.remove(_report_file)
workbook = openpyxl.load_workbook(_file)
Expand Down
13 changes: 11 additions & 2 deletions serializer/upload_session.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from rest_framework import serializers

from gwml2.models.upload_session import UploadSession
Expand All @@ -16,8 +18,15 @@ def get_organisation(self, obj: UploadSession):
return obj.organisation.name if obj.organisation else '-'

def get_report_filename(self, obj: UploadSession):
_report_file = obj.upload_file.url.replace('.xls', '.report.xls')
return _report_file
_file = obj.upload_file.url
_path = obj.upload_file.path
ext = os.path.splitext(_file)[1]
_report_file = _file.replace(ext, f'.report{ext}')
_report_path = _path.replace(ext, f'.report{ext}')
if os.path.exists(_report_path):
return _report_file
else:
return None

def get_uploaded_at(self, obj: UploadSession):
return obj.timestamp
Expand Down
2 changes: 1 addition & 1 deletion templates/upload_session/history.html
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
</div>
</td>
<td><div class="notes">${status ? status : ''}</div></td>
<td class="report">${creatingReport ? 'Creating report' : data.task_status !== 'RUNNING' ? ("<a href=" + data.report_filename + " target='_blank'>report</a>") : ""}</td>
<td class="report">${creatingReport ? 'Creating report' : data.task_status !== 'RUNNING' ? (data.report_filename ? "<a href=" + data.report_filename + " target='_blank'>report</a>" : "-") : ""}</td>
</tr>
`
if (!$row.length) {
Expand Down
11 changes: 8 additions & 3 deletions views/upload_session.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from django.core.exceptions import PermissionDenied
from django.views.generic.detail import DetailView

Expand All @@ -13,7 +15,10 @@ def get_context_data(self, **kwargs):
**kwargs)
if not self.request.user.is_superuser and self.object.uploader != self.request.user.id:
raise PermissionDenied('You do not have permission.')
context['url'] = self.object.upload_file.url.replace(
'.xls', '.report.xls'
)

# Create file report name
_file = self.object.upload_file.url
ext = os.path.splitext(_file)[1]
_report_file = _file.replace(ext, f'.report{ext}')
context['url'] = _report_file
return context

0 comments on commit ff9b4fc

Please sign in to comment.