Skip to content
Snippets Groups Projects
Commit 1be8a449 authored by Patrick Jentsch's avatar Patrick Jentsch
Browse files

cleanup in models file

parent 28801496
No related branches found
No related tags found
No related merge requests found
......@@ -205,6 +205,8 @@ class Role(HashidMixin, db.Model):
if self.has_permission(x.value)
]
}
if backrefs:
pass
if relationships:
json_serializeable['users'] = {
x.hashid: x.to_json_serializeable(relationships=True)
......@@ -256,6 +258,27 @@ class Token(db.Model):
self.access_expiration = datetime.utcnow()
self.refresh_expiration = datetime.utcnow()
def to_json_serializeable(self, backrefs=False, relationships=False):
json_serializeable = {
'id': self.hashid,
'access_token': self.access_token,
'access_expiration': (
None if self.access_expiration is None
else f'{self.access_expiration.isoformat()}Z'
),
'refresh_token': self.refresh_token,
'refresh_expiration': (
None if self.refresh_expiration is None
else f'{self.refresh_expiration.isoformat()}Z'
)
}
if backrefs:
json_serializeable['user'] = \
self.user.to_json_serializeable(backrefs=True)
if relationships:
pass
return json_serializeable
@staticmethod
def clean():
"""Remove any tokens that have been expired for more than a day."""
......@@ -288,6 +311,11 @@ class Avatar(HashidMixin, FileMixin, db.Model):
'id': self.hashid,
**self.file_mixin_to_json_serializeable()
}
if backrefs:
json_serializeable['user'] = \
self.user.to_json_serializeable(backrefs=True)
if relationships:
pass
return json_serializeable
......@@ -328,6 +356,13 @@ class CorpusFollowerAssociation(HashidMixin, db.Model):
'corpus': self.corpus.to_json_serializeable(),
'follower': self.follower.to_json_serializeable()
}
if backrefs:
json_serializeable['corpus'] = \
self.corpus.to_json_serializeable(backrefs=True)
json_serializeable['follower'] = \
self.follower.to_json_serializeable(backrefs=True)
if relationships:
pass
return json_serializeable
......@@ -620,7 +655,6 @@ class User(HashidMixin, UserMixin, db.Model):
def is_following_corpus(self, corpus):
return corpus in self.followed_corpora
def to_json_serializeable(self, backrefs=False, relationships=False, filter_by_privacy_settings=False):
json_serializeable = {
'id': self.hashid,
......@@ -628,9 +662,9 @@ class User(HashidMixin, UserMixin, db.Model):
'email': self.email,
'last_seen': (
None if self.last_seen is None
else self.last_seen.strftime('%Y-%m-%d %H:%M')
else f'{self.last_seen.isoformat()}Z'
),
'member_since': self.member_since.strftime('%Y-%m-%d'),
'member_since': f'{self.member_since.isoformat()}Z',
'username': self.username,
'full_name': self.full_name,
'about_me': self.about_me,
......@@ -804,6 +838,8 @@ class TesseractOCRPipelineModel(FileMixin, HashidMixin, db.Model):
if backrefs:
json_serializeable['user'] = \
self.user.to_json_serializeable(backrefs=True)
if relationships:
pass
return json_serializeable
......@@ -930,7 +966,10 @@ class SpaCyNLPPipelineModel(FileMixin, HashidMixin, db.Model):
**self.file_mixin_to_json_serializeable()
}
if backrefs:
json_serializeable['user'] = self.user.to_json_serializeable(backrefs=True)
json_serializeable['user'] = \
self.user.to_json_serializeable(backrefs=True)
if relationships:
pass
return json_serializeable
......@@ -989,6 +1028,8 @@ class JobInput(FileMixin, HashidMixin, db.Model):
if backrefs:
json_serializeable['job'] = \
self.job.to_json_serializeable(backrefs=True)
if relationships:
pass
return json_serializeable
......@@ -1053,6 +1094,8 @@ class JobResult(FileMixin, HashidMixin, db.Model):
if backrefs:
json_serializeable['job'] = \
self.job.to_json_serializeable(backrefs=True)
if relationships:
pass
return json_serializeable
......@@ -1132,7 +1175,6 @@ class Job(HashidMixin, db.Model):
raise e
return job
def delete(self):
''' Delete the job and its inputs and results from the database. '''
if self.status not in [JobStatus.COMPLETED, JobStatus.FAILED]: # noqa
......@@ -1177,8 +1219,7 @@ class Job(HashidMixin, db.Model):
'service_args': self.service_args,
'service_version': self.service_version,
'status': self.status.name,
'title': self.title,
'url': self.url
'title': self.title
}
if backrefs:
json_serializeable['user'] = \
......@@ -1264,9 +1305,9 @@ class CorpusFile(FileMixin, HashidMixin, db.Model):
def to_json_serializeable(self, backrefs=False, relationships=False):
json_serializeable = {
'id': self.hashid,
'url': self.url,
'address': self.address,
'author': self.author,
'description': self.description,
'booktitle': self.booktitle,
'chapter': self.chapter,
'editor': self.editor,
......@@ -1285,6 +1326,8 @@ class CorpusFile(FileMixin, HashidMixin, db.Model):
if backrefs:
json_serializeable['corpus'] = \
self.corpus.to_json_serializeable(backrefs=True)
if relationships:
pass
return json_serializeable
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment