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

View File

@ -65,7 +65,8 @@ class PromptChallengeResponse(ChallengeResponse):
fields = list(self.stage.fields.all()) fields = list(self.stage.fields.all())
for field in fields: for field in fields:
field: Prompt 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 # Special handling for fields with username type
# these check for existing users with the same username # these check for existing users with the same username
if field.type == FieldTypes.USERNAME: if field.type == FieldTypes.USERNAME: