def get_web_preview_context_from_project_data(manager, request, project_info):
    is_datasender = is_data_sender(request)
    form_model = get_questionnaire_form_model(manager, project_info,
                                              request.POST)
    questionnaire_form = SurveyResponseForm(form_model)
    return {
        'project': project_info,
        'is_datasender': is_datasender,
        'questionnaire_form': questionnaire_form
    }
Beispiel #2
0
def edit(request, project_id, survey_response_id, tab=0):
    manager = get_database_manager(request.user)
    questionnaire_form_model = Project.get(manager, project_id)
    dashboard_page = settings.HOME_PAGE + "?deleted=true"
    reporter_id = NGOUserProfile.objects.get(user=request.user).reporter_id
    is_linked = reporter_id in questionnaire_form_model.data_senders
    reporter_name = NGOUserProfile.objects.get(
        user=request.user).user.first_name
    if questionnaire_form_model.is_void():
        return HttpResponseRedirect(dashboard_page)

    disable_link_class, hide_link_class = get_visibility_settings_for(
        request.user)
    survey_response = get_survey_response_by_id(manager, survey_response_id)
    back_link = reverse(index,
                        kwargs={
                            "project_id": project_id,
                            "questionnaire_code":
                            questionnaire_form_model.form_code,
                            "tab": tab
                        })
    form_ui_model = build_static_info_context(
        manager,
        survey_response,
        questionnaire_form_model=questionnaire_form_model,
        reporter_id=reporter_id)
    form_ui_model.update({
        "back_link":
        back_link,
        'is_datasender':
        is_data_sender(request),
        'hide_change':
        questionnaire_form_model.is_poll
        and questionnaire_form_model.is_open_survey
    })
    data_sender = get_data_sender(manager, survey_response)
    short_code = data_sender[1]
    enable_datasender_edit = True if survey_response.owner_uid else False
    if request.method == 'GET':
        form_initial_values = construct_request_dict(survey_response,
                                                     questionnaire_form_model,
                                                     short_code)
        survey_response_form = SurveyResponseForm(
            questionnaire_form_model,
            form_initial_values,
            datasender_name=data_sender[0],
            reporter_id=reporter_id,
            reporter_name=reporter_name,
            enable_datasender_edit=enable_datasender_edit)

        form_ui_model.update(
            get_form_context(questionnaire_form_model, survey_response_form,
                             manager, hide_link_class, disable_link_class))
        form_ui_model.update({
            "redirect_url": "",
            "reporter_id": reporter_id,
            "is_linked": is_linked,
            "is_pro_sms": get_organization(request).is_pro_sms,
            "reporter_name": reporter_name
        })

        if not survey_response_form.is_valid(
        ) or form_ui_model['datasender_error_message']:
            error_message = _("Please check your answers below for errors.")
            form_ui_model.update({
                'error_message': error_message,
                "reporter_id": reporter_id,
                "is_linked": is_linked,
                "reporter_name": reporter_name
            })
        return render_to_response("project/web_questionnaire.html",
                                  form_ui_model,
                                  context_instance=RequestContext(request))

    if request.method == 'POST':
        send_to_carbon(create_path('submissions.web.simple'), 1)
        original_survey_response = survey_response.copy()
        is_errored_before_edit = True if survey_response.errors != '' else False
        submitted_values = request.POST
        owner_id = submitted_values.get("dsid")
        form_ui_model.update({
            "redirect_url":
            submitted_values.get("redirect_url"),
            'is_datasender':
            is_data_sender(request),
            "is_pro_sms":
            get_organization(request).is_pro_sms
        })
        form_ui_model.update(
            {"click_after_reload": submitted_values.get("click_after_reload")})
        if submitted_values.get("discard"):
            survey_response_form = SurveyResponseForm(questionnaire_form_model,
                                                      survey_response.values)

            form_ui_model.update(
                get_form_context(questionnaire_form_model,
                                 survey_response_form, manager,
                                 hide_link_class, disable_link_class))
            form_ui_model.update({
                "reporter_id": reporter_id,
                "is_linked": is_linked,
                "reporter_name": reporter_name
            })
            return render_to_response("project/web_questionnaire.html",
                                      form_ui_model,
                                      context_instance=RequestContext(request))
        else:
            form_initial_values = construct_request_dict(
                survey_response, questionnaire_form_model, short_code)
            if not owner_id:
                submitted_values = submitted_values.copy()
                submitted_values['dsid'] = form_initial_values['dsid']

            survey_response_form = SurveyResponseForm(
                questionnaire_form_model,
                submitted_values,
                initial=form_initial_values,
                enable_datasender_edit=enable_datasender_edit)

        form_ui_model.update(
            get_form_context(questionnaire_form_model, survey_response_form,
                             manager, hide_link_class, disable_link_class))
        form_ui_model.update({
            "reporter_id": reporter_id,
            "is_linked": is_linked
        })
        if not survey_response_form.is_valid():
            error_message = _("Please check your answers below for errors.")
            form_ui_model.update({
                'error_message': error_message,
                "reporter_id": reporter_id,
                "is_linked": is_linked
            })
            return render_to_response("project/web_questionnaire.html",
                                      form_ui_model,
                                      context_instance=RequestContext(request))

        success_message = _("Your changes have been saved.")
        form_ui_model.update({
            'success_message': success_message,
            "reporter_id": reporter_id,
            "is_linked": is_linked,
            "reporter_name": reporter_name
        })
        # if len(survey_response_form.changed_data) or is_errored_before_edit:
        created_request = helper.create_request(survey_response_form,
                                                request.user.username)

        additional_feed_dictionary = get_project_details_dict_for_feed(
            questionnaire_form_model)
        user_profile = NGOUserProfile.objects.get(user=request.user)
        feeds_dbm = get_feeds_database(request.user)
        response = WebPlayerV2(manager, feeds_dbm, user_profile.reporter_id) \
            .edit_survey_response(created_request, survey_response, owner_id,
                                  additional_feed_dictionary, websubmission_logger)
        mail_feed_errors(response, manager.database_name)
        if response.success:
            build_static_info_context(manager, survey_response, form_ui_model,
                                      questionnaire_form_model, reporter_id)
            ReportRouter().route(get_organization(request).org_id, response)
            _update_static_info_block_status(form_ui_model,
                                             is_errored_before_edit)
            log_edit_action(original_survey_response, survey_response, request,
                            questionnaire_form_model.name,
                            questionnaire_form_model)
            if submitted_values.get("redirect_url"):
                return HttpResponseRedirect(
                    submitted_values.get("redirect_url"))
        else:
            del form_ui_model["success_message"]
            survey_response_form._errors = helper.errors_to_list(
                response.errors, questionnaire_form_model.fields)
            form_ui_model.update({
                "reporter_id": reporter_id,
                "is_linked": is_linked,
                "reporter_name": reporter_name
            })
        return render_to_response("project/web_questionnaire.html",
                                  form_ui_model,
                                  context_instance=RequestContext(request))