make POST on positions api return result again
This commit is contained in:
parent
6e30437b61
commit
e7772d2564
|
@ -28,12 +28,5 @@ def update_positions():
|
||||||
body = request.get_json(force=True)
|
body = request.get_json(force=True)
|
||||||
# TODO verify json
|
# TODO verify json
|
||||||
|
|
||||||
# Reverse the dict so it's easier to use
|
txn = UpdateMemberPositionsTransaction(body)
|
||||||
member_positions = {}
|
|
||||||
for position, user in body.items():
|
|
||||||
if user not in member_positions:
|
|
||||||
member_positions[user] = []
|
|
||||||
member_positions[user].append(position)
|
|
||||||
|
|
||||||
txn = UpdateMemberPositionsTransaction(member_positions)
|
|
||||||
return create_streaming_response(txn)
|
return create_streaming_response(txn)
|
||||||
|
|
|
@ -16,17 +16,24 @@ class UpdateMemberPositionsTransaction(AbstractTransaction):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Positions is a dict where keys are member names and values are the list of positions they have
|
# Positions is a dict where keys are member names and values are the list of positions they have
|
||||||
def __init__(self, positions: Dict[str, List[str]]):
|
def __init__(self, positions_reversed: Dict[str, List[str]]):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.positions = positions
|
|
||||||
self.old_positions = {} # For rollback
|
self.old_positions = {} # For rollback
|
||||||
self.ldap_srv = component.getUtility(ILDAPService)
|
self.ldap_srv = component.getUtility(ILDAPService)
|
||||||
|
|
||||||
|
# Reverse the dict so it's easier to use
|
||||||
|
self.positions = {}
|
||||||
|
for position, user in positions_reversed.items():
|
||||||
|
if user not in self.positions:
|
||||||
|
self.positions[user] = []
|
||||||
|
self.positions[user].append(position)
|
||||||
|
|
||||||
def child_execute_iter(self):
|
def child_execute_iter(self):
|
||||||
cfg = component.getUtility(IConfig)
|
cfg = component.getUtility(IConfig)
|
||||||
mailing_lists = cfg.get('auxiliary mailing lists_exec')
|
mailing_lists = cfg.get('auxiliary mailing lists_exec')
|
||||||
|
|
||||||
subscribe_status: Dict[IUser, bool] = {}
|
subscribe_status: Dict[IUser, bool] = {}
|
||||||
|
new_positions_reversed: Dict[str, str] = {} # For returning result
|
||||||
|
|
||||||
# Remove positions for old users
|
# Remove positions for old users
|
||||||
for user in self.ldap_srv.get_users_with_positions():
|
for user in self.ldap_srv.get_users_with_positions():
|
||||||
|
@ -38,7 +45,12 @@ class UpdateMemberPositionsTransaction(AbstractTransaction):
|
||||||
user = self.ldap_srv.get_user(username)
|
user = self.ldap_srv.get_user(username)
|
||||||
self.old_positions[username] = user.positions[:]
|
self.old_positions[username] = user.positions[:]
|
||||||
user.set_positions(positions)
|
user.set_positions(positions)
|
||||||
|
|
||||||
subscribe_status[user] = len(positions) > 0
|
subscribe_status[user] = len(positions) > 0
|
||||||
|
|
||||||
|
for position in user.positions:
|
||||||
|
new_positions_reversed[position] = user.uid
|
||||||
|
|
||||||
yield f'update_positions_{username}'
|
yield f'update_positions_{username}'
|
||||||
|
|
||||||
# Update mailing list subscription
|
# Update mailing list subscription
|
||||||
|
@ -54,7 +66,7 @@ class UpdateMemberPositionsTransaction(AbstractTransaction):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f'Failed to update mailing list for {user.uid}')
|
logger.warning(f'Failed to update mailing list for {user.uid}')
|
||||||
|
|
||||||
self.finish(None)
|
self.finish(new_positions_reversed)
|
||||||
|
|
||||||
def rollback(self):
|
def rollback(self):
|
||||||
for username, positions in self.old_positions.items():
|
for username, positions in self.old_positions.items():
|
||||||
|
|
Loading…
Reference in New Issue