MarseyWorld/files/classes/hats.py

47 lines
1.3 KiB
Python
Raw Normal View History

from sqlalchemy import *
from sqlalchemy.orm import relationship
from files.__main__ 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-03 03:04:51 +00:00
from flask import g
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-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-03 03:04:51 +00:00
@property
@lazy
def number_sold(self):
return g.db.query(Hat).filter_by(hat_id=self.id).count()
2022-09-03 18:55:03 +00:00
@lazy
def censored_description(self, v):
return censor_slurs(self.description, v)
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)
equipped = Column(Boolean, default=False)
2022-09-03 19:36:50 +00:00
hat_def = relationship("HatDef")
owners = relationship("User", back_populates="owned_hats")
@property
@lazy
def name(self):
return self.hat_def.name
@lazy
def censored_description(self, v):
return self.hat_def.censored_description(v)