admin_schemas: fix USER_UPDATE's coerce

- admin_api.users: fix missing attrs / wrong calls
 - enums: add Flags.value attr for an instantiated Flags
This commit is contained in:
Luna 2019-04-22 01:45:26 -03:00
parent e65db52c62
commit 717c02bdd7
3 changed files with 8 additions and 5 deletions

View File

@ -56,5 +56,5 @@ GUILD_UPDATE = {
} }
USER_UPDATE = { USER_UPDATE = {
'flags': {'required': False, 'coerce': UserFlags} 'flags': {'required': False, 'coerce': UserFlags.from_int}
} }

View File

@ -129,20 +129,21 @@ async def patch_user(user_id: int):
# get the original user for flags checking # get the original user for flags checking
user = await app.storage.get_user(user_id) user = await app.storage.get_user(user_id)
old_flags = UserFlags(user['flags']) old_flags = UserFlags.from_int(user['flags'])
# j.flags is already a UserFlags since we coerce it.
if 'flags' in j: if 'flags' in j:
new_flags = UserFlags(j['flags']) new_flags = j['flags']
# disallow any changes to the staff badge # disallow any changes to the staff badge
if new_flags.staff != old_flags.staff: if new_flags.is_staff != old_flags.is_staff:
raise Forbidden('you can not change a users staff badge') raise Forbidden('you can not change a users staff badge')
await app.db.execute(""" await app.db.execute("""
UPDATE users UPDATE users
SET flags = $1 SET flags = $1
WHERE id = $2 WHERE id = $2
""", j['flags'], user_id) """, new_flags.value, user_id)
public_user, _ = await mass_user_update(user_id, app) public_user, _ = await mass_user_update(user_id, app)
return jsonify(public_user) return jsonify(public_user)

View File

@ -58,6 +58,8 @@ class Flags:
def _make_int(value): def _make_int(value):
res = Flags() res = Flags()
setattr(res, 'value', value)
for attr, val in attrs: for attr, val in attrs:
# get only the ones that represent a field in the # get only the ones that represent a field in the
# number's bits # number's bits