import random
import re
import time
from urllib.parse import urlparse
from flask import render_template
from sqlalchemy import *
from sqlalchemy.orm import relationship, deferred
from files.__main__ import Base
from files.helpers.const import *
from files.helpers.regex import *
from files.helpers.lazy import lazy
from .flags import Flag
from .comment import Comment, normalize_urls_runtime
from .saves import SaveRelationship
from .sub import *
from .subscriptions import *
from .votes import CommentVote
from .polls import *
from flask import g
class Submission(Base):
__tablename__ = "submissions"
id = Column(Integer, primary_key=True)
author_id = Column(Integer, ForeignKey("users.id"))
edited_utc = Column(Integer, default=0)
created_utc = Column(Integer)
thumburl = Column(String)
is_banned = Column(Boolean, default=False)
bannedfor = Column(String)
ghost = Column(Boolean, default=False)
views = Column(Integer, default=0)
deleted_utc = Column(Integer, default=0)
distinguish_level = Column(Integer, default=0)
stickied = Column(String)
stickied_utc = Column(Integer)
hole_pinned = Column(String)
sub = Column(String, ForeignKey("subs.name"))
is_pinned = Column(Boolean, default=False)
private = Column(Boolean, default=False)
club = Column(Boolean, default=False)
comment_count = Column(Integer, default=0)
is_approved = Column(Integer, ForeignKey("users.id"))
over_18 = Column(Boolean, default=False)
is_bot = Column(Boolean, default=False)
upvotes = Column(Integer, default=1)
downvotes = Column(Integer, default=0)
realupvotes = Column(Integer, default=1)
app_id=Column(Integer, ForeignKey("oauth_apps.id"))
title = Column(String)
title_html = Column(String)
url = Column(String)
body = Column(String)
body_html = Column(String)
flair = Column(String)
category_id = Column(Integer, ForeignKey("category.id"))
ban_reason = Column(String)
embed_url = Column(String)
new = Column(Boolean)
notify = Column(Boolean)
author = relationship("User", primaryjoin="Submission.author_id==User.id")
oauth_app = relationship("OauthApp")
approved_by = relationship("User", uselist=False, primaryjoin="Submission.is_approved==User.id")
awards = relationship("AwardRelationship", order_by="AwardRelationship.awarded_utc.desc()", back_populates="post")
flags = relationship("Flag", order_by="Flag.created_utc")
comments = relationship("Comment", primaryjoin="Comment.parent_submission==Submission.id", back_populates="post")
subr = relationship("Sub", primaryjoin="foreign(Submission.sub)==remote(Sub.name)")
options = relationship("SubmissionOption", order_by="SubmissionOption.id")
category = relationship("Category", primaryjoin="Submission.category_id==Category.id")
bump_utc = deferred(Column(Integer, server_default=FetchedValue()))
def __init__(self, *args, **kwargs):
if "created_utc" not in kwargs: kwargs["created_utc"] = int(time.time())
super().__init__(*args, **kwargs)
def __repr__(self):
return f"
{CC} ONLY
" body = self.body if not body: return "" body = censor_slurs(body, v) body = normalize_urls_runtime(body, v) return body @lazy def realtitle(self, v): if self.club and not (v and (v.paid_dues or v.id == self.author_id)): if v: return random.choice(TROLLTITLES).format(username=v.username) elif dues == -2: return f'Please make an account to see this post' else: return f'{CC} MEMBERS ONLY' elif self.title_html: title = self.title_html else: title = self.title title = censor_slurs(title, v) return title @lazy def plaintitle(self, v): if self.club and not (v and (v.paid_dues or v.id == self.author_id)): if v: return random.choice(TROLLTITLES).format(username=v.username) else: return f'{CC} MEMBERS ONLY' else: title = self.title title = censor_slurs(title, v) return title @property @lazy def is_video(self): return self.url and any((self.url.lower().endswith(x) for x in ('.mp4','.webm','.mov'))) and is_safe_url(self.url) @property @lazy def is_audio(self): return self.url and any((self.url.lower().endswith(x) for x in ('.mp3','.wav','.ogg','.aac','.m4a','.flac'))) and is_safe_url(self.url) @property @lazy def is_image(self): if self.url and (self.url.lower().endswith('.webp') or self.url.lower().endswith('.jpg') or self.url.lower().endswith('.png') or self.url.lower().endswith('.gif') or self.url.lower().endswith('.jpeg') or self.url.lower().endswith('?maxwidth=9999') or self.url.lower().endswith('&fidelity=high')) and is_safe_url(self.url): return True return False @lazy def filtered_flags(self, v): return [f for f in self.flags if (v and v.shadowbanned) or not f.user.shadowbanned] @lazy def active_flags(self, v): return len(self.filtered_flags(v))