MarseyWorld/files/classes/hole.py

75 lines
2.2 KiB
Python

import random
import time
from sqlalchemy import Column
from sqlalchemy.ext.mutable import MutableList
from sqlalchemy.orm import relationship, deferred
from sqlalchemy.types import VARCHAR, Boolean, Integer
from sqlalchemy.dialects.postgresql import ARRAY
from files.classes import Base
from files.helpers.lazy import lazy
from files.helpers.config.const import *
from .hole_relationship import *
class Hole(Base):
__tablename__ = "holes"
name = Column(VARCHAR(HOLE_NAME_COLUMN_LENGTH), primary_key=True)
sidebar = Column(VARCHAR(HOLE_SIDEBAR_COLUMN_LENGTH))
sidebar_html = Column(VARCHAR(HOLE_SIDEBAR_HTML_COLUMN_LENGTH))
sidebarurls = Column(MutableList.as_mutable(ARRAY(VARCHAR(HOLE_BANNER_URL_COLUMN_LENGTH))), default=MutableList([]))
bannerurls = Column(MutableList.as_mutable(ARRAY(VARCHAR(HOLE_BANNER_URL_COLUMN_LENGTH))), default=MutableList([]))
marseyurl = Column(VARCHAR(HOLE_MARSEY_URL_LENGTH))
css = deferred(Column(VARCHAR(CSS_LENGTH_LIMIT)))
stealth = Column(Boolean)
created_utc = Column(Integer)
if SITE_NAME == 'WPD':
snappy_quotes = None
else:
snappy_quotes = deferred(Column(VARCHAR(HOLE_SNAPPY_QUOTES_LENGTH)))
blocks = relationship("HoleBlock", primaryjoin="HoleBlock.hole==Hole.name")
followers = relationship("HoleFollow", primaryjoin="HoleFollow.hole==Hole.name")
joins = relationship("StealthHoleUnblock", lazy="dynamic", primaryjoin="StealthHoleUnblock.hole==Hole.name")
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 self.name
@property
@lazy
def random_sidebar(self):
if not self.sidebarurls: return None
return random.choice(self.sidebarurls)
@property
@lazy
def random_banner(self):
if not self.bannerurls: return None
return random.choice(self.bannerurls)
@property
@lazy
def marsey_url(self):
if self.marseyurl: return self.marseyurl
return f'{SITE_FULL_IMAGES}/i/{SITE_NAME}/headericon.webp?x=7'
@property
@lazy
def join_num(self):
return self.joins.count()
@property
@lazy
def block_num(self):
return len(self.blocks)
@property
@lazy
def follow_num(self):
return len(self.followers)