restructure corpus delete method

This commit is contained in:
Patrick Jentsch 2019-10-30 08:28:52 +01:00
parent 7dcc8c48da
commit 7fb37dcb70
3 changed files with 39 additions and 47 deletions

View File

@ -57,6 +57,18 @@ def corpus(corpus_id):
title='Corpus') title='Corpus')
@main.route('/corpora/<int:corpus_id>/delete')
@login_required
def delete_corpus(corpus_id):
delete_thread = threading.Thread(
target=background_delete_corpus,
args=(current_app._get_current_object(), corpus_id)
)
delete_thread.start()
flash('Corpus has been deleted!')
return redirect(url_for('main.dashboard'))
@main.route('/corpora/<int:corpus_id>/download') @main.route('/corpora/<int:corpus_id>/download')
@login_required @login_required
def corpus_download(corpus_id): def corpus_download(corpus_id):
@ -100,27 +112,7 @@ def dashboard():
title=create_corpus_form.title.data) title=create_corpus_form.title.data)
db.session.add(corpus) db.session.add(corpus)
db.session.commit() db.session.commit()
dir = os.path.join(current_app.config['OPAQUE_STORAGE_DIRECTORY'], flash('Corpus created!')
str(corpus.user_id),
'corpora',
str(corpus.id))
try:
os.makedirs(dir)
except OSError:
flash('OSError!')
else:
for file in create_corpus_form.files.data:
filename = secure_filename(file.filename)
file.save(os.path.join(dir, filename))
file_dir = os.path.join(str(corpus.user_id),
'corpora',
str(corpus.id))
corpus_file = CorpusFile(filename=filename,
corpus_id=corpus.id,
dir=file_dir)
db.session.add(corpus_file)
db.session.commit()
flash('Corpus created!')
return redirect(url_for('main.dashboard')) return redirect(url_for('main.dashboard'))
return render_template('main/dashboard.html.j2', return render_template('main/dashboard.html.j2',
create_corpus_form=create_corpus_form, create_corpus_form=create_corpus_form,
@ -169,15 +161,3 @@ def delete_job(job_id):
delete_thread.start() delete_thread.start()
flash('Job has been deleted!') flash('Job has been deleted!')
return redirect(url_for('main.dashboard')) return redirect(url_for('main.dashboard'))
@main.route('/corpora/<int:corpus_id>/delete')
@login_required
def delete_corpus(corpus_id):
delete_thread = threading.Thread(
target=background_delete_corpus,
args=(current_app._get_current_object(), corpus_id)
)
delete_thread.start()
flash('Corpus has been deleted!')
return redirect(url_for('main.dashboard'))

View File

@ -410,6 +410,19 @@ class CorpusFile(db.Model):
title = db.Column(db.String(64)) title = db.Column(db.String(64))
corpus_id = db.Column(db.Integer, db.ForeignKey('corpora.id')) corpus_id = db.Column(db.Integer, db.ForeignKey('corpora.id'))
def delete(self):
logger = logging.getLogger(__name__)
path = os.path.join(current_app.config['OPAQUE_STORAGE_DIRECTORY'],
self.dir,
self.filename)
try:
os.remove(path)
except:
logger.warning('[ERROR] CorpusFile.delete')
return
db.session.delete(self)
db.session.commit()
class Corpus(db.Model): class Corpus(db.Model):
""" """
@ -444,17 +457,19 @@ class Corpus(db.Model):
'title': self.title, 'title': self.title,
'user_id': self.user_id} 'user_id': self.user_id}
def delete_corpus(self): def delete(self):
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
delete_path = os.path.join('/mnt/opaque/', str(self.user_id), 'corpora', for corpus_file in self.files:
str(self.id)) corpus_file.delete()
logger.warning('Delete path is: {}'.format(delete_path)) path = os.path.join(current_app.config['OPAQUE_STORAGE_DIRECTORY'],
while os.path.exists(delete_path): self.user_id,
try: 'corpora',
shutil.rmtree(delete_path, ignore_errors=True) self.id)
logger.warning('Path does still exist.') try:
except OSError: shutil.rmtree(path)
pass except:
logger.warning('[ERROR] Corpus.delete')
return
db.session.delete(self) db.session.delete(self)
db.session.commit() db.session.commit()

View File

@ -63,7 +63,4 @@ def background_delete_corpus(app, corpus_id):
logger.warning('Called by delete_thread.') logger.warning('Called by delete_thread.')
logger.warning('Corpus id is: {}.'.format(corpus_id)) logger.warning('Corpus id is: {}.'.format(corpus_id))
corpus = Corpus.query.filter_by(id=corpus_id).first() corpus = Corpus.query.filter_by(id=corpus_id).first()
for corpus_file in corpus.files: corpus.delete()
db.session.delete(corpus_file)
logger.warning('Corpus object is: {}'.format(corpus))
corpus.delete_corpus()