|
|
|
@ -68,6 +68,13 @@ class TAG_CATEGORY(enum.Enum):
|
|
|
|
|
# visible = 0
|
|
|
|
|
# deleted = 2
|
|
|
|
|
|
|
|
|
|
class IMAGE_STORE(enum.Enum):
|
|
|
|
|
image = 'img'
|
|
|
|
|
jpeg = 'jpeg'
|
|
|
|
|
sample = 'sample'
|
|
|
|
|
thumb_big = 'thumb_big'
|
|
|
|
|
thumb_smol = 'thumb'
|
|
|
|
|
|
|
|
|
|
class TimestampMixin(object):
|
|
|
|
|
created = db.Column(UtcDateTime, nullable=False, server_default=utcnow())
|
|
|
|
|
updated = db.Column(UtcDateTime, nullable=False, onupdate=utcnow(), server_default=utcnow())
|
|
|
|
@ -158,10 +165,6 @@ def admin_required(func):
|
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
|
return dec_view
|
|
|
|
|
|
|
|
|
|
# class UserPreferences(db.Model):
|
|
|
|
|
# user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
|
|
|
|
|
# user = db.relationship('User', backref=db.backref('profile', uselist=False))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
post_tags = db.Table('post_tags', db.metadata,
|
|
|
|
|
db.Column('post_id', db.Integer, db.ForeignKey('post.id')),
|
|
|
|
@ -187,21 +190,14 @@ class Post(TimestampMixin, db.Model):
|
|
|
|
|
approver_id = db.Column(db.Integer, db.ForeignKey('user.id'))
|
|
|
|
|
approver = db.relationship('User', backref=db.backref('approved_posts', lazy=True), foreign_keys=[approver_id])
|
|
|
|
|
|
|
|
|
|
#tags = db.relationship('Tag', secondary=post_tags, back_populates='posts')
|
|
|
|
|
tags = db.relationship('Tag', secondary=post_tags, backref=db.backref('posts'))
|
|
|
|
|
|
|
|
|
|
# parent_id = db.Column(db.Integer, db.ForeignKey('post.id'))
|
|
|
|
|
# parent = db.relationship('Post', backref=db.backref('children', lazy=True), foreign_keys=[parent_id])
|
|
|
|
|
|
|
|
|
|
# def __init__(self, **kwargs):
|
|
|
|
|
# super().__init__(**kwargs)
|
|
|
|
|
|
|
|
|
|
# # self.md5 = hashlib.md5(file.data.getbuffer()).hexdigest()
|
|
|
|
|
# # self.filetype = FILETYPE[file.mimetype.split('/')[1]]
|
|
|
|
|
|
|
|
|
|
# # with Image.open(file.data) as im:
|
|
|
|
|
# # self.width, self.height = im.width, im.height
|
|
|
|
|
# # self.filesize = file.data.getbuffer().nbytes
|
|
|
|
|
|
|
|
|
|
# # self.origin_filename = file.filename
|
|
|
|
|
|
|
|
|
|
# self.generate_image_files()
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
@ -251,31 +247,31 @@ class Post(TimestampMixin, db.Model):
|
|
|
|
|
@property
|
|
|
|
|
def image_files(self):
|
|
|
|
|
return dict(
|
|
|
|
|
# image=os.path.join(current_app.config.get('POST_UPLOADS'), 'img', "{}.{}".format(self.md5, 'jpg' if self.filetype is FILETYPE.jpeg else 'png')),
|
|
|
|
|
image=os.path.join(current_app.config.get('POST_UPLOADS'), 'img', "{}.{}".format(self.md5, self.filetype.file_ext)),
|
|
|
|
|
jpeg=(None if self.filetype.is_jpeg else os.path.join(current_app.config.get('POST_UPLOADS'), 'jpeg', "{}.{}".format(self.md5, 'jpg'))),
|
|
|
|
|
sample=os.path.join(current_app.config.get('POST_UPLOADS'), 'sample', "{}.{}".format(self.md5, 'jpg')),
|
|
|
|
|
thumb_big=os.path.join(current_app.config.get('POST_UPLOADS'), 'thumb_big', "{}.{}".format(self.md5, 'jpg')),
|
|
|
|
|
thumb_smol=os.path.join(current_app.config.get('POST_UPLOADS'), 'thumb', "{}.{}".format(self.md5, 'jpg'))
|
|
|
|
|
image=os.path.join(current_app.config.get('POST_UPLOADS'), IMAGE_STORE.image.value, f"{path}.{self.filetype.file_ext}"),
|
|
|
|
|
jpeg=(os.path.join(current_app.config.get('POST_UPLOADS'), IMAGE_STORE.jpeg.value, f"{self.md5}.jpg") if not self.filetype.is_jpeg else None),
|
|
|
|
|
sample=os.path.join(current_app.config.get('POST_UPLOADS'), IMAGE_STORE.sample.value, f"{self.md5}.jpg"),
|
|
|
|
|
thumb_big=os.path.join(current_app.config.get('POST_UPLOADS'), IMAGE_STORE.thumb_big.value, f"{self.md5}.jpg"),
|
|
|
|
|
thumb_smol=os.path.join(current_app.config.get('POST_UPLOADS'), IMAGE_STORE.thumb_smol.value, f"{self.md5}.jpg")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def url(self, path, endpoint='img'):
|
|
|
|
|
if endpoint == 'img':
|
|
|
|
|
def url(self, path, endpoint=IMAGE_STORE.image):
|
|
|
|
|
store = {s.name:s for s in IMAGE_STORE}.get(endpoint, IMAGE_STORE.image) if not isinstance(endpoint, IMAGE_STORE) else endpoint
|
|
|
|
|
if store is IMAGE_STORE.image:
|
|
|
|
|
return url_for(
|
|
|
|
|
'main.uploaded_img',
|
|
|
|
|
path="{}.{}".format(path, self.filetype.file_ext)
|
|
|
|
|
path=f"{path}.{self.filetype.file_ext}"
|
|
|
|
|
)
|
|
|
|
|
elif endpoint == 'jpeg':
|
|
|
|
|
elif store is IMAGE_STORE.jpeg:
|
|
|
|
|
return url_for(
|
|
|
|
|
'main.uploaded_jpeg' if not self.filetype.is_jpeg else 'main.uploaded_img',
|
|
|
|
|
path="{}.{}".format(path,'jpg')
|
|
|
|
|
path=f"{path}.jpg"
|
|
|
|
|
)
|
|
|
|
|
elif endpoint == 'sample':
|
|
|
|
|
return url_for('main.uploaded_sample', path="{}.{}".format(path,'jpg'))
|
|
|
|
|
elif endpoint == 'thumb_big':
|
|
|
|
|
return url_for('main.uploaded_big_thumb', path="{}.{}".format(path,'jpg'))
|
|
|
|
|
elif endpoint == 'thumb':
|
|
|
|
|
return url_for('main.uploaded_smol_thumb', path="{}.{}".format(path,'jpg'))
|
|
|
|
|
elif store is IMAGE_STORE.sample:
|
|
|
|
|
return url_for('main.uploaded_sample', path=f"{path}.jpg")
|
|
|
|
|
elif store is IMAGE_STORE.thumb_big:
|
|
|
|
|
return url_for('main.uploaded_big_thumb', path=f"{path}.jpg")
|
|
|
|
|
elif store is IMAGE_STORE.thumb_smol:
|
|
|
|
|
return url_for('main.uploaded_smol_thumb', path=f"{path}.jpg")
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def file_uri(self):
|
|
|
|
@ -347,5 +343,3 @@ class Comment(TimestampMixin, db.Model):
|
|
|
|
|
author = current_user == self.user
|
|
|
|
|
moderator = current_user.is_moderator
|
|
|
|
|
return author or moderator
|
|
|
|
|
|
|
|
|
|
# THINK ABOUT LAZY LOADING
|
|
|
|
|