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-03-15 04:47:47 +00:00
|
|
|
from files.helpers.config.const import *
|
2022-11-01 23:46:56 +00:00
|
|
|
|
|
|
|
def badge_grant(user, badge_id, description=None, url=None, notify=True):
|
|
|
|
assert user != None
|
|
|
|
if user.has_badge(badge_id):
|
|
|
|
return
|
|
|
|
|
2023-01-01 02:36:04 +00:00
|
|
|
if description and len(description) > 256:
|
2023-01-01 00:38:41 +00:00
|
|
|
abort(400, "Custom description is too long, max 256 characters!")
|
|
|
|
|
2023-01-01 02:36:04 +00:00
|
|
|
if url and len(url) > 256:
|
2023-01-01 00:38:41 +00:00
|
|
|
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,
|
|
|
|
description=description,
|
|
|
|
url=url,
|
|
|
|
)
|
|
|
|
|
2023-03-15 03:58:00 +00:00
|
|
|
db.add(badge)
|
|
|
|
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}")
|