Пример #1
0
class CommCareCaseResource(SimpleSortableResourceMixin,
                           v0_3.CommCareCaseResource,
                           DomainSpecificResourceMixin):
    xforms_by_name = UseIfRequested(
        ToManyListDictField(
            'corehq.apps.api.resources.v0_4.XFormInstanceResource',
            attribute='xforms_by_name'))

    xforms_by_xmlns = UseIfRequested(
        ToManyListDictField(
            'corehq.apps.api.resources.v0_4.XFormInstanceResource',
            attribute='xforms_by_xmlns'))

    child_cases = UseIfRequested(
        ToManyDictField('corehq.apps.api.resources.v0_4.CommCareCaseResource',
                        attribute='child_cases'))

    parent_cases = UseIfRequested(
        ToManyDictField('corehq.apps.api.resources.v0_4.CommCareCaseResource',
                        attribute='parent_cases'))

    domain = fields.CharField(attribute='domain')

    date_modified = fields.CharField(attribute='modified_on',
                                     default="1900-01-01")
    indexed_on = fields.CharField(attribute='inserted_at',
                                  default="1900-01-01")
    server_date_modified = fields.CharField(attribute='server_modified_on',
                                            default="1900-01-01")
    server_date_opened = fields.CharField(attribute='server_opened_on',
                                          default="1900-01-01")
    opened_by = fields.CharField(attribute='opened_by', null=True)

    def obj_get(self, bundle, **kwargs):
        case_id = kwargs['pk']
        domain = kwargs['domain']
        return self.case_es(domain).get_document(case_id)

    class Meta(v0_3.CommCareCaseResource.Meta):
        max_limit = 1000
        serializer = CommCareCaseSerializer()
        ordering = ['server_date_modified', 'date_modified', 'indexed_on']
        object_class = ESCase
Пример #2
0
class CommCareCaseResource(SimpleSortableResourceMixin, v0_3.CommCareCaseResource, DomainSpecificResourceMixin):
    xforms_by_name = UseIfRequested(ToManyListDictField(
        'corehq.apps.api.resources.v0_4.XFormInstanceResource',
        attribute='xforms_by_name'
    ))

    xforms_by_xmlns = UseIfRequested(ToManyListDictField(
        'corehq.apps.api.resources.v0_4.XFormInstanceResource',
        attribute='xforms_by_xmlns'
    ))

    child_cases = UseIfRequested(
        ToManyDictField(
            'corehq.apps.api.resources.v0_4.CommCareCaseResource',
            attribute='child_cases'
        )
    )

    parent_cases = UseIfRequested(
        ToManyDictField(
            'corehq.apps.api.resources.v0_4.CommCareCaseResource',
            attribute='parent_cases'
        )
    )

    domain = fields.CharField(attribute='domain')

    # Fields that v0.2 assumed were pre-transformed but we are now operating on straight CommCareCase objects again
    date_modified = fields.CharField(attribute='modified_on', default="1900-01-01")
    server_date_modified = fields.CharField(attribute='server_modified_on', default="1900-01-01")
    server_date_opened = fields.CharField(attribute='server_opened_on', default="1900-01-01")

    def obj_get(self, bundle, **kwargs):
        case_id = kwargs['pk']
        try:
            case = CaseAccessors(kwargs['domain']).get_case(case_id)
            return case_to_es_case(case)
        except CaseNotFound:
            raise object_does_not_exist("CommCareCase", case_id)
