Python haystack.inputs.Exact() Examples

The following are 9 code examples of haystack.inputs.Exact(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module haystack.inputs , or try the search function .
Example #1
Source File: filters.py    From aries-vcr with Apache License 2.0 6 votes vote down vote up
def build_query(self, **filters):
        inclusions = {}
        exclusions = None
        SQ = self.view.query_object
        exact_fields = getattr(self.view.serializer_class.Meta, "exact_fields", [])
        for qname, qvals in filters.items():
            if qname not in exact_fields:
                continue
            for qval in qvals:
                if qval:
                    filt = SQ(**{qname: Exact(qval)})
                    inclusions[qname] = (
                        (filt | inclusions[qname]) if qname in inclusions else filt
                    )
        inclusions = (
            functools.reduce(operator.and_, inclusions.values()) if inclusions else None
        )
        return inclusions, exclusions 
Example #2
Source File: filters.py    From aries-vcr with Apache License 2.0 6 votes vote down vote up
def build_query(self, **filters):
        inclusions = {}
        exclusions = None
        SQ = self.view.query_object
        status_fields = getattr(self.view.serializer_class.Meta, "status_fields", {})
        for qname, qval in status_fields.items():
            if qval and qval != "any":
                inclusions[qname] = SQ(**{qname: Exact(qval)})
        for qname, qvals in filters.items():
            if qname not in status_fields:
                continue
            for qval in qvals:
                if qval and qval != "any":
                    inclusions[qname] = SQ(**{qname: Exact(qval)})
                elif qname in inclusions:
                    del inclusions[qname]
        inclusions = (
            functools.reduce(operator.and_, inclusions.values()) if inclusions else None
        )
        return inclusions, exclusions 
Example #3
Source File: filters.py    From TheOrgBook with Apache License 2.0 6 votes vote down vote up
def build_query(self, **filters):
        inclusions = {}
        exclusions = None
        SQ = self.view.query_object
        status_fields = getattr(self.view.serializer_class.Meta, 'status_fields', {})
        for qname, qval in status_fields.items():
            if qval and qval != 'any':
                inclusions[qname] = SQ(**{qname: Exact(qval)})
        for qname, qvals in filters.items():
            if qname not in status_fields:
                continue
            for qval in qvals:
                if qval and qval != 'any':
                    inclusions[qname] = SQ(**{qname: Exact(qval)})
                elif qname in inclusions:
                    del inclusions[qname]
        inclusions = functools.reduce(operator.and_, inclusions.values()) if inclusions else None
        return inclusions, exclusions 
Example #4
Source File: filters.py    From aries-vcr with Apache License 2.0 5 votes vote down vote up
def build_name_query(self, term):
        SQ = self.view.query_object
        filter = super(CredNameFilterBuilder, self).build_name_query(term)
        if term and " " not in term:
            filter = filter | (SQ(source_id=Exact(term)) & SQ(name=Raw("*")))
        return filter 
Example #5
Source File: search_filters.py    From aries-vcr with Apache License 2.0 5 votes vote down vote up
def build_query(self, **filters):
        inclusions = {}
        exclusions = None
        SQ = self.view.query_object
        filter_fields_map = getattr(
            self.view.serializer_class.Meta, "filter_fields_map", ()
        )

        # For every parameter passed in the request
        for qname, qvals in filters.items():
            for qval in qvals:
                # Check if we track this filter type in the serializer
                # in `filter_fields_map` tuple
                if qname in filter_fields_map:
                    mapped_index_attributes = filter_fields_map[qname]
                    attr_inclusions = {}
                    # Construct an OR clause for this filter across all of the
                    # mapped index attributes
                    for mapped_attr in mapped_index_attributes:
                        attr_inclusions[mapped_attr] = SQ(**{mapped_attr: Exact(qval)})
                    attr_inclusions = (
                        functools.reduce(operator.or_, attr_inclusions.values())
                        if attr_inclusions
                        else None
                    )
                    # Add this sub OR clause to the top-level set of AND clauses
                    inclusions[mapped_attr] = attr_inclusions

        inclusions = (
            functools.reduce(operator.and_, inclusions.values()) if inclusions else None
        )

        return inclusions, exclusions 
Example #6
Source File: filters.py    From TheOrgBook with Apache License 2.0 5 votes vote down vote up
def build_name_query(self, term):
        SQ = self.view.query_object
        filter = super(CredNameFilterBuilder, self).build_name_query(term)
        if term and ' ' not in term:
            filter = filter | (SQ(source_id=Exact(term)) & SQ(name=Raw('*')))
        return filter 
Example #7
Source File: filters.py    From TheOrgBook with Apache License 2.0 5 votes vote down vote up
def build_query(self, **filters):
        inclusions = {}
        exclusions = None
        SQ = self.view.query_object
        exact_fields = getattr(self.view.serializer_class.Meta, 'exact_fields', [])
        for qname, qvals in filters.items():
            if qname not in exact_fields:
                continue
            for qval in qvals:
                if qval:
                    filt = SQ(**{qname: Exact(qval)})
                    inclusions[qname] = (filt | inclusions[qname]) if qname in inclusions else filt
        inclusions = functools.reduce(operator.and_, inclusions.values()) if inclusions else None
        return inclusions, exclusions 
Example #8
Source File: filters.py    From aries-vcr with Apache License 2.0 4 votes vote down vote up
def build_query(self, **filters):
        inclusions = {}
        exclusions = {}
        SQ = self.view.query_object
        for qname, qvals in filters.items():
            category = None
            by_value = False
            negate = False
            if ":" in qname:
                parts = qname.split(":", 1)
                if parts[0] == self.query_param:
                    category = parts[1]
                    if "__" in category:
                        category, oper = category.split("__", 1)
                        if oper == "not":
                            negate = True
                        elif oper != "exact":
                            continue
            else:
                if "__" in qname:
                    qname, oper = qname.split("__", 1)
                    if oper == "not":
                        negate = True
                    elif oper != "exact":
                        continue
                if qname == self.query_param:
                    by_value = True
            if not category and not by_value:
                continue
            target = exclusions if negate else inclusions
            for qv in qvals:
                if not qv:
                    continue
                if by_value:
                    if "::" not in qv:
                        continue
                    category, qv = qv.split("::", 1)
                filt = Exact("{}::{}".format(category, qv))
                sq_filt = SQ(**{self.query_param: filt})
                if category in target:
                    target[category] = target[category] | sq_filt
                else:
                    target[category] = sq_filt
        inclusions = (
            functools.reduce(operator.and_, inclusions.values()) if inclusions else None
        )
        exclusions = (
            functools.reduce(operator.and_, exclusions.values()) if exclusions else None
        )
        return inclusions, exclusions 
Example #9
Source File: filters.py    From TheOrgBook with Apache License 2.0 4 votes vote down vote up
def build_query(self, **filters):
        inclusions = {}
        exclusions = {}
        SQ = self.view.query_object
        for qname, qvals in filters.items():
            category = None
            by_value = False
            negate = False
            if ':' in qname:
                parts = qname.split(':', 1)
                if parts[0] == self.query_param:
                    category = parts[1]
                    if '__' in category:
                        category, oper = category.split('__', 1)
                        if oper == 'not':
                            negate = True
                        elif oper != 'exact':
                            continue
            else:
                if '__' in qname:
                    qname, oper = qname.split('__', 1)
                    if oper == 'not':
                        negate = True
                    elif oper != 'exact':
                        continue
                if qname == self.query_param:
                    by_value = True
            if not category and not by_value:
                continue
            target = exclusions if negate else inclusions
            for qv in qvals:
                if not qv:
                    continue
                if by_value:
                    if '::' not in qv:
                        continue
                    category, qv = qv.split('::', 1)
                filt = Exact('{}::{}'.format(category, qv))
                sq_filt = SQ(**{self.query_param: filt})
                if category in target:
                    target[category] = target[category] | sq_filt
                else:
                    target[category] = sq_filt
        inclusions = functools.reduce(operator.and_, inclusions.values()) if inclusions else None
        exclusions = functools.reduce(operator.and_, exclusions.values()) if exclusions else None
        return inclusions, exclusions