How to use the notion.maps.field_map function in notion

To help you get started, we’ve selected a few notion examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jamalex / notion-py / notion / block.py View on Github external
height = field_map("format.block_height")
    full_width = field_map("format.block_full_width")
    page_width = field_map("format.block_page_width")
    width = field_map("format.block_width")

    def set_source_url(self, url):
        self.source = remove_signed_prefix_as_needed(url)
        self.display_source = get_embed_link(self.source)

    def _str_fields(self):
        return super()._str_fields() + ["source"]


class EmbedOrUploadBlock(EmbedBlock):

    file_id = field_map(["file_ids", 0])

    def upload_file(self, path):

        mimetype = mimetypes.guess_type(path)[0] or "text/plain"
        filename = os.path.split(path)[-1]

        data = self._client.post(
            "getUploadFileUrl",
            {"bucket": "secure", "name": filename, "contentType": mimetype},
        ).json()

        with open(path, "rb") as f:
            response = requests.put(
                data["signedPutUrl"], data=f, headers={"Content-type": mimetype}
            )
            response.raise_for_status()
github jamalex / notion-py / notion / block.py View on Github external
_type = "embed"

    display_source = field_map(
        "format.display_source",
        api_to_python=add_signed_prefix_as_needed,
        python_to_api=remove_signed_prefix_as_needed,
    )
    source = property_map(
        "source",
        api_to_python=add_signed_prefix_as_needed,
        python_to_api=remove_signed_prefix_as_needed,
    )
    height = field_map("format.block_height")
    full_width = field_map("format.block_full_width")
    page_width = field_map("format.block_page_width")
    width = field_map("format.block_width")

    def set_source_url(self, url):
        self.source = remove_signed_prefix_as_needed(url)
        self.display_source = get_embed_link(self.source)

    def _str_fields(self):
        return super()._str_fields() + ["source"]


class EmbedOrUploadBlock(EmbedBlock):

    file_id = field_map(["file_ids", 0])

    def upload_file(self, path):
github jamalex / notion-py / notion / space.py View on Github external
from .logger import logger
from .maps import property_map, field_map
from .records import Record


class Space(Record):

    _table = "space"

    child_list_key = "pages"

    name = field_map("name")
    domain = field_map("domain")
    icon = field_map("icon")

    @property
    def pages(self):
        # The page list includes pages the current user might not have permissions on, so it's slow to query.
        # Instead, we just filter for pages with the space as the parent.
        return self._client.search_pages_with_parent(self.id)

    @property
    def users(self):
        user_ids = [permission["user_id"] for permission in self.get("permissions")]
        self._client.refresh_records(notion_user=user_ids)
        return [self._client.get_user(user_id) for user_id in user_ids]

    def _str_fields(self):
github jamalex / notion-py / notion / user.py View on Github external
from .logger import logger
from .maps import property_map, field_map
from .records import Record


class User(Record):

    _table = "notion_user"

    given_name = field_map("given_name")
    family_name = field_map("family_name")
    email = field_map("email")
    locale = field_map("locale")
    time_zone = field_map("time_zone")

    @property
    def full_name(self):
        return " ".join([self.given_name or "", self.family_name or ""]).strip()

    def _str_fields(self):
        return super()._str_fields() + ["email", "full_name"]
