flows: update work with new stages

This commit is contained in:
Jens Langhammer 2020-07-20 16:33:34 +02:00
parent a3d92ebc0a
commit c9663a08da
2 changed files with 12 additions and 7 deletions

View File

@ -3,13 +3,13 @@ from typing import TYPE_CHECKING, Optional, Type
from uuid import uuid4
from django.db import models
from django.forms import ModelForm
from django.http import HttpRequest
from django.utils.translation import gettext_lazy as _
from model_utils.managers import InheritanceManager
from structlog import get_logger
from passbook.core.types import UIUserSettings
from passbook.lib.utils.reflection import class_to_path
from passbook.policies.models import PolicyBindingModel
if TYPE_CHECKING:
@ -47,8 +47,14 @@ class Stage(models.Model):
name = models.TextField()
objects = InheritanceManager()
type = ""
form = ""
def type(self) -> Type["StageView"]:
"""Return StageView class that implements logic for this stage"""
raise NotImplementedError
def form(self) -> Type[ModelForm]:
"""Return Form class used to edit this object"""
raise NotImplementedError
@property
def ui_user_settings(self) -> Optional[UIUserSettings]:
@ -62,9 +68,8 @@ class Stage(models.Model):
def in_memory_stage(view: Type["StageView"]) -> Stage:
"""Creates an in-memory stage instance, based on a `_type` as view."""
class_path = class_to_path(view)
stage = Stage()
stage.type = class_path
setattr(stage, "type", lambda self: view)
return stage

View File

@ -21,7 +21,7 @@ from passbook.core.views.utils import PermissionDeniedView
from passbook.flows.exceptions import EmptyFlowException, FlowNonApplicableException
from passbook.flows.models import Flow, FlowDesignation, Stage
from passbook.flows.planner import FlowPlan, FlowPlanner
from passbook.lib.utils.reflection import class_to_path, path_to_class
from passbook.lib.utils.reflection import class_to_path
from passbook.lib.utils.urls import is_url_absolute, redirect_with_qs
from passbook.lib.views import bad_request_message
@ -94,7 +94,7 @@ class FlowExecutorView(View):
if not self.current_stage:
LOGGER.debug("f(exec): no more stages, flow is done.")
return self._flow_done()
stage_cls = path_to_class(self.current_stage.type)
stage_cls = self.current_stage.type()
self.current_stage_view = stage_cls(self)
self.current_stage_view.args = self.args
self.current_stage_view.kwargs = self.kwargs