Ejemplo n.º 1
0
    def get_context_data(self, **kwargs):
        org = self.request.user.get_org()

        # if there isn't a search filtering the queryset, we can replace the count function with a quick cache lookup to
        # speed up paging
        if hasattr(self, 'folder') and 'search' not in self.request.REQUEST:
            org.patch_folder_queryset(self.object_list, self.folder, self.request)

        context = super(ContactListView, self).get_context_data(**kwargs)

        folders = [dict(count=org.get_folder_count(OrgFolder.contacts_all), label=_("All Contacts"), url=reverse('contacts.contact_list')),
                   dict(count=org.get_folder_count(OrgFolder.contacts_failed), label=_("Failed"), url=reverse('contacts.contact_failed')),
                   dict(count=org.get_folder_count(OrgFolder.contacts_blocked), label=_("Blocked"), url=reverse('contacts.contact_blocked'))]

        groups_qs = ContactGroup.user_groups.filter(org=org, is_active=True).select_related('org')
        groups_qs = groups_qs.extra(select={'lower_group_name': 'lower(contacts_contactgroup.name)'}).order_by('lower_group_name')
        groups = [dict(pk=g.pk, label=g.name, count=g.get_member_count(), is_dynamic=g.is_dynamic) for g in groups_qs]

        # resolve the paginated object list so we can initialize a cache of URNs and fields
        contacts = list(context['object_list'])
        Contact.bulk_cache_initialize(org, contacts, for_show_only=True)

        context['contacts'] = contacts
        context['groups'] = groups
        context['folders'] = folders
        context['has_contacts'] = contacts or org.has_contacts()
        context['send_form'] = SendMessageForm(self.request.user)
        return context
Ejemplo n.º 2
0
    def get_context_data(self, **kwargs):
        org = self.request.user.get_org()

        # if there isn't a search filtering the queryset, we can replace the count function with a quick cache lookup to
        # speed up paging
        if hasattr(self, 'folder') and 'search' not in self.request.REQUEST:
            org.patch_folder_queryset(self.object_list, self.folder, self.request)

        context = super(ContactListView, self).get_context_data(**kwargs)

        folders = [dict(count=org.get_folder_count(OrgFolder.contacts_all), label=_("All Contacts"), url=reverse('contacts.contact_list')),
                   dict(count=org.get_folder_count(OrgFolder.contacts_failed), label=_("Failed"), url=reverse('contacts.contact_failed')),
                   dict(count=org.get_folder_count(OrgFolder.contacts_blocked), label=_("Blocked"), url=reverse('contacts.contact_blocked'))]

        groups_qs = ContactGroup.user_groups.filter(org=org, is_active=True).select_related('org')
        groups_qs = groups_qs.extra(select={'lower_group_name': 'lower(contacts_contactgroup.name)'}).order_by('lower_group_name')
        groups = [dict(pk=g.pk, label=g.name, count=g.get_member_count(), is_dynamic=g.is_dynamic) for g in groups_qs]

        # resolve the paginated object list so we can initialize a cache of URNs and fields
        contacts = list(context['object_list'])
        Contact.bulk_cache_initialize(org, contacts, for_show_only=True)

        context['contacts'] = contacts
        context['groups'] = groups
        context['folders'] = folders
        context['has_contacts'] = contacts or org.has_contacts()
        context['send_form'] = SendMessageForm(self.request.user)
        return context
Ejemplo n.º 3
0
def omnibox_mixed_search(org, query, types):
    """
    Performs a mixed group, contact and URN search, returning the first N matches of each type.
    """
    query_terms = query.split(" ") if query else None
    search_types = types or (SEARCH_ALL_GROUPS, SEARCH_CONTACTS, SEARCH_URNS)
    per_type_limit = 25
    results = []

    if SEARCH_ALL_GROUPS in search_types or SEARCH_STATIC_GROUPS in search_types:
        groups = ContactGroup.get_user_groups(org, ready_only=True)

        # exclude dynamic groups if not searching all groups
        if SEARCH_ALL_GROUPS not in search_types:
            groups = groups.filter(query=None)

        if query:
            groups = term_search(groups, ("name__icontains", ), query_terms)

        results += list(groups.order_by(Upper("name"))[:per_type_limit])

    if SEARCH_CONTACTS in search_types:
        try:
            search_results = search_contacts(
                org,
                query,
                group=org.cached_active_contacts_group,
                sort="name")
            contacts = IDSliceQuerySet(Contact, search_results.contact_ids, 0,
                                       len(search_results.contact_ids))
            results += list(contacts[:per_type_limit])
            Contact.bulk_cache_initialize(org, contacts=results)

        except SearchException:
            pass

    if SEARCH_URNS in search_types:
        if not org.is_anon and query and len(query) >= 3:
            try:
                # build an OR'ed query of all sendable schemes
                sendable_schemes = org.get_schemes(Channel.ROLE_SEND)
                scheme_query = " OR ".join(f"{s} ~ {json.dumps(query)}"
                                           for s in sendable_schemes)
                search_results = search_contacts(
                    org,
                    scheme_query,
                    group=org.cached_active_contacts_group,
                    sort="name")
                urns = ContactURN.objects.filter(
                    contact_id__in=search_results.contact_ids,
                    scheme__in=sendable_schemes)
                results += list(
                    urns.prefetch_related("contact").order_by(
                        Upper("path"))[:per_type_limit])
            except SearchException:
                pass

    return results
