def test_get_handler_invalid_block(self):
     schema = Mock()
     schema.get_block = Mock(return_value=None)
     with self.assertRaises(InvalidLocationException):
         get_block_handler(
             schema=schema,
             block_id="invalid-block-id",
             list_item_id=None,
             questionnaire_store=None,
             language=None,
         )
    def test_get_handler_invalid_location_missing_list_item_id_for_repeat(self):
        schema = Mock()
        schema.get_block = Mock(return_value={"id": "some-block", "type": "Question"})
        schema.is_block_in_repeating_section = Mock(return_value=True)

        with self.assertRaises(InvalidLocationException):
            get_block_handler(
                schema=schema,
                block_id="some-block",
                list_item_id=None,
                list_name="people",
                questionnaire_store=None,
                language=None,
            )
    def test_get_handler_invalid_block_type(self):
        schema = Mock()
        schema.get_block = Mock(return_value={"id": "some-block", "type": "MadeUpType"})
        schema.is_block_in_repeating_section = Mock(return_value=False)

        with self.assertRaises(ValueError):
            get_block_handler(
                schema=schema,
                block_id="some-block",
                list_item_id=None,
                list_name="",
                questionnaire_store=None,
                language=None,
            )
Esempio n. 4
0
def relationship(schema, questionnaire_store, block_id, list_item_id, to_list_item_id):
    try:
        block_handler = get_block_handler(
            schema=schema,
            block_id=block_id,
            list_item_id=list_item_id,
            to_list_item_id=to_list_item_id,
            questionnaire_store=questionnaire_store,
            language=flask_babel.get_locale().language,
        )
    except InvalidLocationException:
        return redirect(url_for(".get_questionnaire"))

    block_handler.form = _generate_wtf_form(
        block_handler.rendered_block, schema, block_handler.current_location
    )
    if request.method == "GET" or not block_handler.form.validate():
        return _render_page(
            block_type=block_handler.block["type"],
            context=block_handler.get_context(),
            current_location=block_handler.current_location,
            previous_location_url=block_handler.get_previous_location_url(),
            schema=schema,
            page_title=block_handler.page_title,
        )

    if "action[save_sign_out]" in request.form:
        block_handler.save_on_sign_out()
        return redirect(url_for("session.get_sign_out"))

    block_handler.handle_post()
    next_location_url = block_handler.get_next_location_url()
    return redirect(next_location_url)
def block(schema, questionnaire_store, block_id, list_name=None, list_item_id=None):
    try:
        block_handler = get_block_handler(
            schema=schema,
            block_id=block_id,
            list_name=list_name,
            list_item_id=list_item_id,
            questionnaire_store=questionnaire_store,
            language=flask_babel.get_locale().language,
            request_args=request.args,
            form_data=request.form,
        )
    except InvalidLocationException as exc:
        raise NotFound from exc

    if "action[clear_radios]" in request.form:
        block_handler.clear_radio_answers()
        return redirect(block_handler.current_location.url())

    if request.method != "POST" or (
        hasattr(block_handler, "form") and not block_handler.form.validate()
    ):
        return _render_page(
            template=block_handler.rendered_block["type"],
            context=block_handler.get_context(),
            previous_location_url=block_handler.get_previous_location_url(),
            schema=schema,
            page_title=block_handler.page_title,
        )

    block_handler.handle_post()
    next_location_url = block_handler.get_next_location_url()
    return redirect(next_location_url)
def relationship(schema, questionnaire_store, block_id, list_item_id,
                 to_list_item_id):
    try:
        block_handler = get_block_handler(
            schema=schema,
            block_id=block_id,
            list_item_id=list_item_id,
            to_list_item_id=to_list_item_id,
            questionnaire_store=questionnaire_store,
            language=flask_babel.get_locale().language,
            list_name=schema.get_block(block_id)["for_list"],
            request_args=request.args,
            form_data=request.form,
        )
    except InvalidLocationException:
        raise NotFound

    if request.method == "GET" or (hasattr(block_handler, "form")
                                   and not block_handler.form.validate()):
        return _render_page(
            template=block_handler.block["type"],
            context=block_handler.get_context(),
            previous_location_url=block_handler.get_previous_location_url(),
            schema=schema,
            page_title=block_handler.page_title,
        )

    block_handler.handle_post()
    next_location_url = block_handler.get_next_location_url()
    return redirect(next_location_url)
Esempio n. 7
0
def block(schema,
          questionnaire_store,
          block_id,
          list_name=None,
          list_item_id=None):
    try:
        block_handler = get_block_handler(
            schema=schema,
            block_id=block_id,
            list_name=list_name,
            list_item_id=list_item_id,
            questionnaire_store=questionnaire_store,
            language=flask_babel.get_locale().language,
            request_args=request.args,
        )
    except InvalidLocationException:
        return redirect(url_for(".get_questionnaire"))

    if block_handler.block["type"] == "RelationshipCollector":
        return redirect(block_handler.get_first_location_url())

    if "action[sign_out]" in request.form:
        return redirect(url_for("session.get_sign_out"))

    if "action[clear_radios]" in request.form:
        block_handler.clear_radio_answers()
        return redirect(request.url)

    block_handler.form = _generate_wtf_form(block_handler.rendered_block,
                                            schema,
                                            block_handler.current_location)

    if request.method == "GET" or not block_handler.form.validate():
        return _render_page(
            block_type=block_handler.rendered_block["type"],
            context=block_handler.get_context(),
            current_location=block_handler.current_location,
            previous_location_url=block_handler.get_previous_location_url(),
            schema=schema,
            page_title=block_handler.page_title,
        )

    if "action[save_sign_out]" in request.form:
        block_handler.save_on_sign_out()
        return redirect(url_for("session.get_sign_out"))

    if block_handler.block["type"] in END_BLOCKS:
        return submit_answers(schema, questionnaire_store,
                              block_handler.router.full_routing_path())

    if block_handler.form.data:
        block_handler.set_started_at_metadata()

    block_handler.handle_post()

    next_location_url = block_handler.get_next_location_url()
    return redirect(next_location_url)
def block(schema,
          questionnaire_store,
          block_id,
          list_name=None,
          list_item_id=None):
    try:
        block_handler = get_block_handler(
            schema=schema,
            block_id=block_id,
            list_name=list_name,
            list_item_id=list_item_id,
            questionnaire_store=questionnaire_store,
            language=flask_babel.get_locale().language,
            request_args=request.args,
            form_data=request.form,
        )
    except InvalidLocationException:
        raise NotFound

    if block_handler.block["type"] == "RelationshipCollector":
        return redirect(block_handler.get_first_location_url())

    if "action[clear_radios]" in request.form:
        block_handler.clear_radio_answers()
        return redirect(request.url)

    if request.method == "GET" or (hasattr(block_handler, "form")
                                   and not block_handler.form.validate()):
        return _render_page(
            template=block_handler.rendered_block["type"],
            context=block_handler.get_context(),
            previous_location_url=block_handler.get_previous_location_url(),
            schema=schema,
            page_title=block_handler.page_title,
        )

    if block_handler.block["type"] in END_BLOCKS:
        submission_handler = SubmissionHandler(
            schema, questionnaire_store,
            block_handler.router.full_routing_path())
        submission_handler.submit_questionnaire()

        language_code = get_session_store().session_data.language_code
        if "census" in cookie_session["theme"]:
            log_out_url = get_census_base_url(cookie_session["theme"],
                                              language_code)
            cookie_session["account_service_log_out_url"] = log_out_url

        return redirect(url_for("post_submission.get_thank_you"))

    block_handler.handle_post()

    next_location_url = block_handler.get_next_location_url()
    return redirect(next_location_url)
def relationships(
    schema,
    questionnaire_store,
    list_name=None,
    list_item_id=None,
    to_list_item_id=None,
    block_id="relationships",
):
    try:
        block_handler = get_block_handler(
            schema=schema,
            block_id=block_id,
            list_item_id=list_item_id,
            to_list_item_id=to_list_item_id,
            questionnaire_store=questionnaire_store,
            list_name=list_name,
            language=flask_babel.get_locale().language,
            request_args=request.args,
            form_data=request.form,
        )
    except InvalidLocationException as exc:
        raise NotFound from exc

    if not list_name:
        if "last" in request.args:
            return redirect(block_handler.get_last_location_url())
        return redirect(block_handler.get_first_location_url())

    if request.method != "POST" or (
        hasattr(block_handler, "form") and not block_handler.form.validate()
    ):
        return _render_page(
            template=block_handler.block["type"],
            context=block_handler.get_context(),
            previous_location_url=block_handler.get_previous_location_url(),
            schema=schema,
            page_title=block_handler.page_title,
        )

    block_handler.handle_post()
    next_location_url = block_handler.get_next_location_url()
    return redirect(next_location_url)