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,25 +151,31 @@ 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 = {
'role_id': self.role_id, 'id': self.id,
'confirmed': self.confirmed, 'role_id': self.role_id,
'email': self.email, 'confirmed': self.confirmed,
'last_seen': self.last_seen.timestamp(), 'email': self.email,
'member_since': self.member_since.timestamp(), 'last_seen': self.last_seen.timestamp(),
'settings': {'dark_mode': self.setting_dark_mode, 'member_since': self.member_since.timestamp(),
'job_status_mail_notifications': 'settings': {'dark_mode': self.setting_dark_mode,
self.setting_job_status_mail_notifications, 'job_status_mail_notifications':
'job_status_site_notifications': self.setting_job_status_mail_notifications,
self.setting_job_status_site_notifications}, 'job_status_site_notifications':
'username': self.username, self.setting_job_status_site_notifications},
'corpora': {corpus.id: corpus.to_dict() 'username': self.username,
for corpus in self.corpora}, 'role': self.role.to_dict()
'jobs': {job.id: job.to_dict() for job in self.jobs}, }
'query_results': {query_result.id: query_result.to_dict() if include_relationships:
for query_result in self.query_results}, dict_user['corpora'] = {corpus.id: corpus.to_dict()
'role': self.role.to_dict()} for corpus in self.corpora}
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 = {
'id': self.id, 'url': self.url,
'user_id': self.user_id, 'id': self.id,
'creation_date': self.creation_date.timestamp(), 'user_id': self.user_id,
'description': self.description, 'creation_date': self.creation_date.timestamp(),
'end_date': (self.end_date.timestamp() if self.end_date else 'description': self.description,
None), 'end_date': self.end_date.timestamp() if self.end_date else 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,19 +549,24 @@ 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 = {
'url': self.url, 'analysis_url': self.analysis_url,
'id': self.id, 'url': self.url,
'user_id': self.user_id, 'id': self.id,
'creation_date': self.creation_date.timestamp(), 'user_id': self.user_id,
'current_nr_of_tokens': self.current_nr_of_tokens, 'creation_date': self.creation_date.timestamp(),
'description': self.description, 'current_nr_of_tokens': self.current_nr_of_tokens,
'status': self.status, 'description': self.description,
'last_edited_date': self.last_edited_date.timestamp(), 'status': self.status,
'max_nr_of_tokens': self.max_nr_of_tokens, 'last_edited_date': self.last_edited_date.timestamp(),
'title': self.title, 'max_nr_of_tokens': self.max_nr_of_tokens,
'files': {file.id: file.to_dict() for file in self.files}} 'title': self.title,
}
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,