Skip to content

filter

notion.filter

Filter

Filter(filter_: dict)

Filter class for handling Notion Filter object. Please see https://developers.notion.com/reference/post-database-query-filter for detail.

You can now query to notion DB with filtering as following:

import SaigeToolkit.SaigeToolkit.notion as notion
filter_1 = notion.property.Select.get_filter("select_col", "equals", "sel1")
filter_2 = notion.property.Number.get_filter("number_col", "does_not_equal", 10)
filter_3 = notion.property.Checkbox.get_filter("checkbox_col", "does_not_equal", True)
filter_4 = notion.filter.Filter.compound("and", [filter_1, filter_2, filter_3])
_, _, results = notion.database.query_database(
    db_id,
    RESEARCH_API_PATH,
    request_body=filter_4.to_dict()
)

See notion.so/8cd58f07a254405ebb1bc327d690c8aa for more examples.

Source code in ResearchToolkit/notion/filter.py
def __init__(self, filter_: dict):
    assert isinstance(filter_, dict)
    self.filter = filter_

filter instance-attribute

filter = filter_

to_dict

to_dict()
Source code in ResearchToolkit/notion/filter.py
def to_dict(self):
    keys = list(self.filter.keys())
    if "property" in keys:
        return {"filter": self.filter}
    assert len(keys) == 1 and keys[0] in ["or", "and"]
    return {
        "filter": {keys[0]: [filter_.to_dict().get("filter") for filter_ in self.filter[keys[0]]]}
    }

compound classmethod

compound(op: str, filters: List[object])
Source code in ResearchToolkit/notion/filter.py
@classmethod
def compound(cls, op: str, filters: List[object]):
    assert op in {"or", "and"}
    return cls({op: filters})

get_avaliable_filter_ops

get_avaliable_filter_ops(property_name: str)
Source code in ResearchToolkit/notion/filter.py
def get_avaliable_filter_ops(property_name: str):

    none_type = type(None)

    text_filter_ops = {
        "equals": str,
        "does_not_equal": str,
        "contains": str,
        "does_not_contain": str,
        "starts_with": str,
        "ends_with": str,
        "is_empty": bool,
        "is_not_empty": bool,
    }

    number_filter_ops = {
        "equals": (int, float),
        "does_not_equal": (int, float),
        "greater_than": (int, float),
        "less_than": (int, float),
        "greater_than_or_equal_to": (int, float),
        "less_than_or_equal_to": (int, float),
        "is_empty": bool,
        "is_not_empty": bool,
    }

    checkbox_filter_ops = {"equals": bool, "does_not_equal": bool}

    select_filter_ops = {
        "equals": str,
        "does_not_equal": str,
        "is_empty": bool,
        "is_not_empty": bool,
    }

    multi_select_filter_ops = {
        "contains": str,
        "does_not_contain": str,
        "is_empty": bool,
        "is_not_empty": bool,
    }

    date_filter_ops = {
        "equals": str,
        "before": str,
        "after": str,
        "on_or_before": str,
        "is_empty": bool,
        "is_not_empty": bool,
        "on_or_after": str,
        "past_week": none_type,
        "past_month": none_type,
        "this_week": none_type,
        "next_week": none_type,
        "next_month": none_type,
        "next_year": none_type,
    }

    files_filter_ops = {
        "is_empty": bool,
        "is_not_empty": bool,
    }

    return {
        "title": text_filter_ops,
        "rich_text": text_filter_ops,
        "url": text_filter_ops,
        "email": text_filter_ops,
        "phone_number": text_filter_ops,
        "number": number_filter_ops,
        "checkbox": checkbox_filter_ops,
        "select": select_filter_ops,
        "multi_select": multi_select_filter_ops,
        "status": select_filter_ops,
        "date": date_filter_ops,
        "people": multi_select_filter_ops,
        "files": files_filter_ops,
        "relation": multi_select_filter_ops,
    }[property_name]