Esempio n. 1
0
def post_block(
        routing_path,
        schema,
        metadata,
        collection_metadata,
        answer_store,
        eq_id,
        form_type,
        collection_id,
        group_id,  # pylint: disable=too-many-locals
        group_instance,
        block_id):
    current_location = Location(group_id, group_instance, block_id)
    completeness = get_completeness(current_user)
    router = Router(schema, routing_path, completeness, current_location)

    if not router.can_access_location():
        next_location = router.get_next_location()
        return _redirect_to_location(collection_id, eq_id, form_type,
                                     next_location)

    block = _get_block_json(current_location, schema, answer_store, metadata)

    schema_context = _get_schema_context(routing_path, current_location,
                                         metadata, collection_metadata,
                                         answer_store, schema)

    rendered_block = renderer.render(block, **schema_context)

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

    if 'action[save_sign_out]' in request.form:
        return _save_sign_out(routing_path, current_location, form, schema,
                              answer_store, metadata)

    if form.validate():
        _set_started_at_metadata_if_required(form, collection_metadata)
        _update_questionnaire_store(current_location, form, schema)
        next_location = path_finder.get_next_location(
            current_location=current_location)

        if _is_end_of_questionnaire(block, next_location):
            return submit_answers(routing_path, eq_id, form_type, schema)

        return redirect(_next_location_url(next_location))

    context = build_view_context(block['type'], metadata, schema, answer_store,
                                 schema_context, rendered_block,
                                 current_location, form)

    return _render_page(block['type'], context, current_location, schema,
                        answer_store, metadata, routing_path)
Esempio n. 2
0
def post_household_composition(routing_path, schema, metadata, answer_store,
                               **kwargs):  # pylint: disable=too-many-locals
    group_id = kwargs['group_id']
    current_location = Location(group_id, 0, 'household-composition')
    questionnaire_store = get_questionnaire_store(current_user.user_id,
                                                  current_user.user_ik)

    answer_store_updater = AnswerStoreUpdater(current_location, schema,
                                              questionnaire_store)
    answer_store_updater.remove_repeats_for_changed_household_answers(
        request.form.copy())

    disable_mandatory = any(x in request.form for x in [
        'action[add_answer]', 'action[remove_answer]', 'action[save_sign_out]'
    ])

    block = _get_block_json(current_location, schema, answer_store, metadata)

    form = post_form_for_location(schema,
                                  block,
                                  current_location,
                                  answer_store,
                                  metadata,
                                  request.form,
                                  disable_mandatory=disable_mandatory)

    form.validate(
    )  # call validate here to keep errors in the form object on the context
    context = _get_context(routing_path, block, current_location, schema, form)

    if 'action[add_answer]' in request.form:
        form.household.append_entry()

        return _render_page(block['type'], context, current_location, schema,
                            answer_store, metadata, routing_path)

    if 'action[remove_answer]' in request.form:
        index_to_remove = int(request.form.get('action[remove_answer]'))
        form.remove_person(index_to_remove)

        return _render_page(block['type'], context, current_location, schema,
                            answer_store, metadata, routing_path)

    if 'action[save_sign_out]' in request.form:
        response = _save_sign_out(routing_path, current_location, form, schema,
                                  answer_store, metadata)
        answer_store_updater.remove_empty_household_members()

        return response

    if form.validate():
        answer_store_updater.save_answers(form)

        metadata = get_metadata(current_user)
        next_location = path_finder.get_next_location(
            current_location=current_location)

        return redirect(next_location.url(metadata))

    return _render_page(block['type'], context, current_location, schema,
                        answer_store, metadata, routing_path)
Esempio n. 3
0
def post_household_composition(routing_path, schema, metadata, answer_store,
                               **kwargs):
    group_id = kwargs['group_id']

    if _household_answers_changed(answer_store, schema):
        _remove_repeating_on_household_answers(answer_store, schema)

    disable_mandatory = any(x in request.form for x in [
        'action[add_answer]', 'action[remove_answer]', 'action[save_sign_out]'
    ])

    current_location = Location(group_id, 0, 'household-composition')

    block = _get_block_json(current_location, schema, answer_store, metadata)

    form = post_form_for_location(schema,
                                  block,
                                  current_location,
                                  answer_store,
                                  metadata,
                                  request.form,
                                  disable_mandatory=disable_mandatory)

    form.validate(
    )  # call validate here to keep errors in the form object on the context
    context = _get_context(routing_path, block, current_location, schema, form)

    if 'action[add_answer]' in request.form:
        form.household.append_entry()

        return _render_page(block['type'], context, current_location, schema,
                            answer_store, metadata, routing_path)

    if 'action[remove_answer]' in request.form:
        index_to_remove = int(request.form.get('action[remove_answer]'))
        form.remove_person(index_to_remove)

        return _render_page(block['type'], context, current_location, schema,
                            answer_store, metadata, routing_path)

    if 'action[save_sign_out]' in request.form:
        response = _save_sign_out(routing_path, current_location, form, schema,
                                  answer_store, metadata)
        remove_empty_household_members_from_answer_store(answer_store, schema)

        return response

    if form.validate():
        questionnaire_store = get_questionnaire_store(current_user.user_id,
                                                      current_user.user_ik)
        update_questionnaire_store_with_answer_data(questionnaire_store,
                                                    current_location,
                                                    form.serialise(), schema)

        metadata = get_metadata(current_user)
        next_location = path_finder.get_next_location(
            current_location=current_location)

        return redirect(next_location.url(metadata))

    return _render_page(block['type'], context, current_location, schema,
                        answer_store, metadata, routing_path)