Add back corpus export.

This commit is contained in:
Patrick Jentsch 2021-01-13 15:23:04 +01:00
parent 0f263df9b2
commit a52db602d9
4 changed files with 29 additions and 19 deletions

View File

@ -24,27 +24,21 @@ corpus_analysis_sessions = {}
corpus_analysis_clients = {} corpus_analysis_clients = {}
@socketio.on('corpus_create_zip') @socketio.on('export_corpus')
@socketio_login_required @socketio_login_required
def corpus_create_zip(corpus_id): def export_corpus(corpus_id):
# TODO: This should not be get_or_404 here - Socket.IO != HTTP request
corpus = Corpus.query.get_or_404(corpus_id) corpus = Corpus.query.get_or_404(corpus_id)
# delete old corpus archive if it exists/has been build before # delete old corpus archive if it exists/has been build before
if corpus.archive_file is not None: if corpus.archive_file is not None and os.path.isfile(corpus.archive_file):
if (os.path.isfile(corpus.archive_file)):
os.remove(corpus.archive_file) os.remove(corpus.archive_file)
root_dir = os.path.join(current_app.config['DATA_DIR'],
str(current_user.id),
'corpora')
base_dir = os.path.join(root_dir, str(corpus.id))
zip_name = corpus.title zip_name = corpus.title
zip_path = os.path.join(root_dir, zip_name) zip_path = os.path.join(current_user.path, 'corpora', zip_name)
corpus.archive_file = os.path.join(base_dir, zip_name) + '.zip' corpus.archive_file = os.path.join(corpus.path, zip_name) + '.zip'
db.session.commit() db.session.commit()
shutil.make_archive(zip_path, shutil.make_archive(zip_path, 'zip', corpus.path)
'zip',
base_dir)
shutil.move(zip_path + '.zip', corpus.archive_file) shutil.move(zip_path + '.zip', corpus.archive_file)
socketio.emit('corpus_zip_created', room=request.sid) socketio.emit('export_corpus_' + str(corpus.id), room=request.sid)
@socketio.on('corpus_analysis_init') @socketio.on('corpus_analysis_init')

View File

@ -123,9 +123,9 @@ def corpus(corpus_id):
corpus_files=corpus_files, title='Corpus') corpus_files=corpus_files, title='Corpus')
@corpora.route('/<int:corpus_id>/export') @corpora.route('/<int:corpus_id>/download')
@login_required @login_required
def export_corpus(corpus_id): def download_corpus(corpus_id):
corpus = Corpus.query.get_or_404(corpus_id) corpus = Corpus.query.get_or_404(corpus_id)
if not (corpus.creator == current_user or current_user.is_administrator()): if not (corpus.creator == current_user or current_user.is_administrator()):
abort(403) abort(403)

View File

@ -7,6 +7,8 @@ class CorpusDisplay extends RessourceDisplay {
init(corpus) { init(corpus) {
this.corpus = corpus; this.corpus = corpus;
for (let exportCorpusTriggerElement of this.displayElement.querySelectorAll('.export-corpus-trigger')) {exportCorpusTriggerElement.addEventListener('click', () => this.requestCorpusExport());}
nopaque.appClient.socket.on(`export_corpus_${this.corpus.id}`, () => this.downloadCorpus());
this.setCreationDate(this.corpus.creation_date); this.setCreationDate(this.corpus.creation_date);
this.setDescription(this.corpus.description); this.setDescription(this.corpus.description);
this.setLastEditedDate(this.corpus.last_edited_date); this.setLastEditedDate(this.corpus.last_edited_date);
@ -33,6 +35,21 @@ class CorpusDisplay extends RessourceDisplay {
} }
} }
requestCorpusExport() {
nopaque.appClient.socket.emit('export_corpus', this.corpus.id);
nopaque.flash('Preparing your corpus export...', 'corpus');
for (let exportCorpusTriggerElement of this.displayElement.querySelectorAll('.export-corpus-trigger')) {exportCorpusTriggerElement.classList.toggle('disabled', true);}
}
downloadCorpus() {
nopaque.flash('Corpus export is done. Your corpus download is ready!', 'corpus');
for (let exportCorpusTriggerElement of this.displayElement.querySelectorAll('.export-corpus-trigger')) {exportCorpusTriggerElement.classList.toggle('disabled', false);}
// Little trick to call the download view after ziping has finished
let fakeBtn = document.createElement('a');
fakeBtn.href = `/corpora/${this.corpus.id}/download`;
fakeBtn.click();
}
setTitle(title) { setTitle(title) {
for (let element of this.displayElement.querySelectorAll('.corpus-title')) {this.setElement(element, title);} for (let element of this.displayElement.querySelectorAll('.corpus-title')) {this.setElement(element, title);}
} }

View File

@ -72,7 +72,7 @@
<div class="card-action right-align"> <div class="card-action right-align">
<a class="analyse-corpus-trigger btn disabled waves-effect waves-light" href="{{ url_for('corpora.analyse_corpus', corpus_id=corpus.id) }}"><i class="material-icons left">search</i>Analyze</a> <a class="analyse-corpus-trigger btn disabled waves-effect waves-light" href="{{ url_for('corpora.analyse_corpus', corpus_id=corpus.id) }}"><i class="material-icons left">search</i>Analyze</a>
<a class="btn build-corpus-trigger disabled waves-effect waves-light" href="{{ url_for('corpora.prepare_corpus', corpus_id=corpus.id) }}"><i class="material-icons left">build</i>Build</a> <a class="btn build-corpus-trigger disabled waves-effect waves-light" href="{{ url_for('corpora.prepare_corpus', corpus_id=corpus.id) }}"><i class="material-icons left">build</i>Build</a>
<a class="btn hide waves-effect waves-light" id="corpus-export"><i class="material-icons left">import_export</i>Export Corpus</a> <a class="btn export-corpus-trigger waves-effect waves-light"><i class="material-icons left">import_export</i>Export</a>
<a class="btn modal-trigger red waves-effect waves-light" data-target="delete-corpus-modal"><i class="material-icons left">delete</i>Delete</a> <a class="btn modal-trigger red waves-effect waves-light" data-target="delete-corpus-modal"><i class="material-icons left">delete</i>Delete</a>
</div> </div>
</div> </div>
@ -127,6 +127,5 @@
nopaque.appClient.loadUser({{ corpus.creator.id }}); nopaque.appClient.loadUser({{ corpus.creator.id }});
let corpusDisplay = new CorpusDisplay(document.querySelector('#corpus-display')); let corpusDisplay = new CorpusDisplay(document.querySelector('#corpus-display'));
let corpusFileList = new CorpusFileList(document.querySelector('#corpus-files')); let corpusFileList = new CorpusFileList(document.querySelector('#corpus-files'));
//let jobInputList = new CorpusFileList(document.querySelector('#job-inputs'));
</script> </script>
{% endblock scripts %} {% endblock scripts %}