diff --git a/models/upload_session.py b/models/upload_session.py index 55fe6283..ee6324db 100644 --- a/models/upload_session.py +++ b/models/upload_session.py @@ -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) diff --git a/serializer/upload_session.py b/serializer/upload_session.py index f83395d5..fd1da347 100644 --- a/serializer/upload_session.py +++ b/serializer/upload_session.py @@ -1,3 +1,5 @@ +import os + from rest_framework import serializers from gwml2.models.upload_session import UploadSession @@ -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 diff --git a/templates/upload_session/history.html b/templates/upload_session/history.html index 374b500d..df6d88dc 100644 --- a/templates/upload_session/history.html +++ b/templates/upload_session/history.html @@ -166,7 +166,7 @@
${status ? status : ''}
- ${creatingReport ? 'Creating report' : data.task_status !== 'RUNNING' ? ("report") : ""} + ${creatingReport ? 'Creating report' : data.task_status !== 'RUNNING' ? (data.report_filename ? "report" : "-") : ""} ` if (!$row.length) { diff --git a/views/upload_session.py b/views/upload_session.py index 71230c40..0dc6fd8a 100644 --- a/views/upload_session.py +++ b/views/upload_session.py @@ -1,3 +1,5 @@ +import os + from django.core.exceptions import PermissionDenied from django.views.generic.detail import DetailView @@ -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