mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-12-25 02:44:18 +00:00
restructure corpus delete method
This commit is contained in:
parent
7dcc8c48da
commit
7fb37dcb70
@ -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,26 +112,6 @@ 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'],
|
|
||||||
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 redirect(url_for('main.dashboard'))
|
||||||
return render_template('main/dashboard.html.j2',
|
return render_template('main/dashboard.html.j2',
|
||||||
@ -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'))
|
|
||||||
|
@ -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,
|
||||||
|
'corpora',
|
||||||
|
self.id)
|
||||||
try:
|
try:
|
||||||
shutil.rmtree(delete_path, ignore_errors=True)
|
shutil.rmtree(path)
|
||||||
logger.warning('Path does still exist.')
|
except:
|
||||||
except OSError:
|
logger.warning('[ERROR] Corpus.delete')
|
||||||
pass
|
return
|
||||||
db.session.delete(self)
|
db.session.delete(self)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
@ -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()
|
|
||||||
|
Loading…
Reference in New Issue
Block a user