2022-02-01 12:22:29 -07:00
|
|
|
from sqlalchemy import Integer, Column, String, Computed
|
|
|
|
|
from sqlalchemy.orm import deferred
|
|
|
|
|
|
|
|
|
|
from wrolpi.common import Base, ModelHelper, get_media_directory, tsvector
|
|
|
|
|
from wrolpi.dates import TZDateTime
|
|
|
|
|
from wrolpi.media_path import MediaPathType
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class File(ModelHelper, Base):
|
|
|
|
|
__tablename__ = 'file'
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
2022-02-01 14:11:14 -07:00
|
|
|
path = Column(MediaPathType)
|
2022-02-01 12:22:29 -07:00
|
|
|
|
2022-02-02 08:50:03 -07:00
|
|
|
idempotency = Column(String)
|
2022-02-01 12:22:29 -07:00
|
|
|
mimetype = Column(String)
|
|
|
|
|
modification_datetime = Column(TZDateTime)
|
|
|
|
|
size = Column(Integer)
|
2022-02-01 14:11:14 -07:00
|
|
|
title = deferred(Column(String)) # used by textsearch column
|
2022-02-01 12:22:29 -07:00
|
|
|
|
|
|
|
|
textsearch = deferred(
|
2022-02-01 14:11:14 -07:00
|
|
|
Column(tsvector, Computed('''to_tsvector('english'::regconfig, title)''')))
|
2022-02-01 12:22:29 -07:00
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
return f'<File id={self.id} path={self.path.relative_to(get_media_directory())} mime={self.mimetype}>'
|
|
|
|
|
|
|
|
|
|
def __json__(self):
|
2022-02-01 14:11:14 -07:00
|
|
|
path = self.path.path.relative_to(get_media_directory())
|
2022-02-01 12:22:29 -07:00
|
|
|
d = dict(
|
|
|
|
|
id=self.id,
|
|
|
|
|
mimetype=self.mimetype,
|
2022-02-02 14:02:51 -07:00
|
|
|
modified=self.modification_datetime, # React browser expects this.
|
2022-02-01 14:11:14 -07:00
|
|
|
path=path,
|
2022-02-01 12:22:29 -07:00
|
|
|
size=self.size,
|
2022-02-01 14:11:14 -07:00
|
|
|
key=path,
|
2022-02-01 12:22:29 -07:00
|
|
|
)
|
|
|
|
|
return d
|