github jamalex / notion-py / notion / block.py View on Github external
"""

    _type = "column_list"

    def evenly_space_columns(self):
        with self._client.as_atomic_transaction():
            for child in self.children:
                child.column_ratio = 1 / len(self.children)


class ColumnBlock(Block):
    """
    Should be added as children of a ColumnListBlock.
    """

    column_ratio = field_map("format.column_ratio")

    _type = "column"


class BasicBlock(Block):

    title = property_map("title")
    color = field_map("format.block_color")

    def convert_to_type(self, new_type):
        """
        Convert this block into another type of BasicBlock. Returns a new instance of the appropriate class.
        """
        assert new_type in BLOCK_TYPES and issubclass(
            BLOCK_TYPES[new_type], BasicBlock
        ), "Target type must correspond to a subclass of BasicBlock"
github jamalex / notion-py / notion / block.py View on Github external
_type = "equation"


class MediaBlock(Block):

    caption = property_map("caption")

    def _str_fields(self):
        return super()._str_fields() + ["caption"]


class EmbedBlock(MediaBlock):

    _type = "embed"

    display_source = field_map(
        "format.display_source",
        api_to_python=add_signed_prefix_as_needed,
        python_to_api=remove_signed_prefix_as_needed,
    )
    source = property_map(
        "source",
        api_to_python=add_signed_prefix_as_needed,
        python_to_api=remove_signed_prefix_as_needed,
    )
    height = field_map("format.block_height")
    full_width = field_map("format.block_full_width")
    page_width = field_map("format.block_page_width")
    width = field_map("format.block_width")

    def set_source_url(self, url):
        self.source = remove_signed_prefix_as_needed(url)
github jamalex / notion-py / notion / user.py View on Github external
from .logger import logger
from .maps import property_map, field_map
from .records import Record


class User(Record):

    _table = "notion_user"

    given_name = field_map("given_name")
    family_name = field_map("family_name")
    email = field_map("email")
    locale = field_map("locale")
    time_zone = field_map("time_zone")

    @property
    def full_name(self):
        return " ".join([self.given_name or "", self.family_name or ""]).strip()

    def _str_fields(self):
        return super()._str_fields() + ["email", "full_name"]
github jamalex / notion-py / notion / block.py View on Github external
Most data in Notion is stored as a "block" (including pages, and all the individual elements within a page).
    These blocks have different types, and in some cases we create subclasses of this class to represent those types.
    Attributes on the Block are mapped to useful attributes of the server-side data structure, as properties, so you can
    get and set values on the API just by reading/writing attributes on these classes. We store a shared local cache on
    the `NotionClient` object of all block data, and reference that as needed from here. Data can be refreshed from the
    server using the `refresh` method.
    """

    _table = "block"

    # we'll mark it as an alias if we load the Block as a child of a page that is not its parent
    _alias_parent = None

    child_list_key = "content"

    type = field_map("type")
    alive = field_map("alive")

    def get_browseable_url(self):
        if "page" in self._type:
            return BASE_URL + self.id.replace("-", "")
        else:
            return self.parent.get_browseable_url() + "#" + self.id.replace("-", "")

    @property
    def children(self):
        if not hasattr(self, "_children"):
            children_ids = self.get("content", [])
            self._client.refresh_records(block=children_ids)
            self._children = Children(parent=self)
        return self._children
github jamalex / notion-py / notion / block.py View on Github external
class PDFBlock(EmbedOrUploadBlock):

    _type = "pdf"


class ImageBlock(EmbedOrUploadBlock):

    _type = "image"


class BookmarkBlock(EmbedBlock):

    _type = "bookmark"

    bookmark_cover = field_map("format.bookmark_cover")
    bookmark_icon = field_map("format.bookmark_icon")
    description = property_map("description")
    link = property_map("link")
    title = property_map("title")

    def set_new_link(self, url):
        self._client.post("setBookmarkMetadata", {"blockId": self.id, "url": url})
        self.refresh()


class LinkToCollectionBlock(MediaBlock):

    _type = "link_to_collection"
    # TODO: add custom fields
github jamalex / notion-py / notion / block.py View on Github external
checked = property_map(
        "checked",
        python_to_api=lambda x: "Yes" if x else "No",
        api_to_python=lambda x: x == "Yes",
    )

    def _str_fields(self):
        return super()._str_fields() + ["checked"]


class CodeBlock(BasicBlock):

    _type = "code"

    language = property_map("language")
    wrap = field_map("format.code_wrap")


class FactoryBlock(BasicBlock):
    """
    Also known as a "Template Button". The title is the button text, and the children are the templates to clone.
    """

    _type = "factory"


class HeaderBlock(BasicBlock):

    _type = "header"


class SubheaderBlock(BasicBlock):