Esempio n. 1
0
def update_case_search_config(domain_link):
    if domain_link.is_remote:
        remote_properties = remote_get_case_search_config(domain_link)
        case_search_config = remote_properties['config']
        if not case_search_config:
            return
        query_addition = remote_properties['addition']
    else:
        try:
            case_search_config = CaseSearchConfig.objects.get(
                domain=domain_link.master_domain).to_json()
        except CaseSearchConfig.DoesNotExist:
            return

        try:
            query_addition = CaseSearchQueryAddition.objects.get(
                domain=domain_link.master_domain).to_json()
        except CaseSearchQueryAddition.DoesNotExist:
            query_addition = None

    CaseSearchConfig.create_model_and_index_from_json(
        domain_link.linked_domain, case_search_config)

    if query_addition:
        CaseSearchQueryAddition.create_from_json(domain_link.linked_domain,
                                                 query_addition)
Esempio n. 2
0
def update_case_search_config(domain_link):
    if domain_link.is_remote:
        remote_properties = remote_get_case_search_config(domain_link)
        case_search_config = remote_properties['config']
        if not case_search_config:
            return
    else:
        try:
            case_search_config = CaseSearchConfig.objects.get(domain=domain_link.master_domain).to_json()
        except CaseSearchConfig.DoesNotExist:
            return

    CaseSearchConfig.create_model_and_index_from_json(domain_link.linked_domain, case_search_config)
Esempio n. 3
0
def search(request, domain):
    """
    Accepts search criteria as GET params, e.g. "https://www.commcarehq.org/a/domain/phone/search/?a=b&c=d"
    Returns results as a fixture with the same structure as a casedb instance.
    """
    criteria = request.GET.dict()
    try:
        case_type = criteria.pop('case_type')
    except KeyError:
        return HttpResponse('Search request must specify case type',
                            status=400)
    try:
        include_closed = criteria.pop('include_closed')
    except KeyError:
        include_closed = False

    search_es = (CaseSearchES().domain(domain).case_type(case_type).size(
        CASE_SEARCH_MAX_RESULTS))

    if include_closed != 'True':
        search_es = search_es.is_closed(False)

    try:
        config = CaseSearchConfig.objects.get(domain=domain)
    except CaseSearchConfig.DoesNotExist as e:
        from corehq.util.soft_assert import soft_assert
        _soft_assert = soft_assert(to="{}@{}.com".format('frener', 'dimagi'),
                                   notify_admins=False,
                                   send_to_ops=False)
        _soft_assert(
            False,
            u"Someone in domain: {} tried accessing case search without a config"
            .format(domain), e)
        config = CaseSearchConfig(domain=domain)

    query_addition_id = criteria.pop(SEARCH_QUERY_ADDITION_KEY, None)

    fuzzies = config.config.get_fuzzy_properties_for_case_type(case_type)
    for key, value in criteria.items():
        search_es = search_es.case_property_query(key,
                                                  value,
                                                  fuzzy=(key in fuzzies))

    query_addition_debug_details = {}
    try:
        search_es = _add_case_search_addition(request, domain, search_es,
                                              query_addition_id,
                                              query_addition_debug_details)
    except QueryMergeException as e:
        return _handle_query_merge_exception(request, e)
    try:
        results = search_es.values()
    except Exception as e:
        return _handle_es_exception(request, e, query_addition_debug_details)

    # Even if it's a SQL domain, we just need to render the results as cases, so CommCareCase.wrap will be fine
    cases = [CommCareCase.wrap(flatten_result(result)) for result in results]
    fixtures = CaseDBFixture(cases).fixture
    return HttpResponse(fixtures, content_type="text/xml")
    def test_link_enables_case_search(self):
        existing_search_config = CaseSearchConfig(domain=self.domain_link.linked_domain, enabled=False)
        existing_search_config.save()
        fuzzy_property = FuzzyProperties(domain=self.domain_link.linked_domain, case_type='case',
                                         properties=['a', 'b', 'd'])
        fuzzy_property.save()
        existing_search_config.fuzzy_properties.add(fuzzy_property)
        update_case_search_config(self.domain_link)
        search_config = (CaseSearchConfig.objects
                         .prefetch_related('fuzzy_properties')
                         .get(domain=self.domain_link.linked_domain))

        self.assertTrue(search_config.enabled)

        # overwrites the existing fuzzy properties settings
        self.assertEqual(1, search_config.fuzzy_properties.count())
        self.assertEqual(['a', 'b', 'c'], search_config.fuzzy_properties.all()[0].properties)

        self.assertNotEqual(search_config.pk, self.search_config.pk)
    def test_link_enables_case_search(self):
        existing_search_config = CaseSearchConfig(domain=self.domain_link.linked_domain, enabled=False)
        existing_search_config.save()
        fuzzy_property = FuzzyProperties(domain=self.domain_link.linked_domain, case_type='case',
                                         properties=['a', 'b', 'd'])
        fuzzy_property.save()
        existing_search_config.fuzzy_properties.add(fuzzy_property)
        update_case_search_config(self.domain_link)
        search_config = (CaseSearchConfig.objects
                         .prefetch_related('fuzzy_properties')
                         .get(domain=self.domain_link.linked_domain))

        self.assertTrue(search_config.enabled)

        # overwrites the existing fuzzy properties settings
        self.assertEqual(1, search_config.fuzzy_properties.count())
        self.assertEqual(['a', 'b', 'c'], search_config.fuzzy_properties.all()[0].properties)

        self.assertNotEqual(search_config.pk, self.search_config.pk)
 def setUpClass(cls):
     super(BaseLinkedCaseClaimTest, cls).setUpClass()
     cls.search_config = CaseSearchConfig(domain=cls.domain, enabled=True)
     cls.search_config.save()
     fuzzy_property = FuzzyProperties(domain=cls.domain, case_type='case', properties=['a', 'b', 'c'])
     fuzzy_property.save()
     ignore_patterns = IgnorePatterns(domain=cls.domain, case_type='case', case_property='a', regex='+')
     ignore_patterns.save()
     cls.search_config.fuzzy_properties.add(fuzzy_property)
     cls.search_config.ignore_patterns.add(ignore_patterns)
