def test_mandatory_mutually_exclusive_question_raises_error_with_question_text(
        self,
    ):
        with self.app_request_context():
            schema = load_schema_from_name("test_question_title_in_error")

            question_schema = schema.get_block("mutually-exclusive-checkbox").get(
                "question"
            )
            answer_store = AnswerStore(
                [{"answer_id": "mandatory-checkbox-answer", "value": ["Tuna"]}]
            )

            renderer = PlaceholderRenderer(
                language="en", schema=schema, answer_store=answer_store
            )
            rendered_schema = renderer.render(question_schema, None)

            form = generate_form(
                schema,
                rendered_schema,
                answer_store,
                metadata=None,
                form_data=MultiDict(),
            )
            form.validate_mutually_exclusive_question(question_schema)
            error = form.question_errors["mutually-exclusive-checkbox-question"]

            assert error == format_message_with_title(
                error_messages["MANDATORY_CHECKBOX"],
                "Did you really answer ‘Tuna’ to the previous question?",
            )
Пример #2
0
    def test_renders_json_uses_language(self):
        mock_transform = {
            "transform": "calculate_date_difference",
            "arguments": {
                "first_date": {
                    "source": "answers",
                    "identifier": "date-of-birth-answer",
                },
                "second_date": {"value": "2019-02-01"},
            },
        }
        json_to_render = self.question_json.copy()
        json_to_render["answers"][0]["options"][0]["label"]["placeholders"][1][
            "transforms"
        ][0] = mock_transform

        renderer = PlaceholderRenderer(
            language="cy",
            schema=QuestionnaireSchema({}),
            answer_store=AnswerStore(
                [
                    {"answer_id": "first-name", "value": "Alfred"},
                    {"answer_id": "last-name", "value": "Aho"},
                    {"answer_id": "date-of-birth-answer", "value": "1986-01-01"},
                ]
            ),
        )

        rendered_schema = renderer.render(json_to_render, list_item_id=None)
        rendered_label = rendered_schema["answers"][0]["options"][0]["label"]

        assert rendered_label == "Alfred Aho age is 33 years. Is this correct?"
Пример #3
0
class Group:
    def __init__(
        self,
        group_schema,
        routing_path,
        answer_store,
        list_store,
        metadata,
        schema,
        location,
        language,
    ):
        self.id = group_schema["id"]

        self.title = group_schema.get("title")

        self.location = location

        self.blocks = self._build_blocks(
            group_schema,
            routing_path,
            answer_store,
            list_store,
            metadata,
            schema,
            location,
        )
        self.placeholder_renderer = PlaceholderRenderer(
            language=language,
            schema=schema,
            answer_store=answer_store,
            metadata=metadata,
        )

    @staticmethod
    def _build_blocks(group_schema, routing_path, answer_store, list_store,
                      metadata, schema, location):
        blocks = []

        for block in group_schema["blocks"]:
            if (block["id"] in routing_path and block["type"] == "Question"
                    and block.get("show_on_section_summary", True)):
                blocks.extend([
                    Block(block, answer_store, list_store, metadata, schema,
                          location).serialize()
                ])

        return blocks

    def serialize(self):
        return self.placeholder_renderer.render(
            {
                "id": self.id,
                "title": self.title,
                "blocks": self.blocks
            },
            self.location.list_item_id,
        )