Ejemplo n.º 4
0
def omnibox_mixed_search(org, search, types):
    """
    Performs a mixed group, contact and URN search, returning the first N matches of each type.
    """
    search_terms = search.split(" ") if search else None
    search_types = types or (SEARCH_ALL_GROUPS, SEARCH_CONTACTS, SEARCH_URNS)
    per_type_limit = 25
    results = []

    if SEARCH_ALL_GROUPS in search_types or SEARCH_STATIC_GROUPS in search_types:
        groups = ContactGroup.get_user_groups(org, ready_only=True)

        # exclude dynamic groups if not searching all groups
        if SEARCH_ALL_GROUPS not in search_types:
            groups = groups.filter(query=None)

        if search:
            groups = term_search(groups, ("name__icontains", ), search_terms)

        results += list(groups.order_by(Upper("name"))[:per_type_limit])

    if SEARCH_CONTACTS in search_types:
        sort_struct = {
            "field_type": "attribute",
            "sort_direction": "asc",
            "field_name": "name.keyword"
        }

        try:
            search_id = int(search)
        except (ValueError, TypeError):
            search_id = None

        if org.is_anon and search_id is not None:
            search_text = f"id = {search_id}"
        elif search:
            search_text = " AND ".join(f"name ~ {search_term}"
                                       for search_term in search_terms)

        else:
            search_text = None

        from temba.utils.es import ES

        try:
            search_object, _ = contact_es_search(org,
                                                 search_text,
                                                 sort_struct=sort_struct)

            es_search = search_object.source(
                fields=("id", )).using(ES)[:per_type_limit].execute()
            contact_ids = list(mapEStoDB(Contact, es_search, only_ids=True))
            es_results = Contact.objects.filter(id__in=contact_ids).order_by(
                Upper("name"))

            results += list(es_results[:per_type_limit])

            Contact.bulk_cache_initialize(org, contacts=results)

        except SearchException:
            # ignore SearchException
            pass

    if SEARCH_URNS in search_types:
        # only include URNs that are send-able
        from temba.channels.models import Channel

        allowed_schemes = org.get_schemes(Channel.ROLE_SEND)

        from temba.utils.es import ES, ModelESSearch

        if search:
            # we use trigrams on Elasticsearch, minimum required length for a term is 3
            filtered_search_terms = (
                search_term for search_term in search_terms
                if search_term != ""  # and len(search_term) >= 3
            )

            must_condition = [{
                "match_phrase": {
                    "urns.path": search_term
                }
            } for search_term in filtered_search_terms]
        else:
            must_condition = []

        es_query = {
            "query": {
                "bool": {
                    "filter": [
                        {
                            "term": {
                                "org_id": org.id
                            }
                        },
                        {
                            "term": {
                                "groups":
                                str(org.cached_all_contacts_group.uuid)
                            }
                        },
                    ],
                    "must": [{
                        "nested": {
                            "path": "urns",
                            "query": {
                                "bool": {
                                    "must":
                                    must_condition,
                                    "should": [{
                                        "term": {
                                            "urns.scheme": scheme
                                        }
                                    } for scheme in allowed_schemes],
                                }
                            },
                        }
                    }],
                }
            },
            "sort": [{
                "name.keyword": {
                    "order": "asc"
                }
            }],
        }

        if not org.is_anon:
            search_object = ModelESSearch(
                model=Contact,
                index="contacts").from_dict(es_query).params(routing=org.id)

            es_search = search_object.source(
                fields=("id", )).using(ES)[:per_type_limit].execute()
            es_results = mapEStoDB(Contact, es_search, only_ids=True)

            # get ContactURNs for filtered Contacts
            urns = ContactURN.objects.filter(contact_id__in=list(es_results))

            # we got max `per_type_limit` contacts, but each contact can have multiple URNs and we need to limit the
            # results to the per type limit
            results += list(
                urns.prefetch_related("contact").order_by(
                    Upper("path"))[:per_type_limit])

    return results  # sorted(results, key=lambda o: o.name if hasattr(o, 'name') else o.path)
