Skip to content
Snippets Groups Projects
Commit 9df82ff2 authored by Erwan Rouchet's avatar Erwan Rouchet Committed by Bastien Abadie
Browse files

Add an SQLite content type to uploaded exports

parent 5180dcda
No related branches found
No related tags found
1 merge request!1379Add an SQLite content type to uploaded exports
......@@ -4,9 +4,11 @@ import os
import sqlite3
import tempfile
import uuid
from datetime import datetime, timezone
from pathlib import Path
from django.db import connections
from django.utils.text import slugify
from django_rq import job
from rq import get_current_job
......@@ -119,7 +121,17 @@ def export_corpus(corpus_export: CorpusExport) -> None:
if rq_job:
rq_job.set_description(f'Uploading export for corpus {corpus_export.corpus.name}')
rq_job.set_progress(0.0)
corpus_export.s3_object.upload_file(db_path, Callback=progress_callback)
# Set the filename in a HTTP header to make downloaded exports nicer
filename = f'{slugify(corpus_export.corpus.name)[:100]}-{datetime.now(timezone.utc).strftime("%Y%m%d-%H%M%S")}.sqlite'
corpus_export.s3_object.upload_file(
db_path,
Callback=progress_callback,
ExtraArgs={
'ContentType': 'application/vnd.sqlite3',
'ContentDisposition': f'attachment; filename="{filename}"'
},
)
corpus_export.state = CorpusExportState.Done
corpus_export.save()
......
import json
import os
import sqlite3
from datetime import datetime, timezone
from unittest.mock import call, patch
from arkindex.dataimport.models import WorkerVersion
......@@ -21,9 +22,12 @@ from arkindex.project.tests import FixtureTestCase
class TestExport(FixtureTestCase):
@patch('arkindex.documents.export.datetime')
@patch('arkindex.documents.export.os.unlink')
@patch('arkindex.project.aws.s3.Object')
def test_export(self, s3_object_mock, unlink_mock):
def test_export(self, s3_object_mock, unlink_mock, datetime_mock):
datetime_mock.now.return_value = datetime(2042, 1, 1, 12, 34, 56, tzinfo=timezone.utc)
element = self.corpus.elements.get(name='Volume 1')
transcription = Transcription.objects.first()
version = WorkerVersion.objects.get(worker__slug='reco')
......@@ -68,8 +72,12 @@ class TestExport(FixtureTestCase):
self.assertEqual(s3_object_mock().upload_file.call_count, 1)
args, kwargs = s3_object_mock().upload_file.call_args
self.assertCountEqual(kwargs.keys(), {'Callback'})
self.assertCountEqual(kwargs.keys(), {'Callback', 'ExtraArgs'})
self.assertTrue(callable(kwargs['Callback']))
self.assertDictEqual(kwargs['ExtraArgs'], {
'ContentType': 'application/vnd.sqlite3',
'ContentDisposition': 'attachment; filename="unit-tests-20420101-123456.sqlite"'
})
self.assertEqual(len(args), 1)
# Retrieve the database path from the S3 upload argument
db_path = args[0]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment