Skip to content

Commit

Permalink
[#107] Make next_approvals instance API work again
Browse files Browse the repository at this point in the history
  • Loading branch information
javrasya committed Nov 17, 2019
1 parent 80a77f0 commit af99b85
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
11 changes: 5 additions & 6 deletions river/core/instanceworkflowobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ def __init__(self, workflow_object, field_name):
self.workflow_object = workflow_object
self.content_type = app_config.CONTENT_TYPE_CLASS.objects.get_for_model(self.workflow_object)
self.field_name = field_name
self.workflow = Workflow.objects.filter(content_type=self.content_type, field_name=self.field_name).first()
self.initialized = False

@transaction.atomic
def initialize_approvals(self):
if not self.initialized:
workflow = Workflow.objects.filter(content_type=self.content_type, field_name=self.field_name).first()
if workflow and workflow.transition_approvals.filter(workflow_object=self.workflow_object).count() == 0:
transition_approval_metas = workflow.transition_approval_metas.all()
if self.workflow and self.workflow.transition_approvals.filter(workflow_object=self.workflow_object).count() == 0:
transition_approval_metas = self.workflow.transition_approval_metas.all()
meta_dict = six.moves.reduce(
lambda agg, meta: dict(agg, **{self._to_key(meta.source_state): agg.get(self._to_key(meta.source_state), []) + [meta]}),
transition_approval_metas,
Expand All @@ -46,7 +46,7 @@ def initialize_approvals(self):
destination_state=next_meta.destination_state,
priority=next_meta.priority,
meta=next_meta,
workflow=workflow,
workflow=self.workflow,
defaults={
'status': PENDING,
}
Expand All @@ -70,8 +70,7 @@ def on_final_state(self):
@property
def next_approvals(self):
return TransitionApproval.objects.filter(
content_type=self.content_type,
field_name=self.field_name,
workflow=self.workflow,
object_id=self.workflow_object.pk,
source_state=self.get_state()
)
Expand Down
28 changes: 28 additions & 0 deletions river/tests/core/test__instance_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,31 @@ def test__shouldHandleUndefinedSecondWorkflowCase(self):

assert_that(workflow_object.model.status1, equal_to(state1))
assert_that(workflow_object.model.status2, none())

def test__shouldReturnNextApprovals(self):
state1 = StateObjectFactory(label="state1")
state2 = StateObjectFactory(label="state2")
state3 = StateObjectFactory(label="state3")

workflow = WorkflowFactory(initial_state=state1, content_type=self.content_type, field_name="my_field")
meta1 = TransitionApprovalMetaFactory.create(
workflow=workflow,
source_state=state1,
destination_state=state2,
priority=0,
)

meta2 = TransitionApprovalMetaFactory.create(
workflow=workflow,
source_state=state1,
destination_state=state3,
priority=0,
)

workflow_object = BasicTestModelObjectFactory()

assert_that(workflow_object.model.my_field, equal_to(state1))
next_approvals = workflow_object.model.river.my_field.next_approvals
assert_that(next_approvals, has_length(2))
assert_that(next_approvals, has_item(meta1.transition_approvals.first()))
assert_that(next_approvals, has_item(meta2.transition_approvals.first()))

0 comments on commit af99b85

Please sign in to comment.