mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-23 00:41:35 -05:00
Refactor saving user profile
This commit is contained in:
@@ -7,6 +7,11 @@ from app import db
|
|||||||
from app.models import (User, VerifyCode)
|
from app.models import (User, VerifyCode)
|
||||||
|
|
||||||
|
|
||||||
|
def _remove_values_for_keys_if_present(dict, keys):
|
||||||
|
for key in keys:
|
||||||
|
dict.pop(key, None)
|
||||||
|
|
||||||
|
|
||||||
def create_secret_code():
|
def create_secret_code():
|
||||||
return ''.join(map(str, random.sample(range(9), 5)))
|
return ''.join(map(str, random.sample(range(9), 5)))
|
||||||
|
|
||||||
@@ -16,9 +21,7 @@ def save_model_user(usr, update_dict={}, pwd=None):
|
|||||||
usr.password = pwd
|
usr.password = pwd
|
||||||
usr.password_changed_at = datetime.utcnow()
|
usr.password_changed_at = datetime.utcnow()
|
||||||
if update_dict:
|
if update_dict:
|
||||||
if update_dict.get('id'):
|
_remove_values_for_keys_if_present(update_dict, ['id', 'password', 'password_changed_at'])
|
||||||
del update_dict['id']
|
|
||||||
update_dict.pop('password_changed_at')
|
|
||||||
db.session.query(User).filter_by(id=usr.id).update(update_dict)
|
db.session.query(User).filter_by(id=usr.id).update(update_dict)
|
||||||
else:
|
else:
|
||||||
db.session.add(usr)
|
db.session.add(usr)
|
||||||
|
|||||||
@@ -55,14 +55,10 @@ def create_user():
|
|||||||
def update_user(user_id):
|
def update_user(user_id):
|
||||||
user_to_update = get_model_users(user_id=user_id)
|
user_to_update = get_model_users(user_id=user_id)
|
||||||
req_json = request.get_json()
|
req_json = request.get_json()
|
||||||
update_dct, errors = user_schema_load_json.load(req_json)
|
|
||||||
pwd = req_json.get('password', None)
|
pwd = req_json.get('password', None)
|
||||||
# TODO password validation, it is already done on the admin app
|
if not pwd:
|
||||||
# but would be good to have the same validation here.
|
raise InvalidRequest('Invalid entry for password', status_code=400)
|
||||||
if pwd is not None and not pwd:
|
save_model_user(user_to_update, update_dict=req_json, pwd=pwd)
|
||||||
errors.update({'password': ['Invalid data for field']})
|
|
||||||
raise InvalidRequest(errors, status_code=400)
|
|
||||||
save_model_user(user_to_update, update_dict=update_dct, pwd=pwd)
|
|
||||||
return jsonify(data=user_schema.dump(user_to_update).data), 200
|
return jsonify(data=user_schema.dump(user_to_update).data), 200
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user