Add URL property to some models

This commit is contained in:
Patrick Jentsch 2020-12-03 15:13:24 +01:00
parent 4c92fdfb6c
commit 29dbdc9f94

View File

@ -1,5 +1,5 @@
from datetime import datetime from datetime import datetime
from flask import current_app from flask import current_app, url_for
from flask_login import UserMixin, AnonymousUserMixin from flask_login import UserMixin, AnonymousUserMixin
from itsdangerous import BadSignature, TimedJSONWebSignatureSerializer from itsdangerous import BadSignature, TimedJSONWebSignatureSerializer
from time import sleep from time import sleep
@ -281,10 +281,20 @@ class JobInput(db.Model):
# Fields # Fields
filename = db.Column(db.String(255)) filename = db.Column(db.String(255))
@property
def download_url(self):
return url_for('job.download_job_input', job_id=self.job_id,
job_input_id=self.id)
@property @property
def path(self): def path(self):
return os.path.join(self.job.path, self.filename) return os.path.join(self.job.path, self.filename)
@property
def url(self):
return url_for('jobs.job', job_id=self.job_id,
_anchor='job-{}-input-{}'.format(self.job_id, self.id))
def __repr__(self): def __repr__(self):
''' '''
String representation of the JobInput. For human readability. String representation of the JobInput. For human readability.
@ -292,7 +302,9 @@ class JobInput(db.Model):
return '<JobInput {}>'.format(self.filename) return '<JobInput {}>'.format(self.filename)
def to_dict(self): def to_dict(self):
return {'id': self.id, return {'download_url': self.download_url,
'url': self.url,
'id': self.id,
'job_id': self.job_id, 'job_id': self.job_id,
'filename': self.filename} 'filename': self.filename}
@ -309,10 +321,20 @@ class JobResult(db.Model):
# Fields # Fields
filename = db.Column(db.String(255)) filename = db.Column(db.String(255))
@property
def download_url(self):
return url_for('job.download_job_result', job_id=self.job_id,
job_result_id=self.id)
@property @property
def path(self): def path(self):
return os.path.join(self.job.path, 'output', self.filename) return os.path.join(self.job.path, 'output', self.filename)
@property
def url(self):
return url_for('jobs.job', job_id=self.job_id,
_anchor='job-{}-result-{}'.format(self.job_id, self.id))
def __repr__(self): def __repr__(self):
''' '''
String representation of the JobResult. For human readability. String representation of the JobResult. For human readability.
@ -320,7 +342,9 @@ class JobResult(db.Model):
return '<JobResult {}>'.format(self.filename) return '<JobResult {}>'.format(self.filename)
def to_dict(self): def to_dict(self):
return {'id': self.id, return {'download_url': self.download_url,
'url': self.url,
'id': self.id,
'job_id': self.job_id, 'job_id': self.job_id,
'filename': self.filename} 'filename': self.filename}
@ -359,6 +383,10 @@ class Job(db.Model):
def path(self): def path(self):
return os.path.join(self.creator.path, 'jobs', str(self.id)) return os.path.join(self.creator.path, 'jobs', str(self.id))
@property
def path(self):
return url_for('job.job', job_id=self.id)
def __repr__(self): def __repr__(self):
''' '''
String representation of the Job. For human readability. String representation of the Job. For human readability.
@ -395,7 +423,8 @@ class Job(db.Model):
self.status = 'submitted' self.status = 'submitted'
def to_dict(self): def to_dict(self):
return {'id': self.id, return {'url': self.url,
'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,
@ -435,10 +464,20 @@ class CorpusFile(db.Model):
school = db.Column(db.String(255)) school = db.Column(db.String(255))
title = db.Column(db.String(255)) title = db.Column(db.String(255))
@property
def download_url(self):
return url_for('corpora.download_corpus_file',
corpus_id=self.corpus_id, corpus_file_id=self.id)
@property @property
def path(self): def path(self):
return os.path.join(self.corpus.path, self.filename) return os.path.join(self.corpus.path, self.filename)
@property
def url(self):
return url_for('corpora.corpus_file', corpus_id=self.corpus_id,
corpus_file_id=self.id)
def delete(self): def delete(self):
try: try:
os.remove(self.path) os.remove(self.path)
@ -449,7 +488,9 @@ class CorpusFile(db.Model):
self.corpus.status = 'unprepared' self.corpus.status = 'unprepared'
def to_dict(self): def to_dict(self):
return {'id': self.id, return {'download_url': self.download_url,
'url': self.url,
'id': self.id,
'corpus_id': self.corpus_id, 'corpus_id': self.corpus_id,
'address': self.address, 'address': self.address,
'author': self.author, 'author': self.author,