Esempio n. 7
0
def update_case_search_config(domain_link):
    if domain_link.is_remote:
        remote_properties = remote_get_case_search_config(domain_link)
        case_search_config = remote_properties['config']
        if not case_search_config:
            return
        query_addition = remote_properties['addition']
    else:
        try:
            case_search_config = CaseSearchConfig.objects.get(domain=domain_link.master_domain).to_json()
        except CaseSearchConfig.DoesNotExist:
            return

        try:
            query_addition = CaseSearchQueryAddition.objects.get(domain=domain_link.master_domain).to_json()
        except CaseSearchQueryAddition.DoesNotExist:
            query_addition = None

    CaseSearchConfig.create_model_and_index_from_json(domain_link.linked_domain, case_search_config)

    if query_addition:
        CaseSearchQueryAddition.create_from_json(domain_link.linked_domain, query_addition)
Esempio n. 8
0
 def _get_config(self):
     try:
         config = (CaseSearchConfig.objects.prefetch_related(
             'fuzzy_properties').prefetch_related('ignore_patterns').get(
                 domain=self.domain))
     except CaseSearchConfig.DoesNotExist as e:
         from corehq.util.soft_assert import soft_assert
         _soft_assert = soft_assert(to="{}@{}.com".format(
             'frener', 'dimagi'),
                                    notify_admins=False,
                                    send_to_ops=False)
         _soft_assert(
             False,
             "Someone in domain: {} tried accessing case search without a config"
             .format(self.domain), e)
         config = CaseSearchConfig(domain=self.domain)
     return config
Esempio n. 9
0
    def test_fuzzy_search_parameters(self):
        config = CaseSearchConfig(domain=self.domain).config

        self.assertItemsEqual(
            config.get_fuzzy_properties_for_case_type('mermaids'), [])

        config.add_fuzzy_properties(case_type="pirates",
                                    properties=["name", "age"])
        config.add_fuzzy_properties(case_type="pirates",
                                    properties=["smells_bad"])
        config.add_fuzzy_properties(case_type="swashbucklers",
                                    properties=["has_parrot"])

        self.assertItemsEqual(
            config.get_fuzzy_properties_for_case_type('pirates'),
            ['name', 'age', 'smells_bad'])
        self.assertItemsEqual(
            config.get_fuzzy_properties_for_case_type('swashbucklers'),
            ['has_parrot'])

        config.add_fuzzy_property(case_type="swashbucklers",
                                  property="has_sword")
        self.assertItemsEqual(
            config.get_fuzzy_properties_for_case_type('swashbucklers'),
            ['has_parrot', 'has_sword'])

        config.remove_fuzzy_property(case_type="pirates",
                                     property="smells_bad")
        self.assertItemsEqual(
            config.get_fuzzy_properties_for_case_type('pirates'),
            ['name', 'age'])

        with self.assertRaises(AttributeError):
            config.remove_fuzzy_property(case_type="pirates",
                                         property="smells_bad")
Esempio n. 10
0
    def test_fuzzy_search_parameters(self):
        config = CaseSearchConfig(domain=self.domain).config

        self.assertItemsEqual(config.get_fuzzy_properties_for_case_type('mermaids'), [])

        config.add_fuzzy_properties(case_type="pirates", properties=["name", "age"])
        config.add_fuzzy_properties(case_type="pirates", properties=["smells_bad"])
        config.add_fuzzy_properties(case_type="swashbucklers", properties=["has_parrot"])

        self.assertItemsEqual(config.get_fuzzy_properties_for_case_type('pirates'), ['name', 'age', 'smells_bad'])
        self.assertItemsEqual(config.get_fuzzy_properties_for_case_type('swashbucklers'), ['has_parrot'])

        config.add_fuzzy_property(case_type="swashbucklers", property="has_sword")
        self.assertItemsEqual(
            config.get_fuzzy_properties_for_case_type('swashbucklers'),
            ['has_parrot', 'has_sword']
        )

        config.remove_fuzzy_property(case_type="pirates", property="smells_bad")
        self.assertItemsEqual(config.get_fuzzy_properties_for_case_type('pirates'), ['name', 'age'])

        with self.assertRaises(AttributeError):
            config.remove_fuzzy_property(case_type="pirates", property="smells_bad")