def test_should_format_non_image_media_question(self):
        form_model = MagicMock(spec=FormModel)
        form_model.is_media_type_fields_present = True
        form_model.media_fields = [VideoField("video", "mp4", "mp4")]
        required_field_names = [
            'some_question', 'ds_id', 'ds_name', 'form_model_id_mp4'
        ]
        results = Response({
            "_hits": [
                Result({
                    '_type': "form_model_id",
                    '_id': 'index_id',
                    '_source': {
                        'ds_id': 'his_id',
                        'ds_name': 'his_name',
                        'form_model_id_q1': 'sub_last_name',
                        'form_model_id_mp4': 'vid.mp4',
                        'some_question': 'answer for it'
                    }
                })
            ]
        })
        form_model.id = 'form_model_id'
        local_time_delta = ('+', 2, 0)

        submissions = SubmissionQueryResponseCreator(
            form_model,
            local_time_delta).create_response(required_field_names, results)

        expected = [[
            'index_id', 'answer for it',
            ["his_name<span class='small_grey'>  his_id</span>"],
            '  <a href=\'/download/attachment/index_id/vid.mp4\'>vid.mp4</a>'
        ]]
        self.assertEqual(submissions, expected)
    def test_should_give_create_response_with_no_unique_id_fields(self):
        form_model = MagicMock(spec=FormModel)
        required_field_names = ['ds_id', 'ds_name', 'some_question']
        results = Response({
            "_hits": [
                Result({
                    '_type': "form_model_id",
                    '_id': 'index_id',
                    '_source': {
                        'ds_id': 'his_id',
                        'ds_name': 'his_name',
                        'some_question': 'answer'
                    }
                })
            ]
        })

        form_model.entity_questions = []
        form_model.id = 'form_model_id'
        local_time_delta = ('+', 2, 0)

        submissions = SubmissionQueryResponseCreator(
            form_model,
            local_time_delta).create_response(required_field_names, results)

        expected = [[
            'index_id', ["his_name<span class='small_grey'>  his_id</span>"],
            'answer'
        ]]
        self.assertEqual(submissions, expected)
Example #3
0
def _get_aggregate_response(form_model, search, search_parameters,
                            local_time_delta):
    search = _aggregate_duplicates(
        form_model,
        search_parameters.get('search_filters').get('duplicatesForFilter'),
        search)
    search_results = search.execute()
    submission_response = SubmissionQueryResponseCreator(
        form_model,
        local_time_delta).create_aggregate_response(search_results,
                                                    search_parameters)
    for submission in submission_response:
        yield submission.to_dict()
Example #4
0
    def test_should_give_back_none_for_no_entry_for_datasender_or_subject_ids(self):
        form_model = MagicMock(spec=FormModel)
        required_field_names = ['ds_id', 'ds_name', 'entity_short_code', 'entity_question', 'some_question']
        query = Mock()
        dict_result = DictSearchResults('', {}, [{'_id': 'index_id',
                                                  '_source': {'some_question': 'answer'}}], '')
        query.values_dict.return_value = dict_result
        form_model.entity_question = Mock(code='q1')

        submissions = SubmissionQueryResponseCreator(form_model).create_response(required_field_names, query)

        expected = [['index_id', None, None, 'answer']]
        self.assertEqual(submissions, expected)
Example #5
0
    def test_should_give_create_response_with_no_unique_id_fields(self):
        form_model = MagicMock(spec=FormModel)
        required_field_names = ['ds_id', 'ds_name', 'some_question']
        query = Mock()
        dict_result = DictSearchResults('', {}, [{'_id': 'index_id',
                                                  '_source': {'ds_id': 'his_id', 'ds_name': 'his_name',
                                                              'some_question': 'answer'}}], '')
        query.values_dict.return_value = dict_result
        form_model.entity_questions = []
        form_model.id = 'form_model_id'
        submissions = SubmissionQueryResponseCreator(form_model).create_response(required_field_names, query)

        expected = [['index_id', ["his_name<span class='small_grey'>  his_id</span>"], 'answer']]
        self.assertEqual(submissions, expected)
