add update method to Users repo
This commit is contained in:
@@ -5,7 +5,7 @@ from atst.database import db
|
||||
from atst.models import User
|
||||
|
||||
from .roles import Roles
|
||||
from .exceptions import NotFoundError, AlreadyExistsError
|
||||
from .exceptions import NotFoundError, AlreadyExistsError, UnauthorizedError
|
||||
|
||||
|
||||
class Users(object):
|
||||
@@ -53,7 +53,7 @@ class Users(object):
|
||||
return user
|
||||
|
||||
@classmethod
|
||||
def update(cls, user_id, atat_role_name):
|
||||
def update_role(cls, user_id, atat_role_name):
|
||||
|
||||
user = Users.get(user_id)
|
||||
atat_role = Roles.get(atat_role_name)
|
||||
@@ -63,3 +63,27 @@ class Users(object):
|
||||
db.session.commit()
|
||||
|
||||
return user
|
||||
|
||||
_UPDATEABLE_ATTRS = {
|
||||
"first_name",
|
||||
"last_name",
|
||||
"email",
|
||||
"phone_number",
|
||||
"service_branch",
|
||||
"citizenship",
|
||||
"designation",
|
||||
"date_latest_training",
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def update(cls, user, user_delta):
|
||||
if not set(user_delta.keys()).issubset(Users._UPDATEABLE_ATTRS):
|
||||
raise UnauthorizedError(user, "update DOD ID")
|
||||
|
||||
for key, value in user_delta.items():
|
||||
setattr(user, key, value)
|
||||
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
|
||||
return user
|
||||
|
||||
Reference in New Issue
Block a user