Пример #3
0
class CommCareCaseResource(SimpleSortableResourceMixin,
                           v0_3.CommCareCaseResource,
                           DomainSpecificResourceMixin):

    xforms_by_name = UseIfRequested(
        ToManyListDictField(
            'corehq.apps.api.resources.v0_4.XFormInstanceResource',
            attribute=lambda case: group_by_dict(case.get_forms(), lambda form:
                                                 form.name)))

    xforms_by_xmlns = UseIfRequested(
        ToManyListDictField(
            'corehq.apps.api.resources.v0_4.XFormInstanceResource',
            attribute=lambda case: group_by_dict(case.get_forms(), lambda form:
                                                 form.xmlns)))

    child_cases = UseIfRequested(
        ToManyDictField(
            'corehq.apps.api.resources.v0_4.CommCareCaseResource',
            attribute=lambda case: dict([(
                index.identifier, CommCareCase.get(index.referenced_id))
                                         for index in case.indices])))

    parent_cases = UseIfRequested(
        ToManyDictField(
            'corehq.apps.api.resources.v0_4.CommCareCaseResource',
            attribute=lambda case: dict([(
                index.identifier, CommCareCase.get(index.referenced_id))
                                         for index in case.reverse_indices])))

    domain = fields.CharField(attribute='domain')

    # Fields that v0.2 assumed were pre-transformed but we are now operating on straight CommCareCase objects again
    date_modified = fields.CharField(attribute='modified_on',
                                     default="1900-01-01")
    server_date_modified = fields.CharField(attribute='server_modified_on',
                                            default="1900-01-01")
    server_date_opened = fields.CharField(attribute='server_opened_on',
                                          default="1900-01-01")

    def case_es(self, domain):
        return MOCK_CASE_ES or CaseES(domain)

    def obj_get_list(self, bundle, domain, **kwargs):
        filters = v0_3.CaseListFilters(bundle.request.GET).filters

        # Since tastypie handles the "from" and "size" via slicing, we have to wipe them out here
        # since ElasticCaseQuery adds them. I believe other APIs depend on the behavior of ElasticCaseQuery
        # hence I am not modifying that
        query = ElasticCaseQuery(domain, filters).get_query()
        if 'from' in query:
            del query['from']
        if 'size' in query:
            del query['size']

        return ESQuerySet(
            payload=query, model=CommCareCase, es_client=self.case_es(domain)
        ).order_by(
            'server_modified_on'
        )  # Not that CaseES is used only as an ES client, for `run_query` against the proper index

    class Meta(v0_3.CommCareCaseResource.Meta):
        max_limit = 100  # Today, takes ~25 seconds for some domains
        serializer = CommCareCaseSerializer()
        ordering = ['server_date_modified', 'date_modified']
Пример #4
0
class CommCareCaseResource(SimpleSortableResourceMixin,
                           v0_3.CommCareCaseResource,
                           DomainSpecificResourceMixin):
    xforms_by_name = UseIfRequested(
        ToManyListDictField(
            'corehq.apps.api.resources.v0_4.XFormInstanceResource',
            attribute='xforms_by_name'))

    xforms_by_xmlns = UseIfRequested(
        ToManyListDictField(
            'corehq.apps.api.resources.v0_4.XFormInstanceResource',
            attribute='xforms_by_xmlns'))

    child_cases = UseIfRequested(
        ToManyDictField('corehq.apps.api.resources.v0_4.CommCareCaseResource',
                        attribute='child_cases'))

    parent_cases = UseIfRequested(
        ToManyDictField('corehq.apps.api.resources.v0_4.CommCareCaseResource',
                        attribute='parent_cases'))

    domain = fields.CharField(attribute='domain')

    date_modified = fields.CharField(attribute='modified_on',
                                     default="1900-01-01")
    indexed_on = fields.CharField(attribute='inserted_at',
                                  default="1900-01-01")
    server_date_modified = fields.CharField(attribute='server_modified_on',
                                            default="1900-01-01")
    server_date_opened = fields.CharField(attribute='server_opened_on',
                                          default="1900-01-01")
    opened_by = fields.CharField(attribute='opened_by', null=True)

    def obj_get(self, bundle, **kwargs):
        case_id = kwargs['pk']
        domain = kwargs['domain']
        return self.case_es(domain).get_document(case_id)

    def case_es(self, domain):
        return MOCK_CASE_ES or CaseES(domain)

    def obj_get_list(self, bundle, domain, **kwargs):
        filters = v0_3.CaseListFilters(bundle.request.GET).filters

        # Since tastypie handles the "from" and "size" via slicing, we have to wipe them out here
        # since ElasticCaseQuery adds them. I believe other APIs depend on the behavior of ElasticCaseQuery
        # hence I am not modifying that
        query = ElasticCaseQuery(domain, filters).get_query()
        if 'from' in query:
            del query['from']
        if 'size' in query:
            del query['size']

        # Note that CaseES is used only as an ES client, for `run_query` against the proper index
        return ElasticAPIQuerySet(
            payload=query, model=ESCase,
            es_client=self.case_es(domain)).order_by('server_modified_on')

    class Meta(v0_3.CommCareCaseResource.Meta):
        max_limit = 1000
        serializer = CommCareCaseSerializer()
        ordering = ['server_date_modified', 'date_modified']
        object_class = ESCase