Example #6
0
    def test_should_append_styling_for_datasender_and_subject_ids(self):
        form_model = MagicMock(spec=FormModel)
        required_field_names = ['ds_id', 'ds_name', 'entity_short_code', 'entity_question']
        query = Mock()
        dict_result = DictSearchResults('', {}, [{'_id': 'index_id',
                                                  '_source': {'ds_id': 'some_id', 'ds_name': 'his_name',
                                                              'entity_short_code': 'subject_id',
                                                              'entity_question': 'sub_last_name'}}], '')
        query.values_dict.return_value = dict_result
        form_model.entity_question = Mock(code='entity_question')

        submissions = SubmissionQueryResponseCreator(form_model).create_response(required_field_names, query)

        expected = [['index_id', ["his_name<span class='small_grey'>  some_id</span>"],
                     ["sub_last_name<span class='small_grey'>  subject_id</span>"]]]
        self.assertEqual(submissions, expected)
Example #7
0
    def test_should_give_back_entries_according_to_header_order(self):
        form_model = MagicMock(spec=FormModel)
        required_field_names = ['some_question', 'ds_id', 'ds_name', 'form_model_id_q1', 'form_model_id_q1_unique_code']
        query = Mock()
        dict_result = DictSearchResults('', {}, [{'_id': 'index_id',
                                                  '_source': {'ds_id': 'his_id', 'ds_name': 'his_name',
                                                              'form_model_id_q1_unique_code': 'subject_id',
                                                              'form_model_id_q1': 'sub_last_name',
                                                              'some_question': 'answer for it'}}], '')
        query.values_dict.return_value = dict_result
        form_model.entity_questions = [UniqueIdField('Test subject', 'name', 'q1', 'which subject')]
        form_model.id = 'form_model_id'

        submissions = SubmissionQueryResponseCreator(form_model).create_response(required_field_names, query)

        expected = [['index_id', 'answer for it', ["his_name<span class='small_grey'>  his_id</span>"],
                     ["sub_last_name<span class='small_grey'>  subject_id</span>"]]]
        self.assertEqual(submissions, expected)
Example #8
0
def get_submissions(request, form_code):
    dbm = get_database_manager(request.user)
    questionnaire = get_project_by_code(dbm, form_code)
    search_parameters = {}
    search_parameters.update(
        {"start_result_number": int(request.POST.get('iDisplayStart'))})
    search_parameters.update(
        {"number_of_results": int(request.POST.get('iDisplayLength'))})
    filter_type = request.GET['type']
    search_parameters.update({"filter": filter_type})

    search_parameters.update({
        "sort_field":
        _get_field_to_sort_on(request.POST, questionnaire, filter_type)
    })
    search_parameters.update(
        {"order": "-" if request.POST.get('sSortDir_0') == "desc" else ""})
    search_filters = json.loads(request.POST.get('search_filters'))
    search_parameters.update({"search_filters": search_filters})
    search_text = search_filters.get("search_text", '')
    search_parameters.update({"search_text": search_text})
    organization = get_organization(request)
    local_time_delta = get_country_time_delta(organization.country)
    search_results, query_fields = get_submissions_paginated(
        dbm, questionnaire, search_parameters, local_time_delta)
    submission_count_with_filters = get_submission_count(
        dbm, questionnaire, search_parameters, local_time_delta)
    submission_count_without_filters = get_submissions_without_user_filters_count(
        dbm, questionnaire, search_parameters)
    submissions = SubmissionQueryResponseCreator(
        questionnaire,
        local_time_delta).create_response(query_fields, search_results)

    return HttpResponse(jsonpickle.encode(
        {
            'data': submissions,
            'iTotalDisplayRecords': submission_count_with_filters,
            'iDisplayStart': int(request.POST.get('iDisplayStart')),
            "iTotalRecords": submission_count_without_filters,
            'iDisplayLength': int(request.POST.get('iDisplayLength'))
        },
        unpicklable=False),
                        content_type='application/json')
