misc: replace get_role_translation() with a get_function_roles() method (#.....)
gitea-wip/wcs/pipeline/head Build started... Details
gitea/wcs/pipeline/head Something is wrong with the build of this commit Details

It removes the unobvious "_translation()" that has nothing to do with
languages and returns a set() instead of a single role identifier; this
may allow multiple roles filling a single function, at a later stage.
This commit is contained in:
Frédéric Péters 2021-04-25 14:36:33 +02:00
parent 14c2aa6f9f
commit 8ec27fd255
6 changed files with 50 additions and 51 deletions

View File

@ -3304,18 +3304,20 @@ class FormBackOfficeStatusPage(FormStatusPage):
r += htmltext('<ul class="form-inspector biglist">') r += htmltext('<ul class="form-inspector biglist">')
for key, label in (workflow.roles or {}).items(): for key, label in (workflow.roles or {}).items():
r += htmltext('<li><span class="label">%s</span>') % label r += htmltext('<li><span class="label">%s</span>') % label
acting_role_id = self.filled.get_role_translation(key) r += htmltext('<div class="value">')
if acting_role_id: acting_role_ids = self.filled.get_function_roles(key)
for acting_role_id in acting_role_ids:
try: try:
acting_role = get_publisher().role_class.get(acting_role_id) acting_role = get_publisher().role_class.get(acting_role_id)
r += htmltext('<div class="value"><span>%s</span></div>') % acting_role.name r += htmltext('<span>%s</span> ') % acting_role.name
except KeyError: except KeyError:
r += htmltext('<div class="value"><span>%s %s</span></div>') % ( r += htmltext('<span>%s %s</span> ') % (
acting_role_id, acting_role_id,
_('(deleted)'), _('(deleted)'),
) )
else: if not acting_role_ids:
r += htmltext('<div class="value"><span class="unset">%s</span></div>') % _('unset') r += htmltext('<span class="unset">%s</span>') % _('unset')
r += htmltext('</div>')
r += htmltext('</li>\n') r += htmltext('</li>\n')
r += htmltext('</ul>') r += htmltext('</ul>')
r += htmltext('</div>') r += htmltext('</div>')

View File

@ -672,7 +672,8 @@ class FormData(StorableObject):
def get_display_id(self): def get_display_id(self):
return str(self.id_display or self.id) return str(self.id_display or self.id)
def get_role_translation(self, role_name): def get_function_roles(self, role_name):
# get a function name or role identifier and return a set of role identifiers
if role_name == '_submitter': if role_name == '_submitter':
raise Exception('_submitter is not a valid role') raise Exception('_submitter is not a valid role')
if str(role_name).startswith('_'): if str(role_name).startswith('_'):
@ -682,14 +683,15 @@ class FormData(StorableObject):
if not role_id and self.formdef.workflow_roles: if not role_id and self.formdef.workflow_roles:
role_id = self.formdef.workflow_roles.get(role_name) role_id = self.formdef.workflow_roles.get(role_name)
if role_id is None: if role_id is None:
return role_id return set()
return str(role_id) return set([str(role_id)])
return str(role_name) return set([str(role_name)])
def get_handling_role_id(self): def get_handling_role_id(self):
# TODO: look at current status and return the role(s) actually # TODO: look at current status and return the role(s) actually
# concerned by the handling of the formdata # concerned by the handling of the formdata
return self.get_role_translation('_receiver') for role_id in self.get_function_roles('_receiver'):
return role_id
def get_handling_role(self): def get_handling_role(self):
try: try:
@ -989,8 +991,7 @@ class FormData(StorableObject):
if self.is_submitter(user): if self.is_submitter(user):
return True return True
elif user: elif user:
role = self.get_role_translation(role) if self.get_function_roles(role).intersection(user.get_roles()):
if role in user.get_roles():
return True return True
return False return False
@ -1010,8 +1011,7 @@ class FormData(StorableObject):
# the very end (where it may be that there is no workflow status item # the very end (where it may be that there is no workflow status item
# at all). # at all).
for function_key in self.formdef.workflow.roles.keys(): for function_key in self.formdef.workflow.roles.keys():
handling_role = self.get_role_translation(function_key) for handling_role in self.get_function_roles(function_key):
if handling_role:
status_action_roles.add(handling_role) status_action_roles.add(handling_role)
wf_status = self.get_status() wf_status = self.get_status()
@ -1042,8 +1042,7 @@ class FormData(StorableObject):
if role == '_submitter': if role == '_submitter':
status_action_roles.add(role) status_action_roles.add(role)
else: else:
real_role = self.get_role_translation(role) for real_role in self.get_function_roles(role):
if real_role:
status_action_roles.add(real_role) status_action_roles.add(real_role)
return status_action_roles return status_action_roles

View File

@ -49,7 +49,7 @@ class HookDirectory(Directory):
break break
if not user: if not user:
continue continue
if self.formdata.get_role_translation(role) in user.get_roles(): if self.formdata.get_function_roles(role).intersection(user.get_roles()):
break break
else: else:
raise errors.AccessForbiddenError('insufficient roles') raise errors.AccessForbiddenError('insufficient roles')

View File

@ -63,16 +63,16 @@ class AggregationEmailWorkflowStatusItem(WorkflowStatusItem):
return return
for dest in self.to: for dest in self.to:
dest = formdata.get_role_translation(dest) for dest_id in formdata.get_function_roles(dest):
try: try:
aggregate = AggregationEmail.get(dest) aggregate = AggregationEmail.get(dest_id)
except KeyError: except KeyError:
aggregate = AggregationEmail(id=dest) aggregate = AggregationEmail(id=dest_id)
aggregate.append( aggregate.append(
{'formdef': formdata.formdef.id, 'formdata': formdata.id, 'formurl': formdata.get_url()} {'formdef': formdata.formdef.id, 'formdata': formdata.id, 'formurl': formdata.get_url()}
) )
aggregate.store() aggregate.store()
register_item_class(AggregationEmailWorkflowStatusItem) register_item_class(AggregationEmailWorkflowStatusItem)

View File

@ -152,12 +152,12 @@ class SendNotificationWorkflowStatusItem(WebserviceCallStatusItem):
users.append(formdata.get_user()) users.append(formdata.get_user())
continue continue
dest = formdata.get_role_translation(dest) for dest_id in formdata.get_function_roles(dest):
try: try:
role = get_publisher().role_class.get(dest) role = get_publisher().role_class.get(dest_id)
except KeyError: except KeyError:
continue continue
users.extend(get_publisher().user_class.get_users_with_role(role.id)) users.extend(get_publisher().user_class.get_users_with_role(role.id))
for user in users: for user in users:
if not user or not user.is_active: if not user or not user.is_active:

View File

@ -531,10 +531,12 @@ class Workflow(StorableObject):
if '_submitter' in (trigger.roles or []) and formdata.is_submitter(user): if '_submitter' in (trigger.roles or []) and formdata.is_submitter(user):
actions.append(action) actions.append(action)
break break
roles = [ roles = set()
formdata.get_role_translation(x) for x in (trigger.roles or []) if x != '_submitter' for role_id in trigger.roles or []:
] if role_id == '_submitter':
if set(roles).intersection(user.get_roles()): continue
roles |= formdata.get_function_roles(role_id)
if roles.intersection(user.get_roles()):
actions.append(action) actions.append(action)
break break
return actions return actions
@ -1642,8 +1644,7 @@ class WorkflowStatus:
continue continue
if user is None: if user is None:
continue continue
role = filled.get_role_translation(role) if filled.get_function_roles(role).intersection(user.get_roles()):
if role in user.get_roles():
break break
else: else:
continue continue
@ -1738,9 +1739,8 @@ class WorkflowStatus:
for role in visibility_roles: for role in visibility_roles:
if role != '_submitter': if role != '_submitter':
role = formdata.get_role_translation(role) if formdata.get_function_roles(role).intersection(user_roles):
if role in user_roles: return True
return True
return False return False
def is_endpoint(self): def is_endpoint(self):
@ -1940,8 +1940,7 @@ class WorkflowStatusItem(XmlSerialisable):
continue continue
if not user: if not user:
continue continue
role = formdata.get_role_translation(role) if formdata.get_function_roles(role).intersection(user.get_roles()):
if role in user.get_roles():
return True return True
return False return False
@ -2955,13 +2954,12 @@ class SendmailWorkflowStatusItem(WorkflowStatusItem):
addresses.append(submitter_email) addresses.append(submitter_email)
continue continue
dest = formdata.get_role_translation(dest) for real_dest in formdata.get_function_roles(dest):
try:
try: role = get_publisher().role_class.get(real_dest)
role = get_publisher().role_class.get(dest) except KeyError:
except KeyError: continue
continue addresses.extend(role.get_emails())
addresses.extend(role.get_emails())
if not addresses: if not addresses:
return return