nopaque/app/corpora/tasks.py

42 lines
1.2 KiB
Python
Raw Normal View History

from ..decorators import background
from ..models import Corpus, CorpusFile
import os
import shutil
@background
def delete_corpus(app, corpus_id):
with app.app_context():
corpus = Corpus.query.get(corpus_id)
if corpus is None:
return
path = os.path.join(app.config['NOPAQUE_STORAGE'], str(corpus.user_id),
'corpora', str(corpus.id))
shutil.rmtree(path, ignore_errors=True)
corpus.delete()
@background
def delete_corpus_file(app, corpus_file_id):
with app.app_context():
corpus_file = CorpusFile.query.get(corpus_file_id)
if corpus_file is None:
return
path = os.path.join(app.config['NOPAQUE_STORAGE'], corpus_file.dir,
corpus_file.filename)
try:
os.remove(path)
except Exception:
pass
else:
corpus_file.delete()
@background
def edit_corpus_file(app, corpus_file_id):
with app.app_context():
corpus_file = CorpusFile.query.get(corpus_file_id)
if corpus_file is None:
raise Exception('Corpus file {} not found!'.format(corpus_file_id))
corpus_file.insert_metadata()