mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-11-15 01:05:42 +00:00
Add a flag to all db Model to_dict methods: include_relationships
This commit is contained in:
parent
869c1ba632
commit
5662140a8d
@ -151,8 +151,9 @@ class User(UserMixin, db.Model):
|
||||
def password(self, password):
|
||||
self.password_hash = generate_password_hash(password)
|
||||
|
||||
def to_dict(self):
|
||||
return {'id': self.id,
|
||||
def to_dict(self, include_relationships=True):
|
||||
dict_user = {
|
||||
'id': self.id,
|
||||
'role_id': self.role_id,
|
||||
'confirmed': self.confirmed,
|
||||
'email': self.email,
|
||||
@ -164,12 +165,17 @@ class User(UserMixin, db.Model):
|
||||
'job_status_site_notifications':
|
||||
self.setting_job_status_site_notifications},
|
||||
'username': self.username,
|
||||
'corpora': {corpus.id: corpus.to_dict()
|
||||
for corpus in self.corpora},
|
||||
'jobs': {job.id: job.to_dict() for job in self.jobs},
|
||||
'query_results': {query_result.id: query_result.to_dict()
|
||||
for query_result in self.query_results},
|
||||
'role': self.role.to_dict()}
|
||||
'role': self.role.to_dict()
|
||||
}
|
||||
if include_relationships:
|
||||
dict_user['corpora'] = {corpus.id: corpus.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):
|
||||
'''
|
||||
@ -301,7 +307,7 @@ class JobInput(db.Model):
|
||||
'''
|
||||
return '<JobInput {}>'.format(self.filename)
|
||||
|
||||
def to_dict(self):
|
||||
def to_dict(self, include_relationships=True):
|
||||
return {'download_url': self.download_url,
|
||||
'url': self.url,
|
||||
'id': self.id,
|
||||
@ -341,7 +347,7 @@ class JobResult(db.Model):
|
||||
'''
|
||||
return '<JobResult {}>'.format(self.filename)
|
||||
|
||||
def to_dict(self):
|
||||
def to_dict(self, include_relationships=True):
|
||||
return {'download_url': self.download_url,
|
||||
'url': self.url,
|
||||
'id': self.id,
|
||||
@ -420,22 +426,26 @@ class Job(db.Model):
|
||||
self.end_date = None
|
||||
self.status = 'submitted'
|
||||
|
||||
def to_dict(self):
|
||||
return {'url': self.url,
|
||||
def to_dict(self, include_relationships=True):
|
||||
dict_job = {
|
||||
'url': self.url,
|
||||
'id': self.id,
|
||||
'user_id': self.user_id,
|
||||
'creation_date': self.creation_date.timestamp(),
|
||||
'description': self.description,
|
||||
'end_date': (self.end_date.timestamp() if self.end_date else
|
||||
None),
|
||||
'end_date': self.end_date.timestamp() if self.end_date else None,
|
||||
'service': self.service,
|
||||
'service_args': self.service_args,
|
||||
'service_version': self.service_version,
|
||||
'status': self.status,
|
||||
'title': self.title,
|
||||
'inputs': {input.id: input.to_dict() for input in self.inputs},
|
||||
'results': {result.id: result.to_dict()
|
||||
for result in self.results}}
|
||||
}
|
||||
if include_relationships:
|
||||
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):
|
||||
@ -485,7 +495,7 @@ class CorpusFile(db.Model):
|
||||
db.session.delete(self)
|
||||
self.corpus.status = 'unprepared'
|
||||
|
||||
def to_dict(self):
|
||||
def to_dict(self, include_relationships=True):
|
||||
return {'download_url': self.download_url,
|
||||
'url': self.url,
|
||||
'id': self.id,
|
||||
@ -539,8 +549,9 @@ class Corpus(db.Model):
|
||||
def url(self):
|
||||
return url_for('corpora.corpus', corpus_id=self.id)
|
||||
|
||||
def to_dict(self):
|
||||
return {'analysis_url': self.analysis_url,
|
||||
def to_dict(self, include_relationships=True):
|
||||
dict_corpus = {
|
||||
'analysis_url': self.analysis_url,
|
||||
'url': self.url,
|
||||
'id': self.id,
|
||||
'user_id': self.user_id,
|
||||
@ -551,7 +562,11 @@ class Corpus(db.Model):
|
||||
'last_edited_date': self.last_edited_date.timestamp(),
|
||||
'max_nr_of_tokens': self.max_nr_of_tokens,
|
||||
'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):
|
||||
output_dir = os.path.join(self.path, 'merged')
|
||||
@ -628,7 +643,7 @@ class QueryResult(db.Model):
|
||||
shutil.rmtree(self.path, ignore_errors=True)
|
||||
db.session.delete(self)
|
||||
|
||||
def to_dict(self):
|
||||
def to_dict(self, include_relationships=True):
|
||||
return {'download_url': self.download_url,
|
||||
'url': self.url,
|
||||
'id': self.id,
|
||||
|
Loading…
Reference in New Issue
Block a user