2024-03-05 15:02:23 +00:00
|
|
|
from flask import url_for
|
|
|
|
from flask_hashids import HashidMixin
|
2024-03-07 14:49:04 +00:00
|
|
|
from pathlib import Path
|
2024-03-05 15:02:23 +00:00
|
|
|
from app import db
|
|
|
|
from .file_mixin import FileMixin
|
|
|
|
|
|
|
|
|
|
|
|
class JobInput(FileMixin, HashidMixin, db.Model):
|
|
|
|
__tablename__ = 'job_inputs'
|
|
|
|
# Primary key
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
# Foreign keys
|
|
|
|
job_id = db.Column(db.Integer, db.ForeignKey('jobs.id'))
|
|
|
|
# Relationships
|
|
|
|
job = db.relationship(
|
|
|
|
'Job',
|
|
|
|
back_populates='inputs'
|
|
|
|
)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f'<JobInput {self.filename}>'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def content_url(self):
|
|
|
|
return url_for(
|
|
|
|
'jobs.download_job_input',
|
|
|
|
job_id=self.job.id,
|
|
|
|
job_input_id=self.id
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def jsonpatch_path(self):
|
|
|
|
return f'{self.job.jsonpatch_path}/inputs/{self.hashid}'
|
|
|
|
|
|
|
|
@property
|
2024-03-07 14:49:04 +00:00
|
|
|
def path(self) -> Path:
|
|
|
|
return self.job.path / 'inputs' / f'{self.id}'
|
2024-03-05 15:02:23 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def url(self):
|
|
|
|
return url_for(
|
|
|
|
'jobs.job',
|
|
|
|
job_id=self.job_id,
|
|
|
|
_anchor=f'job-{self.job.hashid}-input-{self.hashid}'
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def user_hashid(self):
|
|
|
|
return self.job.user.hashid
|
|
|
|
|
|
|
|
@property
|
|
|
|
def user_id(self):
|
|
|
|
return self.job.user.id
|
|
|
|
|
|
|
|
def to_json_serializeable(self, backrefs=False, relationships=False):
|
|
|
|
json_serializeable = {
|
|
|
|
'id': self.hashid,
|
|
|
|
**self.file_mixin_to_json_serializeable()
|
|
|
|
}
|
|
|
|
if backrefs:
|
|
|
|
json_serializeable['job'] = \
|
|
|
|
self.job.to_json_serializeable(backrefs=True)
|
|
|
|
if relationships:
|
|
|
|
pass
|
|
|
|
return json_serializeable
|