Ejemplo n.º 5
0
 def prepare_for_serialization(self, object_list):
     # initialize caches of all contact fields and URNs
     org = self.request.user.get_org()
     Contact.bulk_cache_initialize(org, object_list)
Ejemplo n.º 6
0
 def prepare_for_serialization(self, object_list):
     # initialize caches of all contact fields and URNs
     org = self.request.user.get_org()
     Contact.bulk_cache_initialize(org, object_list)
Ejemplo n.º 7
0
def omnibox_mixed_search(org, search, types):
    """
    Performs a mixed group, contact and URN search, returning the first N matches of each type.
    """
    search_terms = search.split(" ") if search else None
    search_types = types or (SEARCH_ALL_GROUPS, SEARCH_CONTACTS, SEARCH_URNS)
    per_type_limit = 25
    results = []

    if SEARCH_ALL_GROUPS in search_types or SEARCH_STATIC_GROUPS in search_types:
        groups = ContactGroup.get_user_groups(org)

        # exclude dynamic groups if not searching all groups
        if SEARCH_ALL_GROUPS not in search_types:
            groups = groups.filter(query=None)

        if search:
            groups = term_search(groups, ("name__icontains",), search_terms)

        results += list(groups.order_by(Upper("name"))[:per_type_limit])

    if SEARCH_CONTACTS in search_types:
        sort_struct = {"field_type": "attribute", "sort_direction": "asc", "field_name": "name.keyword"}

        try:
            search_id = int(search)
        except (ValueError, TypeError):
            search_id = None

        if org.is_anon and search_id is not None:
            search_text = f"id = {search_id}"
        elif search:
            # we use trigrams on Elasticsearch, minimum required length for a term is 3
            filtered_search_terms = (
                search_term for search_term in search_terms if search_term != "" and len(search_term) >= 3
            )

            search_text = " AND ".join(f"name ~ {search_term}" for search_term in filtered_search_terms)

        else:
            search_text = None

        from temba.utils.es import ES

        try:
            search_object, _ = contact_es_search(org, search_text, sort_struct=sort_struct)

            es_search = search_object.source(fields=("id",)).using(ES)[:per_type_limit].execute()
            contact_ids = list(mapEStoDB(Contact, es_search, only_ids=True))
            es_results = Contact.objects.filter(id__in=contact_ids).order_by(Upper("name"))

            results += list(es_results[:per_type_limit])

            Contact.bulk_cache_initialize(org, contacts=results)

        except SearchException:
            # ignore SearchException
            pass

    if SEARCH_URNS in search_types:
        # only include URNs that are send-able
        from temba.channels.models import Channel

        allowed_schemes = org.get_schemes(Channel.ROLE_SEND)

        from temba.utils.es import ES, ModelESSearch

        if search:
            # we use trigrams on Elasticsearch, minimum required length for a term is 3
            filtered_search_terms = (
                search_term for search_term in search_terms if search_term != "" and len(search_term) >= 3
            )

            must_condition = [{"match_phrase": {"urns.path": search_term}} for search_term in filtered_search_terms]
        else:
            must_condition = []

        es_query = {
            "query": {
                "bool": {
                    "filter": [
                        {"term": {"org_id": org.id}},
                        {"term": {"groups": str(org.cached_all_contacts_group.uuid)}},
                    ],
                    "must": [
                        {
                            "nested": {
                                "path": "urns",
                                "query": {
                                    "bool": {
                                        "must": must_condition,
                                        "should": [{"term": {"urns.scheme": scheme}} for scheme in allowed_schemes],
                                    }
                                },
                            }
                        }
                    ],
                }
            },
            "sort": [{"name.keyword": {"order": "asc"}}],
        }

        if not org.is_anon:
            search_object = ModelESSearch(model=Contact, index="contacts").from_dict(es_query).params(routing=org.id)

            es_search = search_object.source(fields=("id",)).using(ES)[:per_type_limit].execute()
            es_results = mapEStoDB(Contact, es_search, only_ids=True)

            # get ContactURNs for filtered Contacts
            urns = ContactURN.objects.filter(contact_id__in=list(es_results))

            # we got max `per_type_limit` contacts, but each contact can have multiple URNs and we need to limit the
            # results to the per type limit
            results += list(urns.prefetch_related("contact").order_by(Upper("path"))[:per_type_limit])

    return results  # sorted(results, key=lambda o: o.name if hasattr(o, 'name') else o.path)