Example #9
0
def _get_slim_submission_paginated(request, project_uuid):
    dbm = get_database_manager(request.user)
    form_model = FormModel.get(dbm, project_uuid)
    length = int(request.GET.get('length', '10'))
    start = int(request.GET.get('start', '0'))
    search_text = request.GET.get('search_str')
    search_parameters = {}
    search_parameters.update({"start_result_number": start})
    search_parameters.update({"number_of_results": length})
    search_parameters.update({"filter": 'all'})
    search_parameters.update({"headers_for": 'all'})
    search_parameters.update(
        {'response_fields': ['ds_id', 'ds_name', 'date', 'status']})
    search_parameters.update({"sort_field": "date"})
    search_parameters.update({"order": "-"})
    search_filters = {
        "submissionDatePicker": "All Dates",
        "datasenderFilter": "",
        "search_text": search_text,
        "dateQuestionFilters": {},
        "uniqueIdFilters": {}
    }
    search_parameters.update({"search_filters": search_filters})
    search_parameters.update({"search_text": search_text})
    local_time_delta = get_country_time_delta('IN')
    search_results, query_fields = get_submissions_paginated(
        dbm, form_model, search_parameters, local_time_delta)
    submission_count_with_filters = get_submission_count(
        dbm, form_model, search_parameters, local_time_delta)
    submissions = SubmissionQueryResponseCreator(form_model, local_time_delta) \
                    .create_response(query_fields, search_results)
    return {
        'data': submissions,
        'headers': '',
        'total': submission_count_with_filters,
        'start': start,
        "search_count": len(submissions),
        'length': length
    }
Example #10
0
    def test_should_format_image_question(self):
        form_model = MagicMock(spec=FormModel)
        form_model.is_media_type_fields_present = True
        form_model.media_fields = [PhotoField("photo", "img", "img")]
        required_field_names = [
            'some_question', 'ds_id', 'ds_name', 'form_model_id_img'
        ]
        hits = AttrList([
            Result({
                '_type': "form_model_id",
                '_id': 'index_id',
                '_source': {
                    'ds_id': 'his_id',
                    'ds_name': 'his_name',
                    'form_model_id_q1': 'sub_last_name',
                    'form_model_id_img': 'img2.png',
                    'some_question': 'answer for it'
                }
            })
        ])
        hits.total = 1
        results = Response({"_hits": hits})
        form_model.id = 'form_model_id'
        local_time_delta = ('+', 2, 0)

        submissions = SubmissionQueryResponseCreator(
            form_model,
            local_time_delta).create_response(required_field_names, results,
                                              {})

        expected = ([[
            'index_id', 'answer for it',
            ["his_name<span class='small_grey'>  his_id</span>"],
            '<img src=\'/download/attachment/index_id/preview_img2.png\' alt=\'\'/>  <a href=\'/download/attachment/index_id/img2.png\'>img2.png</a>',
            0
        ]], 1)
        self.assertEqual(submissions, expected)
Example #11
0
    def test_should_give_back_entries_according_to_header_order(self):
        form_model = MagicMock(spec=FormModel)
        required_field_names = [
            'some_question', 'ds_id', 'ds_name', 'form_model_id_q1',
            'form_model_id_q1_unique_code'
        ]
        hits = AttrList([
            Result({
                '_type': "form_model_id",
                '_id': 'index_id',
                '_source': {
                    'ds_id': 'his_id',
                    'ds_name': 'his_name',
                    'form_model_id_q1_unique_code': 'subject_id',
                    'form_model_id_q1': 'sub_last_name',
                    'some_question': 'answer for it'
                }
            })
        ])
        hits.total = 1
        results = Response({"_hits": hits})
        form_model.entity_questions = [
            UniqueIdField('Test subject', 'name', 'q1', 'which subject')
        ]
        form_model.id = 'form_model_id'
        local_time_delta = ('+', 2, 0)
        submissions = SubmissionQueryResponseCreator(
            form_model,
            local_time_delta).create_response(required_field_names, results,
                                              {})

        expected = ([[
            'index_id', 'answer for it',
            ["his_name<span class='small_grey'>  his_id</span>"],
            ["sub_last_name<span class='small_grey'>  subject_id</span>"], 0
        ]], 1)
        self.assertEqual(submissions, expected)