Skip to content

Commit d83d64b

Browse files
Merge branch 'hotfix/5.18.1'
2 parents 18b6f0d + 1f3d77e commit d83d64b

File tree

6 files changed

+47
-11
lines changed

6 files changed

+47
-11
lines changed

CITATION.cff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ keywords:
3535
- elasticsearch
3636
- natural language processing
3737
license: MIT
38-
version: 5.18.0
39-
date-released: '2025-04-10'
38+
version: 5.18.1
39+
date-released: '2025-05-22'

backend/download/apps.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
class DownloadConfig(AppConfig):
55
default_auto_field = 'django.db.models.BigAutoField'
66
name = 'download'
7+
8+
def ready(self):
9+
import download.signals

backend/download/signals.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import logging
2+
import os
3+
4+
from django.conf import settings
5+
from django.db.models.signals import post_delete
6+
from django.dispatch import receiver
7+
from download.convert_csv import output_path
8+
9+
from .models import Download
10+
11+
logger = logging.getLogger()
12+
13+
14+
@receiver(post_delete, sender=Download)
15+
def after_download_delete(sender, instance, **kwargs):
16+
if instance.filename:
17+
try:
18+
full_path = os.path.join(settings.CSV_FILES_PATH, instance.filename)
19+
os.remove(full_path)
20+
21+
converted_path = output_path(
22+
settings.CSV_FILES_PATH, instance.filename)[0]
23+
if os.path.exists(converted_path):
24+
os.remove(converted_path)
25+
26+
except Exception as e:
27+
logger.error(f"Error deleting file {instance.filename}: {e}")

backend/download/tests/test_download_views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ def test_unauthenticated_download(db, client, basic_mock_corpus, basic_corpus_pu
243243
content_type='application/json'
244244
)
245245
assert status.is_success(response.status_code)
246+
# check that download object is removed
246247
download_objects = Download.objects.all()
247-
assert download_objects.count() == 1
248-
assert download_objects.first().user == None
248+
assert download_objects.count() == 0
249249

250250
def test_query_text_in_csv(db, client, basic_mock_corpus, basic_corpus_public, index_basic_mock_corpus):
251251
es_query = query.set_query_text(mock_match_all_query(), 'ghost')

backend/download/views.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,20 @@
1919

2020
logger = logging.getLogger()
2121

22-
def send_csv_file(download, directory, encoding, format=None):
22+
23+
def send_csv_file(download, directory, encoding, format=None, delete_after_sent=False):
2324
'''
2425
Perform final formatting and send a CSV file as a FileResponse
2526
'''
26-
converted_filename = convert_csv.convert_csv(
27-
directory, download.filename, download.download_type, encoding, format)
28-
path = os.path.join(directory, converted_filename)
27+
try:
28+
converted_filename = convert_csv.convert_csv(
29+
directory, download.filename, download.download_type, encoding, format)
30+
path = os.path.join(directory, converted_filename)
31+
return FileResponse(open(path, 'rb'), filename=download.descriptive_filename(), as_attachment=True)
32+
finally:
33+
if delete_after_sent:
34+
download.delete()
2935

30-
return FileResponse(open(path, 'rb'), filename=download.descriptive_filename(), as_attachment=True)
3136

3237
class ResultsDownloadView(APIView):
3338
'''
@@ -49,7 +54,8 @@ def post(self, request, *args, **kwargs):
4954
directory, filename = os.path.split(csv_path)
5055
# Create download for download history
5156
download.complete(filename=filename)
52-
return send_csv_file(download, directory, request.data['encoding'])
57+
delete_after_sent = user is None
58+
return send_csv_file(download, directory, request.data['encoding'], delete_after_sent=delete_after_sent)
5359
except Exception as e:
5460
logger.error(e)
5561
raise APIException(

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "i-analyzer",
3-
"version": "5.18.0",
3+
"version": "5.18.1",
44
"license": "MIT",
55
"scripts": {
66
"postinstall": "yarn install-back && yarn install-front",

0 commit comments

Comments
 (0)