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
|
2021-11-21 17:40:47 +00:00
|
|
|
from files.helpers.const import BADGES
|
2021-10-18 15:58:06 +00:00
|
|
|
from datetime import datetime
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
site_name = environ.get("SITE_NAME").strip()
|
|
|
|
|
|
|
|
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):
|
|
|
|
return BADGES[self.badge_id]
|
|
|
|
|
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
|
2021-11-21 17:40:47 +00:00
|
|
|
if ti: return self.badge['description'] + " until " + datetime.utcfromtimestamp(ti).strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
else: return self.badge['description'] + " permanently"
|
2021-10-18 15:58:06 +00:00
|
|
|
elif self.description: return self.description
|
2021-11-21 17:40:47 +00:00
|
|
|
else: return self.badge['description']
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def name(self):
|
2021-11-21 17:40:47 +00:00
|
|
|
return self.badge['name']
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def path(self):
|
2021-11-21 18:54:36 +00:00
|
|
|
return f"/assets/images/badges/{self.name.replace(' ','%20')}.webp"
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def json_core(self):
|
|
|
|
|
|
|
|
return {'text': self.text,
|
|
|
|
'name': self.name,
|
|
|
|
'url': self.url,
|
|
|
|
'icon_url':f"https://{app.config['SERVER_NAME']}{self.path}"
|
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def json(self):
|
|
|
|
return self.json_core
|