Skip to content
Snippets Groups Projects
Commit 8212a50d authored by Todd Dembrey's avatar Todd Dembrey
Browse files

Update the workflow/phases api to simplify finding phases

parent 921e9f3b
No related branches found
No related tags found
No related merge requests found
from collections import defaultdict
from django import template from django import template
register = template.Library() register = template.Library()
def find_last_visible_phase(phases, user, current_phase):
last_phase = current_phase
while not last_phase.permissions.can_view(user):
last_phase = phases[last_phase.step - 1][0]
return last_phase
@register.inclusion_tag('funds/includes/status_bar.html') @register.inclusion_tag('funds/includes/status_bar.html')
def status_bar(workflow, current_phase, user, css_class='', same_stage=False): def status_bar(workflow, current_phase, user, css_class='', same_stage=False):
all_phases = defaultdict(list) phases = workflow.phases_for(user)
for phase in list(workflow.values()):
all_phases[phase.step].append(phase)
# Grab the first phase for each step - visible only, the display phase if same_stage:
phases = [ phases = [
phase for phase, *_ in all_phases.values() phase for phase in phases
if phase.permissions.can_view(user) if phase.stage == current_phase.stage
and (not same_stage or phase.stage == current_phase.stage) ]
]
if not current_phase.permissions.can_view(user): if not current_phase.permissions.can_view(user):
current_phase = find_last_visible_phase(all_phases, user, current_phase) current_phase = workflow.latest_visible(current_phase, user)
# Current step not shown for user, move current phase to last good location # Current step not shown for user, move current phase to last good location
elif not all_phases[current_phase.step][0].permissions.can_view(user): elif not workflow.stepped_phases[current_phase.step][0].permissions.can_view(user):
new_phase_list = [] new_phase_list = []
for phase in reversed(phases): for phase in reversed(phases):
if phase.step <= current_phase.step and current_phase not in new_phase_list: if phase.step <= current_phase.step and current_phase not in new_phase_list:
......
...@@ -38,6 +38,29 @@ class Workflow(dict): ...@@ -38,6 +38,29 @@ class Workflow(dict):
stages.append(phase.stage) stages.append(phase.stage)
return stages return stages
@property
def stepped_phases(self):
phases = defaultdict(list)
for phase in list(workflow.values()):
all_phases[phase.step].append(phase)
return phases
def phases_for(self, user):
# Grab the first phase for each step - visible only, the display phase
phases = [
phase for phase, *_ in self.stepped_phases.values()
if phase.permissions.can_view(user)
]
def previous_visible(self, current, user):
"""Find the latest phase that the user has view permissions for"""
display_phase = self.stepped_phases[current.step][0]
phases = self.phases_for(user)
index = phases.index(current)
for phase in phases[index - 1::-1]:
if phase.permission.can_view(user):
return phase
class Phase: class Phase:
def __init__(self, name, display, stage, permissions, step, public=None, transitions=dict()): def __init__(self, name, display, stage, permissions, step, public=None, transitions=dict()):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment