Exemplo n.º 1
0
    def _is_section_enabled(self, section: Mapping) -> bool:
        if "enabled" not in section:
            return True

        enabled = section["enabled"]
        if isinstance(enabled, dict):
            when_rule_evaluator = RuleEvaluator(
                self._schema,
                self._answer_store,
                self._list_store,
                self._metadata,
                self._response_metadata,
                location=None,
                routing_path_block_ids=None,
            )

            return bool(when_rule_evaluator.evaluate(enabled["when"]))

        return any(
            evaluate_when_rules(
                condition["when"],
                self._schema,
                self._metadata,
                self._answer_store,
                self._list_store,
            ) for condition in enabled)
Exemplo n.º 2
0
def get_rule_evaluator(
    *,
    language="en",
    schema: QuestionnaireSchema = None,
    answer_store: AnswerStore = AnswerStore(),
    list_store: ListStore = ListStore(),
    metadata: Optional[dict] = None,
    response_metadata: Mapping = None,
    location: Union[Location,
                    RelationshipLocation] = Location(section_id="test-section",
                                                     block_id="test-block"),
    routing_path_block_ids: Optional[list] = None,
):
    if not schema:
        schema = get_mock_schema()
        schema.is_repeating_answer = Mock(return_value=True)
        schema.get_default_answer = Mock(return_value=None)

    return RuleEvaluator(
        language=language,
        schema=schema,
        metadata=metadata or {},
        response_metadata=response_metadata,
        answer_store=answer_store,
        list_store=list_store,
        location=location,
        routing_path_block_ids=routing_path_block_ids,
    )
Exemplo n.º 3
0
def test_invalid_field_type_raises_on_invalid(answer_store, list_store):
    schema = QuestionnaireSchema({
        "questionnaire_flow": {
            "type": "Linear",
            "options": {
                "summary": {
                    "collapsible": False
                }
            },
        }
    })

    metadata = {
        "user_id": "789473423",
        "schema_name": "0000",
        "collection_exercise_sid": "test-sid",
        "period_id": "2016-02-01",
        "period_str": "2016-01-01",
        "ref_p_start_date": "2016-02-02",
        "ref_p_end_date": "2016-03-03",
        "ru_ref": "432423423423",
        "ru_name": "Apple",
        "return_by": "2016-07-07",
        "case_id": "1234567890",
        "case_ref": "1000000000000001",
    }

    response_metadata = {}

    value_source_resolver = ValueSourceResolver(
        answer_store=answer_store,
        list_store=list_store,
        metadata=metadata,
        response_metadata=response_metadata,
        schema=schema,
        location=None,
        list_item_id=None,
        escape_answer_values=False,
    )

    rule_evaluator = RuleEvaluator(
        answer_store=answer_store,
        list_store=list_store,
        metadata=metadata,
        response_metadata=response_metadata,
        schema=schema,
        location=None,
    )

    # Given
    invalid_field_type = "Football"
    # When / Then
    with pytest.raises(KeyError):
        get_field_handler(
            answer_schema={"type": invalid_field_type},
            value_source_resolver=value_source_resolver,
            rule_evaluator=rule_evaluator,
            error_messages=error_messages,
        )
Exemplo n.º 4
0
 def get_rule_evaluator(self):
     return RuleEvaluator(
         schema=self.schema,
         answer_store=self.answer_store,
         list_store=self.list_store,
         metadata=self.metadata,
         response_metadata=self.response_metadata,
         location=None,
     )
Exemplo n.º 5
0
def rule_evaluator(mock_schema, response_metadata):
    evaluator = RuleEvaluator(
        answer_store=AnswerStore(),
        list_store=ListStore(),
        metadata={},
        response_metadata=response_metadata,
        schema=mock_schema,
        location=None,
    )

    return evaluator
Exemplo n.º 6
0
def choose_variant(
    block,
    schema,
    metadata,
    response_metadata,
    answer_store,
    list_store,
    variants_key,
    single_key,
    current_location,
):
    if block.get(single_key):
        return block[single_key]
    for variant in block.get(variants_key, []):
        when_rules = variant.get("when", [])

        if isinstance(when_rules, dict):
            when_rule_evaluator = RuleEvaluator(
                schema,
                answer_store,
                list_store,
                metadata,
                response_metadata,
                location=current_location,
            )

            if when_rule_evaluator.evaluate(when_rules):
                return variant[single_key]
        elif evaluate_when_rules(
                when_rules,
                schema,
                metadata,
                answer_store,
                list_store,
                current_location=current_location,
        ):
            return variant[single_key]
