2022-11-01 23:46:56 +00:00
|
|
|
from flask import g
|
2022-11-15 09:19:08 +00:00
|
|
|
|
2022-11-01 23:46:56 +00:00
|
|
|
from files.classes.badges import Badge
|
|
|
|
from files.helpers.alerts import send_repeatable_notification
|
|
|
|
|
2023-09-11 18:30:13 +00:00
|
|
|
def badge_grant(user, badge_id, description=None, url=None, notify=True):
|
2023-08-21 18:28:48 +00:00
|
|
|
g.db.flush()
|
2023-08-05 19:50:53 +00:00
|
|
|
existing = g.db.query(Badge).filter_by(user_id=user.id, badge_id=badge_id).one_or_none()
|
|
|
|
if existing: return
|
2023-01-01 00:38:41 +00:00
|
|
|
|
2023-09-11 18:30:05 +00:00
|
|
|
if description and len(description) > 256:
|
|
|
|
abort(400, "Custom description is too long, max 256 characters!")
|
|
|
|
|
|
|
|
if url and len(url) > 256:
|
|
|
|
abort(400, "URL is too long, max 256 characters!")
|
|
|
|
|
2022-11-01 23:46:56 +00:00
|
|
|
badge = Badge(
|
|
|
|
badge_id=int(badge_id),
|
|
|
|
user_id=user.id,
|
2023-09-11 18:30:05 +00:00
|
|
|
description=description,
|
|
|
|
url=url,
|
2022-11-01 23:46:56 +00:00
|
|
|
)
|
|
|
|
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.add(badge)
|
|
|
|
g.db.flush()
|
2022-11-01 23:46:56 +00:00
|
|
|
|
|
|
|
if notify:
|
|
|
|
send_repeatable_notification(user.id,
|
2023-02-24 02:54:31 +00:00
|
|
|
"@AutoJanny has given you the following profile badge:\n\n" +
|
2023-03-12 13:02:31 +00:00
|
|
|
f"{badge.path}\n\n**{badge.name}**\n\n{badge.badge.description}")
|