Example #1
0
    def test_should_create_subject_field(self):
        self.dbm.database.view = Mock(return_value=Mock(rows=[{"key":(0,"cli1"),"value":"Clinic One"}]))
        entity_field = UniqueIdField("","reporting on", "rep_on", "rep", instruction="")
        project = Project(self.dbm, 'some form', 'some', 'form_code_1', fields=[entity_field])

        subject_field_creator = Mock(spec=SubjectQuestionFieldCreator)
        mock_field = Mock()
        subject_field_creator.create.return_value = mock_field
        form = SurveyResponseForm(project, None,  True)

        self.assertEquals(form.fields["rep_on"].choices, [('cli1', 'Clinic One(cli1)')])
    def test_should_set_initial_values_for_submissions_with_upper_case_question_codes(
            self):
        initial_dict = {'Q1': 'Ans1', 'Q2': 'Ans2'}
        type(self.project).fields = PropertyMock(return_value=[
            TextField(name="Q1", code="Q1", label="some"),
            TextField(name="Q2", code="Q2", label="some")
        ])
        submission_form = SurveyResponseForm(self.project, initial_dict)

        self.assertEquals('Ans1', submission_form.fields.get('Q1').initial)
        self.assertEquals('Ans2', submission_form.fields.get('Q2').initial)
    def test_entity_question_form_field_created(self):
        project = MagicMock(spec=Project)
        fields = [
            UniqueIdField("clinic", "entity question", "q1",
                          "what are you reporting on?")
        ]
        project.form_code = '001'
        project.fields = fields
        project._dbm = Mock(spec=DatabaseManager)

        with patch.object(SubjectQuestionFieldCreator,
                          'create') as create_entity_field:
            choice_field = ChoiceField(('sub1', 'sub2', 'sub3'))
            create_entity_field.return_value = choice_field
            submission_form = SurveyResponseForm(project, {})
            self.assertEqual(choice_field, submission_form.fields['q1'])
    def test_should_create_submission_form_with_appropriate_fields(self):
        fields = [
            IntegerField('field_name', 'integer_field_code', 'label', Mock),
            DateField('Date', 'date_field_code', 'date_label', 'dd.mm.yyyy',
                      Mock),
            GeoCodeField('', 'geo_field_code', '', Mock),
            TextField('', 'text_field_code', '')
        ]
        type(self.project).fields = PropertyMock(return_value=fields)

        submission_form_create = SurveyResponseForm(self.project, {})
        expected_field_keys = [
            'form_code', 'dsid', 'integer_field_code', 'date_field_code',
            'geo_field_code', 'text_field_code'
        ]
        self.assertListEqual(submission_form_create.fields.keys(),
                             expected_field_keys)
Example #5
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))
Example #6
0
 def form(self, initial_data=None):
     return SurveyResponseForm(self.questionnaire,
                               data=initial_data,
                               is_datasender=self.is_data_sender)
Example #7
0
    def test_should_not_create_subject_fields_if_entity_field_is_not_present_in_form_model(self):
        form_model = Project(self.dbm, 'some form', 'some', 'form_code_1', fields=[])
        form = SurveyResponseForm(form_model, data=None, is_datasender=True)

        self.assertIsNone(form.fields.get('entity_question_code'))
Example #8
0
def get_web_preview_context_from_existing_project(project_info):
    return {
        'project': project_info,
        'questionnaire_form': SurveyResponseForm(project_info)
    }
Example #9
0
def get_web_preview_context_from_project_data(manager, post, project_info):
    form_model = get_questionnaire_form_model(manager, project_info, post)
    questionnaire_form = SurveyResponseForm(form_model)
    return {'project': project_info, 'questionnaire_form': questionnaire_form}