2020-08-21 22:42:15 +00:00
|
|
|
"""Flow exporter"""
|
|
|
|
from json import dumps
|
2020-09-09 15:22:24 +00:00
|
|
|
from typing import Iterator, List
|
|
|
|
from uuid import UUID
|
|
|
|
|
|
|
|
from django.db.models import Q
|
2020-08-21 22:42:15 +00:00
|
|
|
|
|
|
|
from passbook.flows.models import Flow, FlowStageBinding, Stage
|
|
|
|
from passbook.flows.transfer.common import DataclassEncoder, FlowBundle, FlowBundleEntry
|
|
|
|
from passbook.policies.models import Policy, PolicyBinding, PolicyBindingModel
|
|
|
|
from passbook.stages.prompt.models import PromptStage
|
|
|
|
|
|
|
|
|
|
|
|
class FlowExporter:
|
|
|
|
"""Export flow with attached stages into json"""
|
|
|
|
|
|
|
|
flow: Flow
|
|
|
|
with_policies: bool
|
|
|
|
with_stage_prompts: bool
|
|
|
|
|
2020-09-09 15:22:24 +00:00
|
|
|
pbm_uuids: List[UUID]
|
|
|
|
|
2020-08-21 22:42:15 +00:00
|
|
|
def __init__(self, flow: Flow):
|
|
|
|
self.flow = flow
|
|
|
|
self.with_policies = True
|
|
|
|
self.with_stage_prompts = True
|
|
|
|
|
2020-09-09 15:22:24 +00:00
|
|
|
def _prepare_pbm(self):
|
|
|
|
self.pbm_uuids = [self.flow.pbm_uuid]
|
|
|
|
for stage_subclass in Stage.__subclasses__():
|
|
|
|
if issubclass(stage_subclass, PolicyBindingModel):
|
|
|
|
self.pbm_uuids += stage_subclass.objects.filter(
|
|
|
|
flow=self.flow
|
|
|
|
).values_list("pbm_uuid", flat=True)
|
|
|
|
self.pbm_uuids += FlowStageBinding.objects.filter(target=self.flow).values_list(
|
|
|
|
"pbm_uuid", flat=True
|
|
|
|
)
|
|
|
|
|
2020-08-21 22:42:15 +00:00
|
|
|
def walk_stages(self) -> Iterator[FlowBundleEntry]:
|
|
|
|
"""Convert all stages attached to self.flow into FlowBundleEntry objects"""
|
|
|
|
stages = (
|
|
|
|
Stage.objects.filter(flow=self.flow).select_related().select_subclasses()
|
|
|
|
)
|
|
|
|
for stage in stages:
|
|
|
|
if isinstance(stage, PromptStage):
|
|
|
|
pass
|
2020-09-05 23:07:06 +00:00
|
|
|
yield FlowBundleEntry.from_model(stage, "name")
|
2020-08-21 22:42:15 +00:00
|
|
|
|
|
|
|
def walk_stage_bindings(self) -> Iterator[FlowBundleEntry]:
|
|
|
|
"""Convert all bindings attached to self.flow into FlowBundleEntry objects"""
|
|
|
|
bindings = FlowStageBinding.objects.filter(target=self.flow).select_related()
|
|
|
|
for binding in bindings:
|
2020-09-05 23:07:06 +00:00
|
|
|
yield FlowBundleEntry.from_model(binding, "target", "stage", "order")
|
2020-08-21 22:42:15 +00:00
|
|
|
|
|
|
|
def walk_policies(self) -> Iterator[FlowBundleEntry]:
|
2020-09-09 15:22:24 +00:00
|
|
|
"""Walk over all policies. This is done at the beginning of the export for stages that have
|
|
|
|
a direct foreign key to a policy."""
|
|
|
|
# Special case for PromptStage as that has a direct M2M to policy, we have to ensure
|
|
|
|
# all policies referenced in there we also include here
|
|
|
|
prompt_stages = PromptStage.objects.filter(flow=self.flow).values_list(
|
|
|
|
"pk", flat=True
|
2020-08-21 22:42:15 +00:00
|
|
|
)
|
2020-09-09 15:22:24 +00:00
|
|
|
query = Q(bindings__in=self.pbm_uuids) | Q(promptstage__in=prompt_stages)
|
|
|
|
policies = Policy.objects.filter(query).select_related()
|
2020-08-21 22:42:15 +00:00
|
|
|
for policy in policies:
|
|
|
|
yield FlowBundleEntry.from_model(policy)
|
2020-09-09 15:22:24 +00:00
|
|
|
|
|
|
|
def walk_policy_bindings(self) -> Iterator[FlowBundleEntry]:
|
|
|
|
"""Walk over all policybindings relative to us. This is run at the end of the export, as
|
|
|
|
we are sure all objects exist now."""
|
|
|
|
bindings = PolicyBinding.objects.filter(
|
|
|
|
target__in=self.pbm_uuids
|
|
|
|
).select_related()
|
2020-08-21 22:42:15 +00:00
|
|
|
for binding in bindings:
|
2020-09-07 09:25:59 +00:00
|
|
|
yield FlowBundleEntry.from_model(binding, "policy", "target", "order")
|
2020-08-21 22:42:15 +00:00
|
|
|
|
|
|
|
def walk_stage_prompts(self) -> Iterator[FlowBundleEntry]:
|
|
|
|
"""Walk over all prompts associated with any PromptStages"""
|
|
|
|
prompt_stages = PromptStage.objects.filter(flow=self.flow)
|
|
|
|
for stage in prompt_stages:
|
|
|
|
for prompt in stage.fields.all():
|
|
|
|
yield FlowBundleEntry.from_model(prompt)
|
|
|
|
|
|
|
|
def export(self) -> FlowBundle:
|
|
|
|
"""Create a list of all objects including the flow"""
|
2020-09-09 15:22:24 +00:00
|
|
|
if self.with_policies:
|
|
|
|
self._prepare_pbm()
|
2020-08-21 22:42:15 +00:00
|
|
|
bundle = FlowBundle()
|
2020-09-05 23:07:06 +00:00
|
|
|
bundle.entries.append(FlowBundleEntry.from_model(self.flow, "slug"))
|
2020-08-21 22:42:15 +00:00
|
|
|
if self.with_stage_prompts:
|
|
|
|
bundle.entries.extend(self.walk_stage_prompts())
|
2020-09-09 15:22:24 +00:00
|
|
|
if self.with_policies:
|
|
|
|
bundle.entries.extend(self.walk_policies())
|
2020-08-21 22:42:15 +00:00
|
|
|
bundle.entries.extend(self.walk_stages())
|
|
|
|
bundle.entries.extend(self.walk_stage_bindings())
|
|
|
|
if self.with_policies:
|
2020-09-09 15:22:24 +00:00
|
|
|
bundle.entries.extend(self.walk_policy_bindings())
|
2020-08-21 22:42:15 +00:00
|
|
|
return bundle
|
|
|
|
|
|
|
|
def export_to_string(self) -> str:
|
|
|
|
"""Call export and convert it to json"""
|
|
|
|
return dumps(self.export(), cls=DataclassEncoder)
|