예제 #1
0
    def test_complex(self):
        """
        The complex thing
        """

        #what it does is creates a Q on the
        #fly and ORs it with set_compexq

        tmp_q = self.q1.set_compexq((Q(a=True, b=True) | Q(c=False, d=True)),
                                    "OR")
        assert tmp_q.result == True

        tmp_q = self.q1.set_compexq((Q(a=True, b=False) | Q(c=False, d=False)),
                                    "AND")
        assert tmp_q.result == False
예제 #2
0
    def __main_filter(self, outside_connector, inside_connector, *args,
                      **kwargs):
        """
        Common OR and AND operation we do.

        @param outside_connector : The connector between two chained query
        @param inside_connector  : The connector inside the query
        """
        temp_q = Q(*args, **kwargs)
        if inside_connector == "OR":
            #because the default is AND
            temp_q.connector = inside_connector
        if self.q:
            current_q = deepcopy(self.q)
        else:
            current_q = None

        if not current_q:
            current_q = temp_q
        else:
            if outside_connector == "OR":
                current_q = current_q | temp_q
            else:
                current_q = current_q & temp_q

        fresh_query = self._clone(q_object=current_q,
                                  pull_result=self.pull_result)
        return fresh_query
예제 #3
0
    def exclude(self, *args, **kwargs):
        """
        Useful when you want to ignore some of the things
        in query,the exclude iverts the query NOT used so
        much ...
        """
        temp_q = ~Q(*args, **kwargs)

        if self.q:
            current_q = deepcopy(self.q)
        else:
            current_q = None

        if not self.q:
            current_q = temp_q
        else:
            current_q.add(temp_q, "AND")
        fresh_query = self._clone(q_object=current_q,
                                  pull_result=self.pull_result)
        return fresh_query
예제 #4
0
    def __traverse_deserialize(self, traverse_object):
        """
        The private recursive part that traverses
        the incoming overlord list (included with facts)
        and produces a Q object query object from it.
        **(heavy recursive code)Caution any change here 
        can make facts not tobe deserialized correctly so
        that will break the facts API.

        @type  : list
        @param : List of overlord incoming facts query
                 something like :[NOT,[AND,[a,TRUE,b,FALSE]]]
 
        @return : Q object returned
        """

        q_object = None
        #lets try divide and conquer :)
        #assume that it is [NOT,[AND,[a,TRUE,b,FALSE]]]

        #print "The traverse object at start is ",traverse_object
        tmp_negated = False
        tmp_connector = "AND"
        if type(traverse_object[0]) == str and traverse_object[0] == "NOT":
            tmp_negated = True
            #q_object.negated = ~q_object
            traverse_object = traverse_object[1:][0]
            #print "After NOT the traverse_object is ",traverse_object
            #raw_input()
        if type(traverse_object[0]) == str and traverse_object[0] in [
                "AND", "OR"
        ]:
            #q_object.connector = traverse_object[0]
            tmp_connector = traverse_object[0]
            traverse_object = traverse_object[1:][0]
            #print "After CONNETOR the traverse_object is ",traverse_object
            #raw_input()

        if type(traverse_object[0]
                ) == str and not traverse_object[0] in self.VALID_QUERY_KEYS:
            #print "In children : ",traverse_object
            for ch in xrange(0, len(traverse_object), 2):
                #q_object.add(Q(tuple(traverse_object[ch:ch+2])),q_object.connector)
                #print "We work on ",traverse_object[ch:ch+2]
                if not q_object:
                    q_object = Q(tuple(traverse_object[ch:ch + 2]))
                    q_object.connector = tmp_connector
                else:
                    if q_object.connector == "OR":
                        q_object = q_object | Q(
                            tuple(traverse_object[ch:ch + 2]))
                    else:
                        q_object = q_object & Q(
                            tuple(traverse_object[ch:ch + 2]))
            if tmp_negated:
                q_object = ~q_object

                #print "IN children Q object is ",q_object
            traverse_object = []
            #print "After CHILDREN the traverse_object is ",traverse_object
            #raw_input()

        if traverse_object:

            #print "The traverse object at end is ",traverse_object
            #raw_input()
            for t_o in traverse_object:
                #print "The traverse object at end is ",t_o
                #raw_input()

                tmp_q = self.__traverse_deserialize(t_o)
                #print "I ADD THAT TO THE ALL ",tmp_q
                #print "WILL BE ADDED TO  ",q_object
                if not q_object:
                    q_object = Q()
                    q_object.connector = tmp_connector
                #q_object.add(tmp_q,q_object.connector)
                if tmp_connector == "OR":
                    q_object = q_object | tmp_q
                else:
                    q_object = q_object & tmp_q
                    #print "AFTER ADDITION ",q_object
            if tmp_negated:
                q_object = ~q_object

        return q_object
예제 #5
0
 def setUp(self):
     self.q1 = FuncLogicQuery(Q(a=True, b=True))
     self.q2 = FuncLogicQuery(Q(a=False, b=True))
     self.q3 = FuncLogicQuery(Q(a=True, b=False))
     self.q4 = FuncLogicQuery(Q(a=False, b=False))
     self.q_negated = FuncLogicQuery(~Q(a=False, b=False))