Merge branch 'development'

This commit is contained in:
Patrick Jentsch 2022-10-11 11:34:19 +02:00
commit 07ec01ae2e
3 changed files with 25 additions and 65 deletions

View File

@ -149,7 +149,7 @@ def create_corpus_file(corpus_id):
mimetype='application/vrt+xml', mimetype='application/vrt+xml',
corpus=corpus corpus=corpus
) )
except OSError: except (AttributeError, OSError):
abort(500) abort(500)
corpus.status = CorpusStatus.UNPREPARED corpus.status = CorpusStatus.UNPREPARED
db.session.commit() db.session.commit()

View File

@ -91,6 +91,26 @@ class FileMixin:
), ),
'mimetype': self.mimetype 'mimetype': self.mimetype
} }
@classmethod
def create(cls, file_storage, **kwargs):
filename = kwargs.pop('filename', file_storage.filename)
mimetype = kwargs.pop('mimetype', file_storage.mimetype)
obj = cls(
filename=secure_filename(filename),
mimetype=mimetype,
**kwargs
)
db.session.add(obj)
db.session.flush(objects=[obj])
db.session.refresh(obj)
try:
file_storage.save(obj.path)
except (AttributeError, OSError) as e:
current_app.logger.error(e)
db.session.rollback()
raise e
return obj
# endregion mixins # endregion mixins
@ -691,26 +711,6 @@ class JobInput(FileMixin, HashidMixin, db.Model):
def user_id(self): def user_id(self):
return self.job.user_id return self.job.user_id
@staticmethod
def create(input_file, **kwargs):
filename = kwargs.get('filename', input_file.filename)
mimetype = kwargs.get('mimetype', input_file.mimetype)
job_input = JobInput(
filename=secure_filename(filename),
mimetype=mimetype,
**kwargs
)
db.session.add(job_input)
db.session.flush(objects=[job_input])
db.session.refresh(job_input)
try:
input_file.save(job_input.path)
except OSError as e:
current_app.logger.error(e)
db.session.rollback()
raise e
return job_input
def to_json(self, backrefs=False, relationships=False): def to_json(self, backrefs=False, relationships=False):
_json = { _json = {
'id': self.hashid, 'id': self.hashid,
@ -766,26 +766,6 @@ class JobResult(FileMixin, HashidMixin, db.Model):
def user_id(self): def user_id(self):
return self.job.user_id return self.job.user_id
@staticmethod
def create(input_file, **kwargs):
filename = kwargs.get('filename', input_file.filename)
mimetype = kwargs.get('mimetype', input_file.mimetype)
job_result = JobResult(
filename=secure_filename(filename),
mimetype=mimetype,
**kwargs
)
db.session.add(job_result)
db.session.flush(objects=[job_result])
db.session.refresh(job_result)
try:
input_file.save(job_result.path)
except OSError as e:
current_app.logger.error(e)
db.session.rollback()
raise e
return job_result
def to_json(self, backrefs=False, relationships=False): def to_json(self, backrefs=False, relationships=False):
_json = { _json = {
'id': self.hashid, 'id': self.hashid,
@ -1024,26 +1004,6 @@ class CorpusFile(FileMixin, HashidMixin, db.Model):
_json['corpus'] = self.corpus.to_json(backrefs=True) _json['corpus'] = self.corpus.to_json(backrefs=True)
return _json return _json
@staticmethod
def create(input_file, **kwargs):
filename = kwargs.pop('filename', input_file.filename)
mimetype = kwargs.pop('mimetype', input_file.mimetype)
corpus_file = CorpusFile(
filename=secure_filename(filename),
mimetype=mimetype,
**kwargs,
)
db.session.add(corpus_file)
db.session.flush(objects=[corpus_file])
db.session.refresh(corpus_file)
try:
input_file.save(corpus_file.path)
except OSError as e:
current_app.logger.error(e)
db.session.rollback()
raise e
return corpus_file
class Corpus(HashidMixin, db.Model): class Corpus(HashidMixin, db.Model):
''' '''
Class to define a corpus. Class to define a corpus.

View File

@ -45,7 +45,7 @@ def file_setup_pipeline():
for input_file in form.images.data: for input_file in form.images.data:
try: try:
JobInput.create(input_file, job=job) JobInput.create(input_file, job=job)
except OSError: except (AttributeError, OSError):
abort(500) abort(500)
job.status = JobStatus.SUBMITTED job.status = JobStatus.SUBMITTED
db.session.commit() db.session.commit()
@ -88,7 +88,7 @@ def tesseract_ocr_pipeline():
abort(500) abort(500)
try: try:
JobInput.create(form.pdf.data, job=job) JobInput.create(form.pdf.data, job=job)
except OSError: except (AttributeError, OSError):
abort(500) abort(500)
job.status = JobStatus.SUBMITTED job.status = JobStatus.SUBMITTED
db.session.commit() db.session.commit()
@ -138,7 +138,7 @@ def transkribus_htr_pipeline():
abort(500) abort(500)
try: try:
JobInput.create(form.pdf.data, job=job) JobInput.create(form.pdf.data, job=job)
except OSError: except (AttributeError, OSError):
abort(500) abort(500)
job.status = JobStatus.SUBMITTED job.status = JobStatus.SUBMITTED
db.session.commit() db.session.commit()
@ -187,7 +187,7 @@ def spacy_nlp_pipeline():
abort(500) abort(500)
try: try:
JobInput.create(form.txt.data, job=job) JobInput.create(form.txt.data, job=job)
except OSError: except (AttributeError, OSError):
abort(500) abort(500)
job.status = JobStatus.SUBMITTED job.status = JobStatus.SUBMITTED
db.session.commit() db.session.commit()