Add a flag to all db Model to_dict methods: include_relationships

This commit is contained in:
Patrick Jentsch 2021-08-18 15:09:56 +02:00
parent 869c1ba632
commit 5662140a8d

View File

@ -151,8 +151,9 @@ class User(UserMixin, db.Model):
def password(self, password): def password(self, password):
self.password_hash = generate_password_hash(password) self.password_hash = generate_password_hash(password)
def to_dict(self): def to_dict(self, include_relationships=True):
return {'id': self.id, dict_user = {
'id': self.id,
'role_id': self.role_id, 'role_id': self.role_id,
'confirmed': self.confirmed, 'confirmed': self.confirmed,
'email': self.email, 'email': self.email,
@ -164,12 +165,17 @@ class User(UserMixin, db.Model):
'job_status_site_notifications': 'job_status_site_notifications':
self.setting_job_status_site_notifications}, self.setting_job_status_site_notifications},
'username': self.username, 'username': self.username,
'corpora': {corpus.id: corpus.to_dict() 'role': self.role.to_dict()
for corpus in self.corpora}, }
'jobs': {job.id: job.to_dict() for job in self.jobs}, if include_relationships:
'query_results': {query_result.id: query_result.to_dict() dict_user['corpora'] = {corpus.id: corpus.to_dict()
for query_result in self.query_results}, for corpus in self.corpora}
'role': self.role.to_dict()} dict_user['jobs'] = {job.id: job.to_dict() for job in self.jobs}
dict_user['query_results'] = {
query_result.id: query_result.to_dict()
for query_result in self.query_results
}
return dict_user
def __repr__(self): def __repr__(self):
''' '''
@ -301,7 +307,7 @@ class JobInput(db.Model):
''' '''
return '<JobInput {}>'.format(self.filename) return '<JobInput {}>'.format(self.filename)
def to_dict(self): def to_dict(self, include_relationships=True):
return {'download_url': self.download_url, return {'download_url': self.download_url,
'url': self.url, 'url': self.url,
'id': self.id, 'id': self.id,
@ -341,7 +347,7 @@ class JobResult(db.Model):
''' '''
return '<JobResult {}>'.format(self.filename) return '<JobResult {}>'.format(self.filename)
def to_dict(self): def to_dict(self, include_relationships=True):
return {'download_url': self.download_url, return {'download_url': self.download_url,
'url': self.url, 'url': self.url,
'id': self.id, 'id': self.id,
@ -420,22 +426,26 @@ class Job(db.Model):
self.end_date = None self.end_date = None
self.status = 'submitted' self.status = 'submitted'
def to_dict(self): def to_dict(self, include_relationships=True):
return {'url': self.url, dict_job = {
'url': self.url,
'id': self.id, 'id': self.id,
'user_id': self.user_id, 'user_id': self.user_id,
'creation_date': self.creation_date.timestamp(), 'creation_date': self.creation_date.timestamp(),
'description': self.description, 'description': self.description,
'end_date': (self.end_date.timestamp() if self.end_date else 'end_date': self.end_date.timestamp() if self.end_date else None,
None),
'service': self.service, 'service': self.service,
'service_args': self.service_args, 'service_args': self.service_args,
'service_version': self.service_version, 'service_version': self.service_version,
'status': self.status, 'status': self.status,
'title': self.title, 'title': self.title,
'inputs': {input.id: input.to_dict() for input in self.inputs}, }
'results': {result.id: result.to_dict() if include_relationships:
for result in self.results}} dict_job['inputs'] = {input.id: input.to_dict()
for input in self.inputs}
dict_job['results'] = {result.id: result.to_dict()
for result in self.results}
return dict_job
class CorpusFile(db.Model): class CorpusFile(db.Model):
@ -485,7 +495,7 @@ class CorpusFile(db.Model):
db.session.delete(self) db.session.delete(self)
self.corpus.status = 'unprepared' self.corpus.status = 'unprepared'
def to_dict(self): def to_dict(self, include_relationships=True):
return {'download_url': self.download_url, return {'download_url': self.download_url,
'url': self.url, 'url': self.url,
'id': self.id, 'id': self.id,
@ -539,8 +549,9 @@ class Corpus(db.Model):
def url(self): def url(self):
return url_for('corpora.corpus', corpus_id=self.id) return url_for('corpora.corpus', corpus_id=self.id)
def to_dict(self): def to_dict(self, include_relationships=True):
return {'analysis_url': self.analysis_url, dict_corpus = {
'analysis_url': self.analysis_url,
'url': self.url, 'url': self.url,
'id': self.id, 'id': self.id,
'user_id': self.user_id, 'user_id': self.user_id,
@ -551,7 +562,11 @@ class Corpus(db.Model):
'last_edited_date': self.last_edited_date.timestamp(), 'last_edited_date': self.last_edited_date.timestamp(),
'max_nr_of_tokens': self.max_nr_of_tokens, 'max_nr_of_tokens': self.max_nr_of_tokens,
'title': self.title, 'title': self.title,
'files': {file.id: file.to_dict() for file in self.files}} }
if include_relationships:
dict_corpus['files'] = {file.id: file.to_dict()
for file in self.files}
return dict_corpus
def build(self): def build(self):
output_dir = os.path.join(self.path, 'merged') output_dir = os.path.join(self.path, 'merged')
@ -628,7 +643,7 @@ class QueryResult(db.Model):
shutil.rmtree(self.path, ignore_errors=True) shutil.rmtree(self.path, ignore_errors=True)
db.session.delete(self) db.session.delete(self)
def to_dict(self): def to_dict(self, include_relationships=True):
return {'download_url': self.download_url, return {'download_url': self.download_url,
'url': self.url, 'url': self.url,
'id': self.id, 'id': self.id,