stages/prompt: only set placeholder when in context

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-11-08 15:10:27 +01:00
parent 133bedafba
commit 99ef94b7aa
2 changed files with 10 additions and 5 deletions

View file

@ -13,6 +13,7 @@ from rest_framework.fields import (
EmailField,
HiddenField,
IntegerField,
ReadOnlyField,
)
from rest_framework.serializers import BaseSerializer
@ -85,6 +86,8 @@ class Prompt(SerializerModel):
"required": self.required,
}
if self.type == FieldTypes.TEXT_READ_ONLY:
field_class = ReadOnlyField
if self.type == FieldTypes.EMAIL:
field_class = EmailField
if self.type == FieldTypes.NUMBER:
@ -101,14 +104,14 @@ class Prompt(SerializerModel):
if self.type == FieldTypes.DATE_TIME:
field_class = DateTimeField
if self.type == FieldTypes.STATIC:
kwargs["initial"] = self.placeholder
kwargs["default"] = self.placeholder
kwargs["required"] = False
kwargs["label"] = ""
if self.type == FieldTypes.SEPARATOR:
kwargs["required"] = False
kwargs["label"] = ""
if default:
kwargs["initial"] = default
kwargs["default"] = default
return field_class(**kwargs)
def save(self, *args, **kwargs):

View file

@ -100,7 +100,8 @@ class PromptChallengeResponse(ChallengeResponse):
type__in=[FieldTypes.HIDDEN, FieldTypes.STATIC, FieldTypes.TEXT_READ_ONLY]
)
for static_hidden in static_hidden_fields:
attrs[static_hidden.field_key] = self.fields[static_hidden.field_key].initial
field = self.fields[static_hidden.field_key]
attrs[static_hidden.field_key] = field.default
# Check if we have two password fields, and make sure they are the same
password_fields: QuerySet[Prompt] = self.stage.fields.filter(type=FieldTypes.PASSWORD)
@ -165,10 +166,11 @@ class PromptStageView(ChallengeStageView):
def get_challenge(self, *args, **kwargs) -> Challenge:
fields = list(self.executor.current_stage.fields.all().order_by("order"))
serializers = []
context_prompt = self.executor.plan.get(PLAN_CONTEXT_PROMPT, {})
context_prompt = self.executor.plan.context.get(PLAN_CONTEXT_PROMPT, {})
for field in fields:
data = StagePromptSerializer(field).data
data["placeholder"] = context_prompt.get(field.field_key)
if field.field_key in context_prompt:
data["placeholder"] = context_prompt.get(field.field_key)
serializers.append(data)
challenge = PromptChallenge(
data={