1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
from sqlalchemy import create_engine, Table, Column, String, Integer, DateTime
import sqlalchemy.sql.functions as func
from sqlalchemy_utc import UtcDateTime, utcnow
from wtforms_alchemy import ModelForm
from app.sql.common import db, Base, Session
from app.utils.file_utils import sha256_tree
from app.settings import app_cfg
from os.path import join
class Upload(Base):
"""Table for storing references to various media"""
__tablename__ = 'upload'
id = Column(Integer, primary_key=True)
episode_id = Column(Integer)
sha256 = Column(String(256), nullable=False)
fn = Column(String(256), nullable=False)
ext = Column(String(4, convert_unicode=True), nullable=False)
tag = Column(String(64, convert_unicode=True), nullable=True)
username = Column(String(16, convert_unicode=True), nullable=False)
created_at = Column(UtcDateTime(), default=utcnow())
def toJSON(self):
return {
'id': self.id,
'episode_id': self.episode_id,
'sha256': self.sha256,
'fn': self.fn,
'ext': self.ext,
'tag': self.tag,
'username': self.username,
'url': self.url(),
'created_at': self.created_at,
}
# def filename(self):
# return "{}{}".format(self.fn)
# def filepath(self):
# return join(app_cfg.DIR_UPLOADS, sha256_tree(self.sha256))
# def fullpath(self):
# return join(self.filepath(), self.filename())
def url(self):
if self.tag:
return join('/static/data_store/uploads', str(self.episode_id), self.tag, self.fn)
return join('/static/data_store/uploads', str(self.episode_id), self.fn)
# return join(app_cfg.URL_UPLOADS, sha256_tree(self.sha256), self.filename())
|