コード例 #1
0
def get_view_submission(schema, eq_id, form_type):  # pylint: disable=unused-argument

    session_data = get_session_store().session_data

    if _is_submission_viewable(schema.json, session_data.submitted_time):
        submitted_data = data_access.get_by_key(SubmittedResponse,
                                                session_data.tx_id)

        if submitted_data:

            metadata_context = build_metadata_context_for_survey_completed(
                session_data)

            pepper = current_app.eq['secret_store'].get_secret_by_name(
                'EQ_SERVER_SIDE_STORAGE_ENCRYPTION_USER_PEPPER')
            encrypter = StorageEncryption(current_user.user_id,
                                          current_user.user_ik, pepper)

            submitted_data = json.loads(
                encrypter.decrypt_data(submitted_data.data))
            answer_store = AnswerStore(
                existing_answers=submitted_data.get('answers'))

            metadata = submitted_data.get('metadata')

            routing_path = PathFinder(schema, answer_store, metadata,
                                      []).get_full_routing_path()

            schema_context = _get_schema_context(routing_path, 0, metadata,
                                                 answer_store, schema)
            rendered_schema = renderer.render(schema.json, **schema_context)
            summary_rendered_context = build_summary_rendering_context(
                schema, rendered_schema['sections'], answer_store, metadata)

            context = {
                'summary': {
                    'groups':
                    summary_rendered_context,
                    'answers_are_editable':
                    False,
                    'is_view_submission_response_enabled':
                    is_view_submitted_response_enabled(schema.json),
                },
                'variables': None,
            }

            return render_theme_template(
                schema.json['theme'],
                template_name='view-submission.html',
                metadata=metadata_context,
                analytics_ua_id=current_app.config['EQ_UA_ID'],
                survey_id=schema.json['survey_id'],
                survey_title=TemplateRenderer.safe_content(
                    schema.json['title']),
                content=context)

    return redirect(
        url_for('post_submission.get_thank_you',
                eq_id=eq_id,
                form_type=form_type))
コード例 #2
0
def build_view_context_for_calculated_summary(metadata, schema, answer_store,
                                              schema_context, block_type,
                                              variables, current_location):
    block = schema.get_block(current_location.block_id)
    section_list = _build_calculated_summary_section_list(
        schema, block, current_location.group_id)

    context = build_view_context_for_summary(schema, section_list,
                                             answer_store, metadata,
                                             block_type, variables,
                                             schema_context)

    context['summary']['groups'] = [
        context['summary']['groups'][current_location.group_instance]
    ]
    schema_context['group_instance_id'] = get_group_instance_id(
        schema, answer_store, current_location)
    rendered_block = renderer.render(block, **schema_context)
    formatted_total = _get_formatted_total(context['summary'].get(
        'groups', []))

    context['summary'].update({
        'calculated_question':
        _get_calculated_question(rendered_block['calculation'], answer_store,
                                 schema, metadata,
                                 current_location.group_instance,
                                 formatted_total),
        'title':
        get_question_title(rendered_block, answer_store, schema, metadata,
                           current_location.group_instance) %
        dict(total=formatted_total),
    })
    return context
コード例 #3
0
def build_view_context_for_calculated_summary(metadata, schema, answer_store,
                                              schema_context, block_type,
                                              variables, csrf_token,
                                              current_location):
    rendered_block = renderer.render(
        schema.get_block(current_location.block_id), **schema_context)
    section_list = _build_calculated_summary_section_list(
        schema, rendered_block, current_location.group_id)

    context = build_view_context_for_summary(schema, section_list,
                                             answer_store, metadata,
                                             csrf_token, block_type, variables)

    formatted_total = _get_formatted_total(rendered_block, answer_store,
                                           current_location.group_instance,
                                           schema)
    context['summary'].update({
        'calculated_question':
        _get_calculated_question(rendered_block['calculation'], answer_store,
                                 schema, metadata,
                                 current_location.group_instance,
                                 formatted_total),
        'title':
        get_question_title(rendered_block, answer_store, schema, metadata,
                           current_location.group_instance) %
        dict(total=formatted_total),
    })
    return context
