From 7fb37dcb702ce8b55132b431603c4ccaff13edcc Mon Sep 17 00:00:00 2001
From: Patrick Jentsch
Date: Wed, 30 Oct 2019 08:28:52 +0100
Subject: [PATCH] restructure corpus delete method
---
app/main/views.py | 46 +++++++++++++---------------------------------
app/models.py | 35 +++++++++++++++++++++++++----------
app/utils.py | 5 +----
3 files changed, 39 insertions(+), 47 deletions(-)
diff --git a/app/main/views.py b/app/main/views.py
index 362332bc..a61836fe 100644
--- a/app/main/views.py
+++ b/app/main/views.py
@@ -57,6 +57,18 @@ def corpus(corpus_id):
title='Corpus')
+@main.route('/corpora//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//download')
@login_required
def corpus_download(corpus_id):
@@ -100,27 +112,7 @@ def dashboard():
title=create_corpus_form.title.data)
db.session.add(corpus)
db.session.commit()
- dir = os.path.join(current_app.config['OPAQUE_STORAGE_DIRECTORY'],
- 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!')
+ flash('Corpus created!')
return redirect(url_for('main.dashboard'))
return render_template('main/dashboard.html.j2',
create_corpus_form=create_corpus_form,
@@ -169,15 +161,3 @@ def delete_job(job_id):
delete_thread.start()
flash('Job has been deleted!')
return redirect(url_for('main.dashboard'))
-
-
-@main.route('/corpora//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'))
diff --git a/app/models.py b/app/models.py
index 9f416c64..da5275d9 100644
--- a/app/models.py
+++ b/app/models.py
@@ -410,6 +410,19 @@ class CorpusFile(db.Model):
title = db.Column(db.String(64))
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):
"""
@@ -444,17 +457,19 @@ class Corpus(db.Model):
'title': self.title,
'user_id': self.user_id}
- def delete_corpus(self):
+ def delete(self):
logger = logging.getLogger(__name__)
- delete_path = os.path.join('/mnt/opaque/', str(self.user_id), 'corpora',
- str(self.id))
- logger.warning('Delete path is: {}'.format(delete_path))
- while os.path.exists(delete_path):
- try:
- shutil.rmtree(delete_path, ignore_errors=True)
- logger.warning('Path does still exist.')
- except OSError:
- pass
+ for corpus_file in self.files:
+ corpus_file.delete()
+ path = os.path.join(current_app.config['OPAQUE_STORAGE_DIRECTORY'],
+ self.user_id,
+ 'corpora',
+ self.id)
+ try:
+ shutil.rmtree(path)
+ except:
+ logger.warning('[ERROR] Corpus.delete')
+ return
db.session.delete(self)
db.session.commit()
diff --git a/app/utils.py b/app/utils.py
index 2abecee2..7c1d9638 100644
--- a/app/utils.py
+++ b/app/utils.py
@@ -63,7 +63,4 @@ def background_delete_corpus(app, corpus_id):
logger.warning('Called by delete_thread.')
logger.warning('Corpus id is: {}.'.format(corpus_id))
corpus = Corpus.query.filter_by(id=corpus_id).first()
- for corpus_file in corpus.files:
- db.session.delete(corpus_file)
- logger.warning('Corpus object is: {}'.format(corpus))
- corpus.delete_corpus()
+ corpus.delete()