Skip to content

Commit

Permalink
Merge pull request #763 from LeXofLeviafan/filter-contains
Browse files Browse the repository at this point in the history
added contains/not-contains filter types
  • Loading branch information
jarun authored Aug 24, 2024
2 parents d0a5824 + c775c01 commit 314861a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 74 deletions.
10 changes: 10 additions & 0 deletions bukuserver/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ def not_equal_func(query, value, index):
return filter(lambda x: x[index] != value, query)


def contains_func(query, value, index):
return filter(lambda x: value in x[index], query)


def not_contains_func(query, value, index):
return filter(lambda x: value not in x[index], query)


def greater_func(query, value, index):
return filter(lambda x: x[index] > value, query)

Expand Down Expand Up @@ -51,6 +59,8 @@ class FilterType(Enum):

EQUAL = {'func': equal_func, 'text':'equals'}
NOT_EQUAL = {'func': not_equal_func, 'text':'not equal'}
CONTAINS = {'func': contains_func, 'text':'contains'}
NOT_CONTAINS = {'func': not_contains_func, 'text':'not contains'}
GREATER = {'func': greater_func, 'text':'greater than'}
SMALLER = {'func': smaller_func, 'text':'smaller than'}
IN_LIST = {'func': in_list_func, 'text':'in list'}
Expand Down
124 changes: 50 additions & 74 deletions bukuserver/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,57 +291,39 @@ def scaffold_filters(self, name):
)
)
elif name == BookmarkField.ID.name.lower():
res.extend(
[
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.EQUAL),
bs_filters.BookmarkBaseFilter(
name, filter_type=FilterType.NOT_EQUAL
),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.IN_LIST),
bs_filters.BookmarkBaseFilter(
name, filter_type=FilterType.NOT_IN_LIST
),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.GREATER),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.SMALLER),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.TOP_X),
bs_filters.BookmarkBaseFilter(
name, filter_type=FilterType.BOTTOM_X
),
]
)
res += [
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.EQUAL),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.NOT_EQUAL),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.IN_LIST),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.NOT_IN_LIST),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.GREATER),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.SMALLER),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.TOP_X),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.BOTTOM_X),
]
elif name == BookmarkField.URL.name.lower():

def netloc_match_func(query, value, index):
return filter(lambda x: urlparse(x[index]).netloc == value, query)

res.extend(
[
bs_filters.BookmarkBaseFilter(
name, "netloc match", netloc_match_func
),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.EQUAL),
bs_filters.BookmarkBaseFilter(
name, filter_type=FilterType.NOT_EQUAL
),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.IN_LIST),
bs_filters.BookmarkBaseFilter(
name, filter_type=FilterType.NOT_IN_LIST
),
]
)
res += [
bs_filters.BookmarkBaseFilter(name, "netloc match", netloc_match_func),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.EQUAL),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.NOT_EQUAL),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.IN_LIST),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.NOT_IN_LIST),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.CONTAINS),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.NOT_CONTAINS),
]
elif name == BookmarkField.TITLE.name.lower():
res.extend(
[
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.EQUAL),
bs_filters.BookmarkBaseFilter(
name, filter_type=FilterType.NOT_EQUAL
),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.IN_LIST),
bs_filters.BookmarkBaseFilter(
name, filter_type=FilterType.NOT_IN_LIST
),
]
)
res += [
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.EQUAL),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.NOT_EQUAL),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.IN_LIST),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.NOT_IN_LIST),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.CONTAINS),
bs_filters.BookmarkBaseFilter(name, filter_type=FilterType.NOT_CONTAINS),
]
elif name == BookmarkField.TAGS.name.lower():

def get_list_from_buku_tags(item):
Expand All @@ -357,16 +339,14 @@ def tags_not_contain_func(query, value, index):
if value not in get_list_from_buku_tags(item[index]):
yield item

res.extend(
[
bs_filters.BookmarkBaseFilter(name, "contain", tags_contain_func),
bs_filters.BookmarkBaseFilter(name, "not contain", tags_not_contain_func),
bs_filters.BookmarkTagNumberEqualFilter(name, "number equal"),
bs_filters.BookmarkTagNumberNotEqualFilter(name, "number not equal"),
bs_filters.BookmarkTagNumberGreaterFilter(name, "number greater than"),
bs_filters.BookmarkTagNumberSmallerFilter(name, "number smaller than"),
]
)
res += [
bs_filters.BookmarkBaseFilter(name, "contain", tags_contain_func),
bs_filters.BookmarkBaseFilter(name, "not contain", tags_not_contain_func),
bs_filters.BookmarkTagNumberEqualFilter(name, "number equal"),
bs_filters.BookmarkTagNumberNotEqualFilter(name, "number not equal"),
bs_filters.BookmarkTagNumberGreaterFilter(name, "number greater than"),
bs_filters.BookmarkTagNumberSmallerFilter(name, "number smaller than"),
]
elif name in self.scaffold_list_columns():
pass
else:
Expand Down Expand Up @@ -508,26 +488,22 @@ def top_most_common_func(query, value, index):
most_common_item = [x[0] for x in most_common]
return filter(lambda x: x[index] in most_common_item, query)

res.extend(
[
bs_filters.TagBaseFilter(name, filter_type=FilterType.EQUAL),
bs_filters.TagBaseFilter(name, filter_type=FilterType.NOT_EQUAL),
bs_filters.TagBaseFilter(name, filter_type=FilterType.IN_LIST),
bs_filters.TagBaseFilter(name, filter_type=FilterType.NOT_IN_LIST),
]
)
res += [
bs_filters.TagBaseFilter(name, filter_type=FilterType.EQUAL),
bs_filters.TagBaseFilter(name, filter_type=FilterType.NOT_EQUAL),
bs_filters.TagBaseFilter(name, filter_type=FilterType.IN_LIST),
bs_filters.TagBaseFilter(name, filter_type=FilterType.NOT_IN_LIST),
bs_filters.TagBaseFilter(name, filter_type=FilterType.CONTAINS),
bs_filters.TagBaseFilter(name, filter_type=FilterType.NOT_CONTAINS),
]
if name == "usage_count":
res.extend(
[
bs_filters.TagBaseFilter(name, filter_type=FilterType.GREATER),
bs_filters.TagBaseFilter(name, filter_type=FilterType.SMALLER),
bs_filters.TagBaseFilter(name, filter_type=FilterType.TOP_X),
bs_filters.TagBaseFilter(name, filter_type=FilterType.BOTTOM_X),
bs_filters.TagBaseFilter(
name, "top most common", top_most_common_func
),
]
)
res += [
bs_filters.TagBaseFilter(name, filter_type=FilterType.GREATER),
bs_filters.TagBaseFilter(name, filter_type=FilterType.SMALLER),
bs_filters.TagBaseFilter(name, filter_type=FilterType.TOP_X),
bs_filters.TagBaseFilter(name, filter_type=FilterType.BOTTOM_X),
bs_filters.TagBaseFilter(name, "top most common", top_most_common_func),
]
elif name == "name":
pass
else:
Expand Down

0 comments on commit 314861a

Please sign in to comment.