def apply_query_exclude(cls, queryset, options, value):
"""Apply `exclude` functional query.
:param queryset: Original queryset.
:param options: Filter options.
:param value: value to filter on.
:type queryset: elasticsearch_dsl.search.Search
:type options: dict
:type value: str
:return: Modified queryset.
:rtype: elasticsearch_dsl.search.Search
"""
__values = cls.split_lookup_value(value)
__queries = []
for __value in __values:
__queries.append(
~Q('term', **{options['field']: __value})
)
if __queries:
queryset = queryset.query(
six.moves.reduce(operator.or_, __queries)
)
return queryset
python类or_()的实例源码
def test(cls, nodelist):
# MUST NOT short-circuit evaluation, or invalid syntax can be skipped!
items = [
cls.interpret(nodelist[i])
for i in range(1, len(nodelist), 2)
]
return functools.reduce(operator.or_, items)
def test(cls, nodelist):
# MUST NOT short-circuit evaluation, or invalid syntax can be skipped!
items = [
cls.interpret(nodelist[i])
for i in range(1, len(nodelist), 2)
]
return functools.reduce(operator.or_, items)
def test_menu_action(self):
'Test that menu actions are accessible to menu\'s group'
with Transaction().start(DB_NAME, USER, context=CONTEXT):
pool = Pool()
Menu = pool.get('ir.ui.menu')
ModelData = pool.get('ir.model.data')
module_menus = ModelData.search([
('model', '=', 'ir.ui.menu'),
('module', '=', self.module),
])
menus = Menu.browse([mm.db_id for mm in module_menus])
for menu, module_menu in zip(menus, module_menus):
if not menu.action_keywords:
continue
menu_groups = set(menu.groups)
actions_groups = reduce(operator.or_,
(set(k.action.groups) for k in menu.action_keywords
if k.keyword == 'tree_open'))
if not actions_groups:
continue
assert menu_groups <= actions_groups, (
'Menu "%(menu_xml_id)s" actions are not accessible to '
'%(groups)s' % {
'menu_xml_id': module_menu.fs_id,
'groups': ','.join(g.name
for g in menu_groups - actions_groups),
})
def get_search_results(self, request, queryset, search_term):
"""
Returns a tuple containing a queryset to implement the search,
and a boolean indicating if the results may contain duplicates.
"""
# Apply keyword searches.
def construct_search(field_name):
if field_name.startswith('^'):
return "%s__istartswith" % field_name[1:]
elif field_name.startswith('='):
return "%s__iexact" % field_name[1:]
elif field_name.startswith('@'):
return "%s__search" % field_name[1:]
else:
return "%s__icontains" % field_name
use_distinct = False
search_fields = self.get_search_fields(request)
if search_fields and search_term:
orm_lookups = [construct_search(str(search_field))
for search_field in search_fields]
for bit in search_term.split():
or_queries = [models.Q(**{orm_lookup: bit})
for orm_lookup in orm_lookups]
queryset = queryset.filter(reduce(operator.or_, or_queries))
if not use_distinct:
for search_spec in orm_lookups:
if lookup_needs_distinct(self.opts, search_spec):
use_distinct = True
break
return queryset, use_distinct
def test(cls, nodelist):
# MUST NOT short-circuit evaluation, or invalid syntax can be skipped!
items = [
cls.interpret(nodelist[i])
for i in range(1, len(nodelist), 2)
]
return functools.reduce(operator.or_, items)
def get_search_results(self, request, queryset, search_term):
"""
Returns a tuple containing a queryset to implement the search,
and a boolean indicating if the results may contain duplicates.
"""
# Apply keyword searches.
def construct_search(field_name):
if field_name.startswith('^'):
return "%s__istartswith" % field_name[1:]
elif field_name.startswith('='):
return "%s__iexact" % field_name[1:]
elif field_name.startswith('@'):
return "%s__search" % field_name[1:]
else:
return "%s__icontains" % field_name
use_distinct = False
search_fields = self.get_search_fields(request)
if search_fields and search_term:
orm_lookups = [construct_search(str(search_field))
for search_field in search_fields]
for bit in search_term.split():
or_queries = [models.Q(**{orm_lookup: bit})
for orm_lookup in orm_lookups]
queryset = queryset.filter(reduce(operator.or_, or_queries))
if not use_distinct:
for search_spec in orm_lookups:
if lookup_needs_distinct(self.opts, search_spec):
use_distinct = True
break
return queryset, use_distinct
def orwhere(self, *expressions):
self._where = self._add_query_clauses(
self._where, expressions, operator.or_)
def test(cls, nodelist):
# MUST NOT short-circuit evaluation, or invalid syntax can be skipped!
return functools.reduce(operator.or_, [cls.interpret(nodelist[i]) for i in range(1,len(nodelist),2)])
def union(dfa1, dfa2):
return product(dfa1, operator.or_, dfa2)
def get_query(self, request, term):
qs = self.get_queryset()
if term:
search_filters = []
if len(term.split()) == 1:
if self.search_fields:
for field in self.search_fields:
search_filters.append(Q(**{field: term}))
qs = qs.filter(reduce(operator.or_, search_filters))
else:
# Accounts for 'John Doe' term; will compare against get_full_name
term = term.lower()
qs = [x for x in qs if term in x.get_full_name().lower()]
return qs
def get_search_filter(self, keyword):
search_fields = getattr(self, 'search_fields', None)
if search_fields is None:
search_fields = self.fields # Assume all fields
field_queries = list(
zip(map(lambda x: '%s__icontains' % x, search_fields),
(keyword,)*len(search_fields))
)
lookups = [Q(x) for x in field_queries]
return reduce(operator.or_, lookups)
def _zero_mantissa(dval):
"""Determine whether the mantissa bits of the given double are all
zero."""
bb = _double_as_bytes(dval)
return ((bb[1] & 0x0f) | reduce(operator.or_, bb[2:])) == 0
##
## Functions to test for IEEE 754 special values
##
def get_search_results(self, request, queryset, search_term):
"""
Returns a tuple containing a queryset to implement the search,
and a boolean indicating if the results may contain duplicates.
"""
# Apply keyword searches.
def construct_search(field_name):
if field_name.startswith('^'):
return "%s__istartswith" % field_name[1:]
elif field_name.startswith('='):
return "%s__iexact" % field_name[1:]
elif field_name.startswith('@'):
return "%s__search" % field_name[1:]
else:
return "%s__icontains" % field_name
use_distinct = False
search_fields = self.get_search_fields(request)
if search_fields and search_term:
orm_lookups = [construct_search(str(search_field))
for search_field in search_fields]
for bit in search_term.split():
or_queries = [models.Q(**{orm_lookup: bit})
for orm_lookup in orm_lookups]
queryset = queryset.filter(reduce(operator.or_, or_queries))
if not use_distinct:
for search_spec in orm_lookups:
if lookup_needs_distinct(self.opts, search_spec):
use_distinct = True
break
return queryset, use_distinct
def test_or_scalar(self):
self.check_array_scalar_op(operator.or_)
def test_or_scalarzero(self):
self.check_array_scalarzero_op(operator.or_)
def test_ror_scalarzero(self):
self.check_array_scalarzero_op(operator.or_, swap=True)
def test_or_array(self):
self.check_array_array_op(operator.or_)
def test_doubly_broadcasted_or(self):
self.check_array_doubly_broadcasted_op(operator.or_)
def get_user_filters(user):
"""
Returns `django.db.models.query.QuerySet` filters based on allowed category for given user
"""
where = [Q()]
user_specific_where = []
abuse_permissions = AbusePermission.objects.filter(user=user.id)
for perm in abuse_permissions:
if perm.profile.name == 'Expert':
user_specific_where.append(Q(category=perm.category))
elif perm.profile.name == 'Advanced':
user_specific_where.append(Q(category=perm.category, confidential=False))
elif perm.profile.name == 'Beginner':
user_specific_where.append(Q(
priority__in=USER_FILTERS_BEGINNER_PRIORITY,
category=perm.category,
confidential=False,
escalated=False,
moderation=False
))
if len(user_specific_where):
user_specific_where = reduce(operator.or_, user_specific_where)
where.append(user_specific_where)
else:
# If no category allowed
where.append(Q(category=None))
return where