本文整理汇总了Python中temba.contacts.models.Contact.bulk_cache_initialize方法的典型用法代码示例。如果您正苦于以下问题:Python Contact.bulk_cache_initialize方法的具体用法?Python Contact.bulk_cache_initialize怎么用?Python Contact.bulk_cache_initialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类temba.contacts.models.Contact
的用法示例。
在下文中一共展示了Contact.bulk_cache_initialize方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_context_data
# 需要导入模块: from temba.contacts.models import Contact [as 别名]
# 或者: from temba.contacts.models.Contact import bulk_cache_initialize [as 别名]
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
示例2: prepare_for_serialization
# 需要导入模块: from temba.contacts.models import Contact [as 别名]
# 或者: from temba.contacts.models.Contact import bulk_cache_initialize [as 别名]
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)
示例3: omnibox_mixed_search
# 需要导入模块: from temba.contacts.models import Contact [as 别名]
# 或者: from temba.contacts.models.Contact import bulk_cache_initialize [as 别名]
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"}}],
#.........这里部分代码省略.........