2022-11-15 09:19:08 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
from sqlalchemy import Column, ForeignKey
|
2023-07-07 23:12:59 +00:00
|
|
|
from sqlalchemy.orm import relationship
|
2022-11-15 09:19:08 +00:00
|
|
|
from sqlalchemy.sql.sqltypes import *
|
2023-06-08 02:03:23 +00:00
|
|
|
from flask import g
|
2022-11-15 09:19:08 +00:00
|
|
|
|
|
|
|
from files.classes import Base
|
2022-09-03 03:04:51 +00:00
|
|
|
from files.helpers.lazy import lazy
|
2022-09-03 18:55:03 +00:00
|
|
|
from files.helpers.regex import censor_slurs
|
2022-09-02 23:58:55 +00:00
|
|
|
|
|
|
|
class HatDef(Base):
|
|
|
|
__tablename__ = "hat_defs"
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
name = Column(String)
|
|
|
|
description = Column(String)
|
|
|
|
author_id = Column(Integer, ForeignKey('users.id'))
|
|
|
|
price = Column(Integer)
|
2022-09-10 05:37:11 +00:00
|
|
|
submitter_id = Column(Integer, ForeignKey("users.id"))
|
2022-09-19 20:40:33 +00:00
|
|
|
created_utc = Column(Integer)
|
2022-09-02 23:58:55 +00:00
|
|
|
|
2022-09-03 19:28:49 +00:00
|
|
|
author = relationship("User", primaryjoin="HatDef.author_id == User.id", back_populates="designed_hats")
|
2022-09-10 05:37:11 +00:00
|
|
|
submitter = relationship("User", primaryjoin="HatDef.submitter_id == User.id")
|
2022-09-02 23:58:55 +00:00
|
|
|
|
2022-09-19 20:40:33 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
if "created_utc" not in kwargs: kwargs["created_utc"] = int(time.time())
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2022-09-12 10:19:35 +00:00
|
|
|
def __repr__(self):
|
2022-11-29 21:02:38 +00:00
|
|
|
return f"<{self.__class__.__name__}(id={self.id})>"
|
2022-09-12 10:19:35 +00:00
|
|
|
|
2023-06-08 00:49:37 +00:00
|
|
|
@property
|
2022-09-03 03:04:51 +00:00
|
|
|
@lazy
|
2023-06-08 00:49:37 +00:00
|
|
|
def number_sold(self):
|
2023-06-08 02:03:23 +00:00
|
|
|
return g.db.query(Hat).filter_by(hat_id=self.id).count()
|
2022-09-03 03:04:51 +00:00
|
|
|
|
2022-09-03 18:55:03 +00:00
|
|
|
@lazy
|
|
|
|
def censored_description(self, v):
|
|
|
|
return censor_slurs(self.description, v)
|
|
|
|
|
2022-11-01 03:37:52 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def is_purchasable(self):
|
|
|
|
return self.price > 0
|
|
|
|
|
|
|
|
|
2022-09-02 23:58:55 +00:00
|
|
|
class Hat(Base):
|
|
|
|
__tablename__ = "hats"
|
|
|
|
|
|
|
|
user_id = Column(Integer, ForeignKey('users.id'), primary_key=True)
|
2022-09-03 19:36:50 +00:00
|
|
|
hat_id = Column(Integer, ForeignKey('hat_defs.id'), primary_key=True)
|
2022-09-05 03:44:24 +00:00
|
|
|
equipped = Column(Boolean, default=False)
|
2022-09-19 20:40:33 +00:00
|
|
|
created_utc = Column(Integer)
|
2022-09-03 19:36:50 +00:00
|
|
|
|
2022-09-05 03:44:24 +00:00
|
|
|
hat_def = relationship("HatDef")
|
|
|
|
owners = relationship("User", back_populates="owned_hats")
|
|
|
|
|
2022-09-19 20:40:33 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
if "created_utc" not in kwargs: kwargs["created_utc"] = int(time.time())
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2022-09-12 10:19:35 +00:00
|
|
|
def __repr__(self):
|
2022-11-29 21:02:38 +00:00
|
|
|
return f"<{self.__class__.__name__}(user_id={self.user_id}, hat_id={self.hat_id})>"
|
2022-09-12 10:19:35 +00:00
|
|
|
|
2022-09-05 03:44:24 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def name(self):
|
|
|
|
return self.hat_def.name
|
|
|
|
|
|
|
|
@lazy
|
|
|
|
def censored_description(self, v):
|
2022-09-29 05:43:29 +00:00
|
|
|
return self.hat_def.censored_description(v)
|