stages/prompt: default prompts to the current value of the context

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-11-07 21:59:50 +01:00
parent c4e029ffe2
commit 5e23b11764
2 changed files with 10 additions and 6 deletions

View File

@ -1,5 +1,5 @@
"""prompt models"""
from typing import Type
from typing import Any, Optional, Type
from uuid import uuid4
from django.db import models
@ -74,13 +74,17 @@ class Prompt(SerializerModel):
return PromptSerializer
@property
def field(self) -> CharField:
def field(self, default: Optional[Any]) -> CharField:
"""Get field type for Challenge and response"""
field_class = CharField
_default = self.placeholder
if default:
_default = default
kwargs = {
"required": self.required,
"default": _default,
}
if self.type == FieldTypes.EMAIL:
field_class = EmailField
if self.type == FieldTypes.NUMBER:
@ -88,7 +92,6 @@ class Prompt(SerializerModel):
if self.type == FieldTypes.HIDDEN:
field_class = HiddenField
kwargs["required"] = False
kwargs["default"] = self.placeholder
if self.type == FieldTypes.CHECKBOX:
field_class = BooleanField
kwargs["required"] = False
@ -97,7 +100,7 @@ class Prompt(SerializerModel):
if self.type == FieldTypes.DATE_TIME:
field_class = DateTimeField
if self.type == FieldTypes.STATIC:
kwargs["initial"] = self.placeholder
kwargs["initial"] = _default
kwargs["required"] = False
kwargs["label"] = ""
if self.type == FieldTypes.SEPARATOR:

View File

@ -65,7 +65,8 @@ class PromptChallengeResponse(ChallengeResponse):
fields = list(self.stage.fields.all())
for field in fields:
field: Prompt
self.fields[field.field_key] = field.field
current = plan.context.get(PLAN_CONTEXT_PROMPT, {}).get(field.field_key)
self.fields[field.field_key] = field.field(current)
# Special handling for fields with username type
# these check for existing users with the same username
if field.type == FieldTypes.USERNAME: