make POST on positions api return result again

This commit is contained in:
Rio6 2021-08-18 16:41:11 -04:00
parent 6e30437b61
commit e7772d2564
2 changed files with 16 additions and 11 deletions

View File

@ -28,12 +28,5 @@ def update_positions():
body = request.get_json(force=True)
# TODO verify json
# Reverse the dict so it's easier to use
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)
txn = UpdateMemberPositionsTransaction(body)
return create_streaming_response(txn)

View File

@ -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
def __init__(self, positions: Dict[str, List[str]]):
def __init__(self, positions_reversed: Dict[str, List[str]]):
super().__init__()
self.positions = positions
self.old_positions = {} # For rollback
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):
cfg = component.getUtility(IConfig)
mailing_lists = cfg.get('auxiliary mailing lists_exec')
subscribe_status: Dict[IUser, bool] = {}
new_positions_reversed: Dict[str, str] = {} # For returning result
# Remove positions for old users
for user in self.ldap_srv.get_users_with_positions():
@ -38,7 +45,12 @@ class UpdateMemberPositionsTransaction(AbstractTransaction):
user = self.ldap_srv.get_user(username)
self.old_positions[username] = user.positions[:]
user.set_positions(positions)
subscribe_status[user] = len(positions) > 0
for position in user.positions:
new_positions_reversed[position] = user.uid
yield f'update_positions_{username}'
# Update mailing list subscription
@ -54,7 +66,7 @@ class UpdateMemberPositionsTransaction(AbstractTransaction):
except Exception as e:
logger.warning(f'Failed to update mailing list for {user.uid}')
self.finish(None)
self.finish(new_positions_reversed)
def rollback(self):
for username, positions in self.old_positions.items():