2022-06-06 04:07:38 +00:00
|
|
|
from flask import g, abort
|
|
|
|
from files.classes.user import User
|
|
|
|
from files.classes.badges import Badge, BadgeDef
|
|
|
|
|
|
|
|
# TODO: More sanity checks on passed parameters.
|
|
|
|
# TODO: Add `replace=False` parameter which, when set true, removes any
|
2022-06-13 18:33:25 +00:00
|
|
|
# existing badge with identical id & user and replaces with new one.
|
2022-06-10 11:12:19 +00:00
|
|
|
def badge_grant(user_id, badge_id, desc='', url='', commit=True):
|
2022-06-06 04:07:38 +00:00
|
|
|
user = g.db.query(User).filter(User.id == int(user_id)).one_or_none()
|
|
|
|
if not user:
|
2022-06-10 11:12:19 +00:00
|
|
|
return None
|
2022-06-06 04:07:38 +00:00
|
|
|
elif user.has_badge(badge_id):
|
2022-06-10 11:12:19 +00:00
|
|
|
return None
|
2022-06-06 04:07:38 +00:00
|
|
|
|
|
|
|
badge = Badge(
|
|
|
|
badge_id=int(badge_id),
|
|
|
|
user_id=user.id,
|
|
|
|
description=desc if desc != '' else None,
|
|
|
|
url=url if url != '' else None,
|
|
|
|
)
|
|
|
|
|
|
|
|
g.db.add(badge)
|
2022-06-10 11:12:19 +00:00
|
|
|
if commit:
|
|
|
|
g.db.commit()
|
|
|
|
else:
|
|
|
|
g.db.flush()
|
|
|
|
return badge
|