Beispiel #1
0
    def _get_data_case(self, params, filters):
        MAX_RESULTS = 200 # TODO vary by domain (cc-plus gets a higher limit?)
        # bleh
        _get = self.request.GET.copy()
        _get['iDisplayStart'] = '0'
        _get['iDisplayLength'] = str(MAX_RESULTS)
        self.request.GET = _get

        source = CaseListReport(self.request, domain=self.domain)

        total_count = source.es_results['hits']['total']
        if total_count > MAX_RESULTS:
            # can't really think of a better way to return out-of-band
            # metadata from a generator
            yield {'_meta': {
                    'total_rows': total_count,
                    'capped_rows': MAX_RESULTS,
                }}

        # TODO ideally we'd want access to all the data shown on the
        # case detail report. certain case types can override this via
        # case.to_full_dict(). however, there is currently no efficient
        # way to call this over a large block of cases. so now we (via the
        # CaseListReport/DataSource) limit ourselves only to that which
        # can be queried in bulk

        for data in source.get_data():
            case = CommCareCase.wrap(data['_case']).get_json()
            del data['_case']

            data['num_forms'] = len(case['xform_ids'])
            standard_props = (
                'case_name',
                'case_type',
                'date_opened',
                'external_id',
                'owner_id',
             )
            data.update(('prop_%s' % k, v) for k, v in case['properties'].iteritems() if k not in standard_props)

            GEO_DEFAULT = 'gps' # case property
            geo = None
            geo_directive = params['geo_fetch'].get(data['case_type'], GEO_DEFAULT)
            if geo_directive.startswith('link:'):
                # TODO use linked case
                pass
            elif geo_directive == '_random':
                # for testing -- just map the case to a random point
                import random
                import math
                geo = '%s %s' % (math.degrees(math.asin(random.uniform(-1, 1))), random.uniform(-180, 180))
            elif geo_directive:
                # case property
                geo = data.get('prop_%s' % geo_directive)

            if geo:
                data['geo'] = geo
                yield data
 def test_with_project_data_slug(self):
     report_slugs = ['project_data']
     q_dict_get = QueryDict('', mutable=True)
     q_dict_get.setlist('case_list_filter', report_slugs)
     self.request.GET = q_dict_get
     data = CaseListReport(self.request,
                           domain=self.domain).es_results['hits'].get(
                               'hits', [])
     expected_case_ids = ['id-1', 'id-2', 'id-3', 'id-5']
     queried_case_ids = [case['_id'] for case in data]
     self.assertCountEqual(expected_case_ids, queried_case_ids)