コード例 #4
0
def _render_schema(current_location):
    metadata = get_metadata(current_user)
    answer_store = get_answer_store(current_user)
    block_json = SchemaHelper.get_block_for_location(g.schema_json, current_location)
    block_json = _evaluate_skip_conditions(block_json, current_location, answer_store, metadata)
    aliases = SchemaHelper.get_aliases(g.schema_json)
    block_context = build_schema_context(metadata, aliases, answer_store, current_location.group_instance)
    return renderer.render(block_json, **block_context)
コード例 #5
0
    def serialize(self):
        schema_context_with_group_instance_id = deepcopy(self.schema_context)
        schema_context_with_group_instance_id['group_instance_id'] = self.group_instance_id

        return renderer.render(
            {
                'id': self.id,
                'title': self.title,
                'blocks': self.blocks,
            },
            **schema_context_with_group_instance_id
        )
コード例 #6
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)
コード例 #7
0
ファイル: questionnaire.py プロジェクト: qateam123/eq
def get_summary(eq_id, form_type, collection_id):  # pylint: disable=unused-argument
    answer_store = get_answer_store(current_user)
    path_finder = PathFinder(g.schema_json, answer_store, get_metadata(current_user))
    latest_location = path_finder.get_latest_location(get_completed_blocks(current_user))
    metadata = get_metadata(current_user)

    if latest_location.block_id is 'summary':
        answers = get_answer_store(current_user)
        schema_context = build_schema_context(metadata, g.schema.aliases, answers)
        rendered_schema_json = renderer.render(g.schema_json, **schema_context)
        summary_context = build_summary_rendering_context(rendered_schema_json, answer_store, metadata)
        return _build_template(current_location=latest_location, context=summary_context)

    return redirect(latest_location.url(metadata))
コード例 #8
0
def build_view_context_for_final_summary(metadata, schema, answer_store,
                                         schema_context, block_type, variables,
                                         csrf_token):
    section_list = renderer.render(schema.json, **schema_context)['sections']

    context = build_view_context_for_summary(schema, section_list,
                                             answer_store, metadata,
                                             csrf_token, block_type, variables)

    context['summary'].update({
        'is_view_submission_response_enabled':
        is_view_submitted_response_enabled(schema.json),
    })

    return context
コード例 #9
0
def build_view_context_for_section_summary(metadata, schema, answer_store,
                                           schema_context, block_type,
                                           variables, csrf_token, group_id):
    section_id = schema.get_group(group_id)['parent_id']
    section = schema.get_section(section_id)
    section_list = [renderer.render(section, **schema_context)]
    title = section['title']

    context = build_view_context_for_summary(schema, section_list,
                                             answer_store, metadata,
                                             csrf_token, block_type, variables)

    context['summary'].update({
        'title': title,
    })
    return context
コード例 #10
0
def build_view_context(block_type, metadata, schema, answer_store,
                       schema_context, rendered_block, current_location, form):
    variables = None
    if schema.json.get('variables'):
        variables = renderer.render(schema.json.get('variables'),
                                    **schema_context)

    if block_type == 'Summary':
        form = form or FlaskForm()
        return build_view_context_for_final_summary(metadata, schema,
                                                    answer_store,
                                                    schema_context, block_type,
                                                    variables, form.csrf_token,
                                                    rendered_block)
    if block_type == 'SectionSummary':
        form = form or FlaskForm()
        return build_view_context_for_section_summary(
            metadata, schema, answer_store, schema_context, block_type,
            variables, form.csrf_token, current_location.group_id)

    if block_type == 'CalculatedSummary':
        form = form or FlaskForm()
        return build_view_context_for_calculated_summary(
            metadata, schema, answer_store, schema_context, block_type,
            variables, form.csrf_token, current_location)

    if block_type == 'AnswerSummary':
        form = form or FlaskForm()
        return build_view_context_for_answer_summary(metadata, schema,
                                                     answer_store, block_type,
                                                     variables,
                                                     form.csrf_token,
                                                     current_location)

    if block_type in ('Question', 'ConfirmationQuestion'):
        form = form or get_form_for_location(
            schema, rendered_block, current_location, answer_store, metadata)
        return build_view_context_for_question(metadata, schema, answer_store,
                                               current_location, variables,
                                               rendered_block, form)

    if block_type in ('Introduction', 'Interstitial', 'Confirmation'):
        form = form or FlaskForm()
        return build_view_context_for_non_question(variables, form.csrf_token,
                                                   rendered_block)
