Exemplo n.º 1
0
    def test_datetime_condition(self):
        today_stamp = unix_timestamp(date.today())
        with FilterCtxStrict('age__eq', today_stamp) as value:
            self.assertEqual(value, 'age=%s' % today_stamp)

        now_stamp = unix_timestamp(datetime.now())
        with FilterCtxStrict('age__eq', now_stamp) as value:
            self.assertEqual(value, 'age=%s' % now_stamp)
Exemplo n.º 2
0
    def test_datetime_condition(self):
        today_stamp = unix_timestamp(date.today())
        with FilterCtxStrict('age__eq', today_stamp) as value:
            self.assertEqual(value, 'age=%s' % today_stamp)

        now_stamp = unix_timestamp(datetime.now())
        with FilterCtxStrict('age__eq', now_stamp) as value:
            self.assertEqual(value, 'age=%s' % now_stamp)
Exemplo n.º 3
0
    def __enter__(self):
        v_attr = self.v_attr
        if isinstance(v_attr, six.string_types):
            v_attr = int_from_digit(v_attr, is_strict=self.is_strict)
        if isinstance(self.v_attr, (tuple, list)):
            v_attr = list_of_integers_only(v_attr, is_strict=self.is_strict)
        if isinstance(self.v_attr, (datetime, date)):
            v_attr = unix_timestamp(self.v_attr)

        if not v_attr and v_attr != 0:
            return None

        for ending in self._allowed_conditions_map.keys():
            if not self.k_attr.endswith(ending):
                continue

            if (
                ending not in ("__between", "__in")
                and isinstance(v_attr, (tuple, list))
                and not self.__exit__(
                    exc_val=SphinxQLSyntaxException(
                        "%s found but not allowed for %s condition" % (self.v_attr, self.k_attr)
                    )
                )
            ):
                continue

            if (
                ending in ("__between", "__in")
                and not isinstance(v_attr, (tuple, list))
                and not self.__exit__(
                    exc_val=SphinxQLSyntaxException(
                        "%s condition found but the type of %s is not list or tuple" % (self.k_attr, self.v_attr)
                    )
                )
            ):
                continue

            a = self.k_attr[: self.k_attr.rindex(ending)]
            v = v_attr

            if ending == "__between":
                if (len(v_attr) != 2 or len(v_attr) != len(self.v_attr)) and not self.__exit__(
                    exc_val=SphinxQLSyntaxException(
                        "%s condition wants a pair value and %s is not" % (self.k_attr, self.v_attr)
                    )
                ):
                    continue

                f_v, s_v = v_attr
                return self._allowed_conditions_map[ending].format(a=a, f_v=f_v, s_v=s_v)

            if ending == "__in":
                v = ",".join([str(v) for v in v_attr])

            return self._allowed_conditions_map[ending].format(a=a, v=v)

        return self.__exit__(exc_val=SphinxQLSyntaxException("%s is invalid condition" % self.k_attr))
Exemplo n.º 4
0
 def test_with_time_filter(self):
     search = Search(['company'], config=SearchConfig)
     search = search.match('Yandex').filter(date_created__lte=datetime.date.today())
     today = datetime.date.today()
     sxql = (
         "SELECT * FROM company WHERE MATCH('Yandex') "
         "AND date_created<=%s" % unix_timestamp(today)
     )
     self.assertEqual(search.lex(), sxql)
Exemplo n.º 5
0
    def __enter__(self):
        v_attr = self.v_attr
        if isinstance(v_attr, six.string_types):
            try:
                v_attr = int_from_digit(v_attr, is_strict=self.is_strict)
            except SphinxQLSyntaxException:
                pass
        if isinstance(self.v_attr, (tuple, list)):
            v_attr = list_of_integers_only(v_attr, is_strict=self.is_strict)
        if isinstance(self.v_attr, (datetime, date)):
            v_attr = unix_timestamp(self.v_attr)

        if not v_attr and v_attr != 0:
            return None

        for ending in self._allowed_conditions_map.keys():
            if not self.k_attr.endswith(ending):
                continue

            if (ending not in ('__between', '__in')
                    and isinstance(v_attr, (tuple, list))
                    and not self.__exit__(exc_val=SphinxQLSyntaxException(
                        '%s found but not allowed for %s condition' %
                        (self.v_attr, self.k_attr)))):
                continue

            if (ending in ('__between', '__in') and not isinstance(
                    v_attr, (tuple, list)
            ) and not self.__exit__(exc_val=SphinxQLSyntaxException(
                    '%s condition found but the type of %s is not list or tuple'
                    % (self.k_attr, self.v_attr)))):
                continue

            a = self.k_attr[:self.k_attr.rindex(ending)]
            v = v_attr

            if ending == '__between':
                if ((len(v_attr) != 2 or len(v_attr) != len(self.v_attr))
                        and not self.__exit__(exc_val=SphinxQLSyntaxException(
                            '%s condition wants a pair value and %s is not' %
                            (self.k_attr, self.v_attr)))):
                    continue

                f_v, s_v = v_attr
                return self._allowed_conditions_map[ending].format(a=a,
                                                                   f_v=f_v,
                                                                   s_v=s_v)

            if ending == '__in':
                v = ','.join([str(v) for v in v_attr])

            return self._allowed_conditions_map[ending].format(a=a, v=v)

        return self.__exit__(
            exc_val=SphinxQLSyntaxException('%s is invalid condition' %
                                            self.k_attr))
