Exemplo n.º 1
0
def test_is_valid_regex():
    valid_pattern = [r"Test", r"\d+", r"^test1234", r"^\s{4}", r"(asdf)*"]
    invalid_pattern = [r"\g++", r"^tes[t1234", r"^\s4}", r"(asdf*"]

    for pattern in valid_pattern:
        assert utils.is_valid_regex(pattern) is True, pattern

    for pattern in invalid_pattern:
        assert utils.is_valid_regex(pattern) is False, pattern

    assert utils.is_valid_regex(None) is False
    assert utils.is_valid_regex(2) is False
    assert utils.is_valid_regex(["moh"]) is False
Exemplo n.º 2
0
    def filter_queryset(self, qs):
        # use request parameters to filter queryset
        search_string = self.request.GET.get('search[value]', None)

        product_id_get_param = 'columns[' + str(self.column_order["product_id"]) + '][search][value]'
        description_get_param = 'columns[' + str(self.column_order["description"]) + '][search][value]'
        list_price_get_param = 'columns[' + str(self.column_order["list_price"]) + '][search][value]'
        tags_get_param = 'columns[' + str(self.column_order["tags"]) + '][search][value]'

        product_id_search = self.request.GET.get(product_id_get_param, None)
        description_search = self.request.GET.get(description_get_param, None)
        list_price_search = self.request.GET.get(list_price_get_param, None)
        tags_search = self.request.GET.get(tags_get_param, None)

        if search_string:
            # by default, the search field searched in the Product ID and the description field
            if is_valid_regex(search_string):
                qs = qs.filter(Q(product_id__regex=search_string) |
                               Q(description__regex=search_string))
            else:
                qs = qs.filter(Q(product_id__contains=search_string) |
                               Q(description__contains=search_string))

        if product_id_search:
            if is_valid_regex(product_id_search):
                qs = qs.filter(product_id__regex=product_id_search)
            else:
                qs = qs.filter(product_id__contains=product_id_search)

        if description_search:
            if is_valid_regex(description_search):
                qs = qs.filter(description__regex=description_search)
            else:
                qs = qs.filter(description__contains=description_search)

        if list_price_search:
            if is_valid_regex(list_price_search):
                qs = qs.filter(list_price__regex=list_price_search)
            else:
                qs = qs.filter(list_price__contains=list_price_search)

        if tags_search:
            if is_valid_regex(tags_search):
                qs = qs.filter(tags__regex=tags_search)
            else:
                qs = qs.filter(tags__contains=tags_search)

        return qs
def test_is_valid_regex():
    valid_pattern = [
        r"Test",
        r"\d+",
        r"^test1234",
        r"^\s{4}",
        r"(asdf)*"
    ]
    invalid_pattern = [
        r"\g++",
        r"^tes[t1234",
        r"^\s4}",
        r"(asdf*"
    ]

    for pattern in valid_pattern:
        assert utils.is_valid_regex(pattern) is True, pattern

    for pattern in invalid_pattern:
        assert utils.is_valid_regex(pattern) is False, pattern

    assert utils.is_valid_regex(None) is False
    assert utils.is_valid_regex(2) is False
    assert utils.is_valid_regex(["moh"]) is False
Exemplo n.º 4
0
    def filter_queryset(self, qs):
        search_string = self.request.GET.get('search[value]', None)
        try_regex = get_try_regex_from_user_profile(self.request)

        if search_string:
            # search in the Product Group name and Vendor name by default
            operation = "iregex" if is_valid_regex(search_string) and try_regex else "icontains"
            qs = qs.filter(
                Q(**{"product_id__%s" % operation: search_string}) |
                Q(**{"description__%s" % operation: search_string})
            )

        # apply column based search
        qs = self.apply_column_based_search(request=self.request, query_set=qs, try_regex=try_regex)

        return qs
Exemplo n.º 5
0
    def filter_queryset(self, qs):
        search_string = self.request.GET.get('search[value]', None)
        try_regex = get_try_regex_from_user_profile(self.request)

        if search_string:
            # search in the Product Group name and Vendor name by default
            operation = "iregex" if is_valid_regex(
                search_string) and try_regex else "icontains"
            qs = qs.filter(
                Q(**{"product_id__%s" % operation: search_string})
                | Q(**{"description__%s" % operation: search_string}))

        # apply column based search
        qs = self.apply_column_based_search(request=self.request,
                                            query_set=qs,
                                            try_regex=try_regex)

        return qs
Exemplo n.º 6
0
    def apply_column_based_search(self, request, query_set, try_regex=True):
        """
        apply the column based search parameters from datatables to the query_set

        :param request: the request object
        :param query_set: the query_set that should be used to apply the filters
        :param try_regex: indicates that the search term should try regular expression first
        """
        # apply column based search parameters
        for name, param in self.column_based_filter.items():
            get_param = 'columns[' + str(param["order"]) + '][search][value]'
            column_search_string = request.GET.get(get_param, None)

            if column_search_string:
                query_set = query_set.filter(
                    **{
                        "%s__%s" % (param["expr"], "iregex" if is_valid_regex(column_search_string) and try_regex else "icontains"):
                        column_search_string
                    })
        return query_set
Exemplo n.º 7
0
    def apply_column_based_search(self, request, query_set, try_regex=True):
        """
        apply the column based search parameters from datatables to the query_set

        :param request: the request object
        :param query_set: the query_set that should be used to apply the filters
        :param try_regex: indicates that the search term should try regular expression first
        """
        # apply column based search parameters
        for name, param in self.column_based_filter.items():
            get_param = 'columns[' + str(param["order"]) + '][search][value]'
            column_search_string = request.GET.get(get_param, None)

            if column_search_string:
                query_set = query_set.filter(**{
                    "%s__%s" % (
                        param["expr"],
                        "iregex" if is_valid_regex(column_search_string) and try_regex else "icontains"
                    ): column_search_string
                })
        return query_set