corpus page: Only show compile buttons if corpus is unprepared AND has files.

This commit is contained in:
Patrick Jentsch 2020-04-15 14:30:41 +02:00
parent b251f9993d
commit b1d819444e
5 changed files with 50 additions and 20 deletions

View File

@ -1,4 +1,4 @@
from flask import (abort, current_app, flash, redirect, request,
from flask import (abort, current_app, flash, make_response, redirect, request,
render_template, url_for, send_from_directory)
from flask_login import current_user, login_required
from threading import Thread
@ -99,8 +99,8 @@ def add_corpus_file(corpus_id):
for corpus_file in corpus.files:
if filename == corpus_file.filename:
flash('File already registered to this corpus.')
return redirect(url_for('corpora.add_corpus_file',
corpus_id=corpus_id))
return make_response({'redirect_url': url_for('corpora.corpus', corpus_id=corpus.id)}, 500)
# return redirect(url_for('corpora.add_corpus_file', corpus_id=corpus_id))
# Save the file
dir = os.path.join(str(corpus.user_id), 'corpora', str(corpus.id))
file.save(os.path.join(current_app.config['NOPAQUE_STORAGE'],
@ -125,7 +125,10 @@ def add_corpus_file(corpus_id):
corpus_file.id))
thread.start()
flash('Corpus file added!')
return redirect(url_for('corpora.corpus', corpus_id=corpus_id))
return make_response(
{'redirect_url': url_for('corpora.corpus', corpus_id=corpus.id)},
201)
# return redirect(url_for('corpora.corpus', corpus_id=corpus_id))
return render_template('corpora/add_corpus_file.html.j2',
add_corpus_file_form=add_corpus_file_form,
corpus=corpus, title='Add corpus file')

View File

@ -426,6 +426,24 @@ class CorpusFile(db.Model):
self.corpus.status = 'unprepared'
db.session.commit()
def to_dict(self):
return {'id': self.id,
'address': self.address,
'author': self.author,
'booktitle': self.booktitle,
'chapter': self.chapter,
'dir': self.dir,
'editor': self.editor,
'filename': self.filename,
'institution': self.institution,
'journal': self.journal,
'pages': self.pages,
'publisher': self.publisher,
'publishing_year': self.publishing_year,
'school': self.school,
'title': self.title,
'corpus_id': self.corpus_id}
class Corpus(db.Model):
"""
@ -455,6 +473,7 @@ class Corpus(db.Model):
return {'id': self.id,
'creation_date': self.creation_date.timestamp(),
'description': self.description,
'files': [file.to_dict() for file in self.files],
'status': self.status,
'title': self.title,
'user_id': self.user_id}

View File

@ -8,7 +8,7 @@
</div>
<div class="col s12 m8">
<form method="POST" enctype="multipart/form-data">
<form class="nopaque-job-form" data-progress-modal="progress-modal">
{{ add_corpus_file_form.hidden_tag() }}
<div class="card">
<div class="card-content">
@ -103,4 +103,16 @@
</ul>
</form>
</div>
<div id="progress-modal" class="modal">
<div class="modal-content">
<h4><i class="material-icons prefix">file_upload</i> Uploading files for <span class="title"></span></h4>
<div class="progress">
<div class="determinate" style="width: 0%"></div>
</div>
</div>
<div class="modal-footer">
<a href="#!" class="modal-close waves-effect waves-light btn red abort-request">Cancel</a>
</div>
</div>
{% endblock %}

View File

@ -35,9 +35,7 @@
</div>
<div class="card-action right-align">
<a href="{{ url_for('corpora.analyse_corpus', corpus_id=corpus.id) }}" class="btn disabled hide waves-effect waves-light" id="analyze"><i class="material-icons left">search</i>Analyze</a>
{% if corpus.files[0] is defined %}
<a href="{{ url_for('corpora.prepare_corpus', corpus_id=corpus.id) }}" class="btn disabled hide waves-effect waves-light" id="compile"><i class="material-icons left">merge_type</i>Compile</a>
{% endif %}
<a data-target="delete-corpus-modal" class="btn modal-trigger red waves-effect waves-light"><i class="material-icons left">delete</i>Delete</a>
</div>
</div>
@ -121,6 +119,7 @@
constructor(corpusId, foreignCorpusFlag) {
this.corpusId = corpusId;
this.foreignCorpusFlag = foreignCorpusFlag;
if (this.foreignCorpusFlag) {
nopaque.foreignCorporaSubscribers.push(this);
} else {
@ -131,14 +130,11 @@
_init() {
let corpus;
if (this.foreignCorpusFlag) {
corpus = nopaque.foreignCorpora[this.corpusId];
} else {
corpus = nopaque.corpora[this.corpusId];
}
corpus = (this.foreignCorpusFlag ? nopaque.foreignCorpora[this.corpusId]
: nopaque.corpora[this.corpusId]);
// Status
this.setStatus(corpus.status);
this.setStatus(corpus.status, corpus.files.length);
}
_update(patch) {
@ -167,7 +163,9 @@
}
setStatus(status) {
let analyzeElement, compileElement, progressIndicatorElement, statusElement;
let analyzeElement, compileElement, numFiles, progressIndicatorElement, statusElement;
numFiles = (this.foreignCorpusFlag ? nopaque.foreignCorpora[this.corpusId] : nopaque.corpora[this.corpusId]).files.length;
progressIndicatorElement = document.getElementById("progress-indicator");
if (["queued", "running", "start analysis", "stop analysis"].includes(status)) {
@ -188,7 +186,7 @@
}
compileElement = document.getElementById("compile");
if (status === 'unprepared') {
if (status === "unprepared" && numFiles > 0) {
compileElement.classList.remove("disabled", "hide");
} else {
compileElement.classList.add("disabled", "hide");

View File

@ -146,6 +146,7 @@
constructor(jobId, foreignJobFlag) {
this.jobId = jobId;
this.foreignJobFlag = foreignJobFlag;
if (this.foreignJobFlag) {
nopaque.foreignJobsSubscribers.push(this);
} else {
@ -156,11 +157,8 @@
_init() {
let job;
if (this.foreignJobFlag) {
job = nopaque.foreignJobs[this.jobId];
} else {
job = nopaque.jobs[this.jobId];
}
job = (this.foreignJobFlag ? nopaque.foreignJobs[this.jobId]
: nopaque.jobs[this.jobId]);
// End date
this.setEndDate(job.end_date);