core: refactor MessageStage to not use dynamic class

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2022-10-26 20:01:42 +02:00
parent 9b6e07de17
commit f8fab14e1e

View file

@ -45,22 +45,18 @@ class Action(Enum):
DENY = "deny" DENY = "deny"
def message_stage(message: str, level: int) -> StageView: class MessageStage(StageView):
"""Show a pre-configured message after the flow is done""" """Show a pre-configured message after the flow is done"""
class MessageStage(StageView):
"""Show a pre-configured message after the flow is done"""
message: str
level: int
# pylint: disable=unused-argument # pylint: disable=unused-argument
def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
"""Show a pre-configured message after the flow is done""" """Show a pre-configured message after the flow is done"""
message = getattr(self.executor.current_stage, "message", "")
level = getattr(self.executor.current_stage, "level", messages.SUCCESS)
messages.add_message( messages.add_message(
self.request, self.request,
self.level, level,
self.message, message,
) )
return self.executor.stage_ok() return self.executor.stage_ok()
@ -68,10 +64,6 @@ def message_stage(message: str, level: int) -> StageView:
"""Wrapper for post requests""" """Wrapper for post requests"""
return self.get(request) return self.get(request)
MessageStage.message = message
MessageStage.level = level
return MessageStage
class SourceFlowManager: class SourceFlowManager:
"""Help sources decide what they should do after authorization. Based on source settings and """Help sources decide what they should do after authorization. Based on source settings and
@ -284,14 +276,11 @@ class SourceFlowManager:
connection, connection,
stages=[ stages=[
in_memory_stage( in_memory_stage(
message_stage( MessageStage,
messages.SUCCESS, message=_(
_( "Successfully authenticated with %(source)s!" % {"source": self.source.name}
"Successfully authenticated with %(source)s!"
% {"source": self.source.name}
), ),
) )
)
], ],
**flow_kwargs, **flow_kwargs,
) )
@ -339,14 +328,11 @@ class SourceFlowManager:
connection, connection,
stages=[ stages=[
in_memory_stage( in_memory_stage(
message_stage( MessageStage,
messages.SUCCESS, message=_(
_( "Successfully authenticated with %(source)s!" % {"source": self.source.name}
"Successfully authenticated with %(source)s!"
% {"source": self.source.name}
), ),
) )
)
], ],
**{ **{
PLAN_CONTEXT_PROMPT: delete_none_keys(self.enroll_info), PLAN_CONTEXT_PROMPT: delete_none_keys(self.enroll_info),