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
|
|
|
|
|
|
|
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)
|
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})>"
|
|
|
|
|
2021-11-21 17:40:47 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def badge(self):
|
2022-01-22 10:41:37 +00:00
|
|
|
with open("badges.json", 'r') as f:
|
2022-01-22 19:21:16 +00:00
|
|
|
return loads(f.read().replace("'",'"'))[str(self.badge_id)]
|
2021-11-21 17:40:47 +00:00
|
|
|
|
2021-10-15 14:08:27 +00:00
|
|
|
@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-22 10:41:37 +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-22 10:41:37 +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-22 10:41:37 +00:00
|
|
|
elif self.badge["description"]: text = self.badge["description"]
|
2022-01-21 14:42:13 +00:00
|
|
|
else: return ''
|
2022-01-21 14:40:01 +00:00
|
|
|
return f' - {text}'
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def name(self):
|
2022-01-22 10:41:37 +00:00
|
|
|
return self.badge["name"]
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def path(self):
|
2022-01-22 12:27:28 +00:00
|
|
|
return f"/static/assets/images/badges/{self.badge_id}.webp?a=6"
|
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}"
|
2021-12-11 00:53:16 +00:00
|
|
|
}
|