Exemplo n.º 6
0
    def __enter__(self):
        v_attr = self.v_attr
        if isinstance(v_attr, six.string_types):
            try:
                v_attr = int_from_digit(
                    v_attr,
                    is_strict=self.is_strict
                )
            except SphinxQLSyntaxException:
                v_attr = string_for_value(v_attr, is_strict=self.is_strict)

        if isinstance(self.v_attr, (tuple, list)):
            # This tuple or list can be list of integer or strings
            try:
                v_attr = list_of_integers_only(
                                            v_attr,
                                            is_strict=self.is_strict
                                            )
            except SphinxQLSyntaxException:
                #If This list is not integer check if it is strings
                v_attr = list_of_strings_only(
                                            v_attr,
                                            is_strict=self.is_strict
                                            )

        if isinstance(self.v_attr, (datetime, date)):
            v_attr = unix_timestamp(self.v_attr)

        if not v_attr and v_attr != 0:
            return None

        for ending in self._allowed_conditions_map.keys():
            if not self.k_attr.endswith(ending):
                continue

            if (
                ending not in ('__between', '__in', '__notin')
                and isinstance(v_attr, (tuple, list))
                and not self.__exit__(
                    exc_val=SphinxQLSyntaxException(
                        '%s found but not allowed for %s condition' %
                        (self.v_attr, self.k_attr)
                    )
                )
            ):
                continue

            if (
                ending in ('__between', '__in', '__notin')
                and not isinstance(v_attr, (tuple, list))
                and not self.__exit__(
                    exc_val=SphinxQLSyntaxException(
                        '%s condition found but the type of %s is not list or tuple' %
                        (self.k_attr, self.v_attr)
                    )
                )
            ):
                continue

            a = self.k_attr[:self.k_attr.rindex(ending)]
            v = v_attr

            if ending == '__between':
                if (
                    (len(v_attr) != 2 or len(v_attr) != len(self.v_attr))
                    and not self.__exit__(
                        exc_val=SphinxQLSyntaxException(
                            '%s condition wants a pair value and %s is not' %
                            (self.k_attr, self.v_attr)
                        )
                    )
                ):
                    continue

                f_v, s_v = v_attr
                return self._allowed_conditions_map[ending].format(
                    a=a,
                    f_v=f_v,
                    s_v=s_v
                )

            if ending in ('__in', '__notin'):
                v = ','.join([str(v) for v in v_attr])

            return self._allowed_conditions_map[ending].format(a=a, v=v)

        return self.__exit__(
            exc_val=SphinxQLSyntaxException(
                '%s is invalid condition' % self.k_attr
            )
        )
Exemplo n.º 7
0
    def __enter__(self):
        v_attr = self.v_attr
        if isinstance(v_attr, six.string_types):
            v_attr = int_from_digit(
                v_attr,
                is_strict=self.is_strict
            )
        if isinstance(self.v_attr, (tuple, list)):

            # for v in self.v_attr:
            #     if not isinstance(v, (int, float)) and not self.__exit__(
            #         exc_val=SphinxQLSyntaxException(
            #             'value %s must be instance of int or float' % v
            #         )
            #     ):
            # continue
            pass
        if isinstance(self.v_attr, (datetime, date)):
            v_attr = unix_timestamp(self.v_attr)

        if not v_attr and v_attr != 0:
            return None

        for ending in self._allowed_conditions_map.keys():
            if not self.k_attr.endswith(ending):
                continue

            if (
                ending not in ('__between', '__in')
                and isinstance(v_attr, (tuple, list))
                and not self.__exit__(
                    exc_val=SphinxQLSyntaxException(
                        '%s found but not allowed for %s condition' %
                        (self.v_attr, self.k_attr)
                    )
                )
            ):
                continue

            if (
                ending in ('__between', '__in')
                and not isinstance(v_attr, (tuple, list))
                and not self.__exit__(
                    exc_val=SphinxQLSyntaxException(
                        '%s condition found but the type of %s is not list or tuple' %
                        (self.k_attr, self.v_attr)
                    )
                )
            ):
                continue

            a = self.k_attr[:self.k_attr.rindex(ending)]
            v = v_attr

            if ending == '__between':
                if (
                    (len(v_attr) != 2 or len(v_attr) != len(self.v_attr))
                    and not self.__exit__(
                        exc_val=SphinxQLSyntaxException(
                            '%s condition wants a pair value and %s is not' %
                            (self.k_attr, self.v_attr)
                        )
                    )
                ):
                    continue

                f_v, s_v = v_attr
                return self._allowed_conditions_map[ending].format(
                    a=a,
                    f_v=f_v,
                    s_v=s_v
                )

            if ending == '__in':
                v = ','.join([str(v) for v in v_attr])

            return self._allowed_conditions_map[ending].format(a=a, v=v)

        return self.__exit__(
            exc_val=SphinxQLSyntaxException(
                '%s is invalid condition' % self.k_attr
            )
        )