2021-10-15 14:08:27 +00:00
|
|
|
from sqlalchemy import *
|
|
|
|
from sqlalchemy.orm import relationship
|
|
|
|
from files.__main__ import Base, app
|
|
|
|
from os import environ
|
|
|
|
from files.helpers.lazy import lazy
|
2022-01-19 09:07:16 +00:00
|
|
|
from files.helpers.const import *
|
2021-10-18 15:58:06 +00:00
|
|
|
from datetime import datetime
|
2022-01-22 10:41:37 +00:00
|
|
|
from json import loads
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2022-01-23 19:51:56 +00:00
|
|
|
class BadgeDef(Base):
|
|
|
|
__tablename__ = "badge_defs"
|
|
|
|
|
2022-01-24 16:17:21 +00:00
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
2022-01-23 19:51:56 +00:00
|
|
|
name = Column(String)
|
|
|
|
description = Column(String)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f"<BadgeDef(id={self.id})>"
|
|
|
|
|
|
|
|
|
2021-10-15 14:08:27 +00:00
|
|
|
class Badge(Base):
|
|
|
|
|
|
|
|
__tablename__ = "badges"
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
|
|
|
|
user_id = Column(Integer, ForeignKey('users.id'))
|
2021-11-21 17:40:47 +00:00
|
|
|
badge_id = Column(Integer)
|
2021-10-15 14:08:27 +00:00
|
|
|
description = Column(String)
|
|
|
|
url = Column(String)
|
2021-11-25 22:09:18 +00:00
|
|
|
user = relationship("User", viewonly=True)
|
2022-01-24 16:17:21 +00:00
|
|
|
badge = relationship("BadgeDef", primaryjoin="foreign(Badge.badge_id) == remote(BadgeDef.id)", viewonly=True)
|
2021-10-18 15:58:06 +00:00
|
|
|
|
2021-10-15 14:08:27 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return f"<Badge(user_id={self.user_id}, badge_id={self.badge_id})>"
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def text(self):
|
2022-01-30 20:43:04 +00:00
|
|
|
if self.name == "Chud":
|
2021-10-18 15:58:06 +00:00
|
|
|
ti = self.user.agendaposter_expires_utc
|
2022-01-23 19:51:56 +00:00
|
|
|
if ti: text = self.badge.description + " until " + datetime.utcfromtimestamp(ti).strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
else: text = self.badge.description + " permanently"
|
2022-01-31 01:41:04 +00:00
|
|
|
elif self.badge_id in (94,95,96,97,98,109):
|
2022-01-06 22:18:38 +00:00
|
|
|
if self.badge_id == 94: ti = self.user.progressivestack
|
|
|
|
elif self.badge_id == 95: ti = self.user.bird
|
|
|
|
elif self.badge_id == 96: ti = self.user.flairchanged
|
|
|
|
elif self.badge_id == 97: ti = self.user.longpost
|
2022-01-31 01:41:04 +00:00
|
|
|
elif self.badge_id == 98: ti = self.user.marseyawarded
|
|
|
|
elif self.badge_id == 109: ti = self.user.rehab
|
2022-01-23 19:51:56 +00:00
|
|
|
text = self.badge.description + " until " + datetime.utcfromtimestamp(ti).strftime('%Y-%m-%d %H:%M:%S')
|
2022-01-21 14:40:01 +00:00
|
|
|
elif self.description: text = self.description
|
2022-01-23 19:51:56 +00:00
|
|
|
elif self.badge.description: text = self.badge.description
|
2022-02-03 06:42:47 +00:00
|
|
|
else: return self.name
|
2022-01-24 16:17:21 +00:00
|
|
|
return f'{self.name} - {text}'
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def name(self):
|
2022-01-24 23:49:02 +00:00
|
|
|
return self.badge.name
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def path(self):
|
2022-01-28 21:42:09 +00:00
|
|
|
return f"{SITE_FULL}/static/assets/images/badges/{self.badge_id}.webp?a=1008"
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
2021-12-11 00:53:16 +00:00
|
|
|
def json(self):
|
2021-10-15 14:08:27 +00:00
|
|
|
return {'text': self.text,
|
|
|
|
'name': self.name,
|
|
|
|
'url': self.url,
|
2022-01-28 21:42:09 +00:00
|
|
|
'icon_url':self.path
|
2022-01-23 21:59:08 +00:00
|
|
|
}
|