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):
|
2021-10-18 15:58:06 +00:00
|
|
|
if self.name == "Agendaposter":
|
|
|
|
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-06 22:18:38 +00:00
|
|
|
elif self.badge_id in (94,95,96,97,98):
|
|
|
|
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
|
|
|
|
else: ti = self.user.marseyawarded
|
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-01-21 14:42:13 +00:00
|
|
|
else: return ''
|
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 16:46:07 +00:00
|
|
|
try: return self.badge.name
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
print(self.badge_id)
|
|
|
|
return ""
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def path(self):
|
2022-01-24 16:44:54 +00:00
|
|
|
return f"/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-19 09:07:16 +00:00
|
|
|
'icon_url':f"{SITE_FULL}{self.path}"
|
2022-01-23 21:59:08 +00:00
|
|
|
}
|