2022-05-04 23:09:46 +00:00
|
|
|
from sqlalchemy import *
|
|
|
|
from sqlalchemy.orm import relationship
|
|
|
|
from files.__main__ import Base, app
|
|
|
|
from files.helpers.lazy import lazy
|
|
|
|
from files.helpers.const import *
|
|
|
|
from datetime import datetime
|
|
|
|
from json import loads
|
2022-06-10 21:40:43 +00:00
|
|
|
import time
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
class BadgeDef(Base):
|
|
|
|
__tablename__ = "badge_defs"
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
|
|
name = Column(String)
|
|
|
|
description = Column(String)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f"<BadgeDef(id={self.id})>"
|
|
|
|
|
|
|
|
|
|
|
|
class Badge(Base):
|
|
|
|
|
|
|
|
__tablename__ = "badges"
|
|
|
|
|
|
|
|
user_id = Column(Integer, ForeignKey('users.id'), primary_key=True)
|
|
|
|
badge_id = Column(Integer, ForeignKey('badge_defs.id'), primary_key=True)
|
|
|
|
description = Column(String)
|
|
|
|
url = Column(String)
|
2022-06-10 21:40:43 +00:00
|
|
|
created_utc = Column(Integer)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-07-02 06:48:04 +00:00
|
|
|
user = relationship("User", back_populates="badges")
|
|
|
|
badge = relationship("BadgeDef", primaryjoin="foreign(Badge.badge_id) == remote(BadgeDef.id)")
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-06-10 21:40:43 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
if "created_utc" not in kwargs:
|
|
|
|
kwargs["created_utc"] = int(time.time())
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return f"<Badge(user_id={self.user_id}, badge_id={self.badge_id})>"
|
|
|
|
|
2022-07-09 08:35:47 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def until(self):
|
|
|
|
if self.badge_id == 28: return self.user.agendaposter
|
|
|
|
if self.badge_id == 94: return self.user.progressivestack
|
|
|
|
if self.badge_id == 95: return self.user.bird
|
|
|
|
if self.badge_id == 96: return self.user.flairchanged
|
|
|
|
if self.badge_id == 97: return self.user.longpost
|
|
|
|
if self.badge_id == 98: return self.user.marseyawarded
|
|
|
|
if self.badge_id == 109: return self.user.rehab
|
|
|
|
return None
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def text(self):
|
2022-07-09 08:35:47 +00:00
|
|
|
if self.badge_id == 28:
|
|
|
|
if ti: text = self.badge.description + " until"
|
2022-05-04 23:09:46 +00:00
|
|
|
else: text = self.badge.description + " permanently"
|
|
|
|
elif self.badge_id in {94,95,96,97,98,109}:
|
2022-07-09 08:35:47 +00:00
|
|
|
text = self.badge.description + " until"
|
|
|
|
elif self.description:
|
|
|
|
text = self.description
|
|
|
|
elif self.badge.description:
|
|
|
|
text = self.badge.description
|
|
|
|
else:
|
|
|
|
return self.name
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
return f'{self.name} - {text}'
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def name(self):
|
|
|
|
return self.badge.name
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def path(self):
|
2022-06-22 15:51:19 +00:00
|
|
|
return f"/i/badges/{self.badge_id}.webp"
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def json(self):
|
|
|
|
return {'text': self.text,
|
|
|
|
'name': self.name,
|
|
|
|
'url': self.url,
|
|
|
|
'icon_url':self.path
|
|
|
|
}
|