Exemplo n.º 7
0
    def __init__(
        self,
        block_schema,
        *,
        answer_store,
        list_store,
        metadata,
        response_metadata,
        schema,
        location,
        return_to,
    ):
        self.id = block_schema["id"]
        self.title = block_schema.get("title")
        self.number = block_schema.get("number")

        self._rule_evaluator = RuleEvaluator(
            schema=schema,
            answer_store=answer_store,
            list_store=list_store,
            metadata=metadata,
            response_metadata=response_metadata,
            location=location,
        )

        self._value_source_resolver = ValueSourceResolver(
            answer_store=answer_store,
            list_store=list_store,
            metadata=metadata,
            response_metadata=response_metadata,
            schema=schema,
            location=location,
            list_item_id=location.list_item_id if location else None,
            use_default_answer=True,
        )

        self.question = self.get_question(
            block_schema=block_schema,
            answer_store=answer_store,
            list_store=list_store,
            metadata=metadata,
            response_metadata=response_metadata,
            schema=schema,
            location=location,
            return_to=return_to,
        )
    def _get_period_range_for_single_date(
        self,
        date_from: Mapping[str, dict],
        date_to: Mapping[str, dict],
    ) -> timedelta:

        list_item_id = self.location.list_item_id if self.location else None
        value_source_resolver = ValueSourceResolver(
            answer_store=self.answer_store,
            list_store=self.list_store,
            metadata=self.metadata,
            response_metadata=self.response_metadata,
            schema=self.schema,
            location=self.location,
            list_item_id=list_item_id,
            escape_answer_values=False,
        )

        rule_evaluator = RuleEvaluator(
            schema=self.schema,
            answer_store=self.answer_store,
            list_store=self.list_store,
            metadata=self.metadata,
            response_metadata=self.response_metadata,
            location=self.location,
        )

        handler = DateHandler(date_from, value_source_resolver, rule_evaluator,
                              error_messages)

        min_period_date = handler.get_date_value(
            "minimum") or handler.get_date_value("maximum")
        handler.answer_schema = date_to
        max_period_date = handler.get_date_value(
            "maximum") or handler.get_date_value("minimum")

        if not min_period_date or not max_period_date:
            raise ValueError("Period range must have a start and end date")

        # Work out the largest possible range, for date range question
        period_range = max_period_date - min_period_date

        return period_range
def get_answer_fields(
    question: QuestionSchema,
    data: Union[None, MultiDict[str, Any], Mapping[str, Any]],
    schema: QuestionnaireSchema,
    answer_store: AnswerStore,
    list_store: ListStore,
    metadata: Mapping[str, Any],
    response_metadata: Mapping[str, Any],
    location: Union[Location, RelationshipLocation, None],
) -> dict[str, FieldHandler]:
    list_item_id = location.list_item_id if location else None
    value_source_resolver = ValueSourceResolver(
        answer_store=answer_store,
        list_store=list_store,
        metadata=metadata,
        schema=schema,
        location=location,
        list_item_id=list_item_id,
        escape_answer_values=False,
        response_metadata=response_metadata,
    )

    rule_evaluator = RuleEvaluator(
        schema=schema,
        answer_store=answer_store,
        list_store=list_store,
        metadata=metadata,
        response_metadata=response_metadata,
        location=location,
    )

    answer_fields = {}
    question_title = question.get("title")
    for answer in question.get("answers", []):
        for option in answer.get("options", []):
            if "detail_answer" in option:
                if data:
                    disable_validation = not _option_value_in_data(
                        answer, option, data)
                else:
                    disable_validation = True

                detail_answer = option["detail_answer"]

                answer_fields[option["detail_answer"]
                              ["id"]] = get_field_handler(
                                  answer_schema=detail_answer,
                                  value_source_resolver=value_source_resolver,
                                  rule_evaluator=rule_evaluator,
                                  error_messages=schema.error_messages,
                                  disable_validation=disable_validation,
                                  question_title=question_title,
                              ).get_field()
        answer_fields[answer["id"]] = get_field_handler(
            answer_schema=answer,
            value_source_resolver=value_source_resolver,
            rule_evaluator=rule_evaluator,
            error_messages=schema.error_messages,
            question_title=question_title,
        ).get_field()

    return answer_fields