コード例 #11
0
def build_relationship_choices(answer_ids,
                               answer_store,
                               group_instance,
                               member_label=None):  # pylint: disable=too-many-locals
    """
    A function to build a list of tuples of as yet undefined person relationships

    :param answer_ids: The answers that drive the relationship matrix
    :param answer_store: The answer store to use for current answers
    :param group_instance: The instance of the group being iterated over
    :param member_label: The label to override on the member name
    :return:
    """
    household_members = []

    answers = list(answer_store.filter(answer_ids=answer_ids, limit=True))

    for answer in answers:
        if member_label:
            context = {
                'answers':
                _build_answers(answer_store, answer['group_instance_id']),
            }

            rendered_label = renderer.render('{{' + member_label + '}}',
                                             **context)

            household_members.append(rendered_label)
        else:
            household_members.append(answer['value'])

    remaining_people = household_members[group_instance +
                                         1:] if group_instance < len(
                                             household_members) else []

    current_person_name = household_members[group_instance]

    choices = []

    for remaining_person in remaining_people:
        other_person_name = remaining_person
        choices.append((current_person_name, other_person_name))

    return choices
コード例 #12
0
def _get_context(full_routing_path,
                 block,
                 current_location,
                 schema,
                 form=None):
    metadata = get_metadata(current_user)
    answer_store = get_answer_store(current_user)
    schema_context = _get_schema_context(full_routing_path,
                                         current_location.group_instance,
                                         metadata, answer_store, schema)
    rendered_block = renderer.render(block, **schema_context)

    return build_view_context(block['type'],
                              metadata,
                              schema,
                              answer_store,
                              schema_context,
                              rendered_block,
                              current_location,
                              form=form)
コード例 #13
0
def _get_context(block, current_location, answer_store):

    error_messages = SchemaHelper.get_messages(g.schema_json)
    form, template_params = get_form_for_location(block, current_location,
                                                  answer_store, error_messages)
    content = {'form': form, 'block': block}
    if template_params:
        content.update(template_params)

    if block['type'] == 'Summary':
        metadata = get_metadata(current_user)
        aliases = SchemaHelper.get_aliases(g.schema_json)
        schema_context = build_schema_context(metadata, aliases, answer_store)
        rendered_schema_json = renderer.render(g.schema_json, **schema_context)
        content.update({
            'summary':
            build_summary_rendering_context(rendered_schema_json, answer_store,
                                            metadata)
        })

    return content
コード例 #14
0
def build_view_context_for_answer_summary(
        metadata,
        schema,
        answer_store,
        block_type,  # pylint: disable=too-many-locals
        variables,
        current_location):
    summary_block = schema.get_block(current_location.block_id)

    group = {
        'answers': [],
    }

    def group_instance(answer):
        return answer['group_instance']

    summary_answers = []

    for answer_id in summary_block.get('answer_ids'):
        answers = list(answer_store.filter(answer_ids=[answer_id]))
        answers.sort(key=group_instance)
        summary_answers.extend(answers)

    for answer in summary_answers:
        question_id = schema.get_answer(answer['answer_id']).get('parent_id')
        block_id = schema.get_question(question_id).get('parent_id')
        group_id = schema.get_block(block_id).get('parent_id')

        link = url_for('questionnaire.get_block',
                       eq_id=metadata['eq_id'],
                       form_type=metadata['form_type'],
                       collection_id=metadata['collection_exercise_sid'],
                       group_id=group_id,
                       group_instance=answer['group_instance'],
                       block_id=block_id)

        label = answer['value']

        if summary_block.get('answer_label'):
            render_context = {
                'answers':
                _build_answers(answer_store, answer.get('group_instance_id')),
            }

            label = renderer.render(
                '{{' + summary_block.get('answer_label') + '}}',
                **render_context)

        view_answer = {
            'id': answer['answer_id'],
            'label': label,
            'block': {
                'link': link,
            },
        }

        group['answers'].append(view_answer)

    return {
        'summary': {
            'title': summary_block.get('title'),
            'label': summary_block.get('label'),
            'groups': [group],
            'answers_are_editable': True,
            'summary_type': block_type,
            'icon': summary_block.get('icon'),
        },
        'variables': variables,
    }