Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
class ColumnListBlock(Block):
"""
Must contain only ColumnBlocks as children.
"""
_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):
"""
command=list_command,
)
)
# update the local block cache to reflect the updates
self._client.refresh_records(
block=[
self.id,
self.get("parent_id"),
target_block.id,
target_block.get("parent_id"),
]
)
class DividerBlock(Block):
_type = "divider"
class ColumnListBlock(Block):
"""
Must contain only ColumnBlocks as children.
"""
_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)
def get_block(self, url_or_id, force_refresh=False):
"""
Retrieve an instance of a subclass of Block that maps to the block/page identified by the URL or ID passed in.
"""
block_id = extract_id(url_or_id)
block = self.get_record_data("block", block_id, force_refresh=force_refresh)
if not block:
return None
if block.get("parent_table") == "collection":
if block.get("is_template"):
block_class = TemplateBlock
else:
block_class = CollectionRowBlock
else:
block_class = BLOCK_TYPES.get(block.get("type", ""), Block)
return block_class(self, block_id)
_type = "text"
class EquationBlock(BasicBlock):
latex = field_map(
["properties", "title"],
python_to_api=lambda x: [[x]],
api_to_python=lambda x: x[0][0],
)
_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,
)
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"
self.type = new_type
return self._client.get_block(self.id)
def _str_fields(self):
return super()._str_fields() + ["title"]
def __contains__(self, item):
if isinstance(item, str):
item_id = extract_id(item)
elif isinstance(item, Block):
item_id = item.id
else:
return False
return item_id in self._block_ids
def filter(self, type=None):
kids = list(self)
if type:
if isinstance(type, str):
type = BLOCK_TYPES.get(type, Block)
kids = [kid for kid in kids if isinstance(kid, type)]
return kids
class InvisionBlock(EmbedBlock):
_type = "invision"
class CalloutBlock(BasicBlock):
icon = field_map("format.page_icon")
_type = "callout"
BLOCK_TYPES = {
cls._type: cls
for cls in locals().values()
if type(cls) == type and issubclass(cls, Block) and hasattr(cls, "_type")
}
self._client.refresh_records(
block=[
self.id,
self.get("parent_id"),
target_block.id,
target_block.get("parent_id"),
]
)
class DividerBlock(Block):
_type = "divider"
class ColumnListBlock(Block):
"""
Must contain only ColumnBlocks as children.
"""
_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.
"""
def _str_fields(self):
"""
Determines the list of fields to include in the __str__ representation. Override and extend this in subclasses.
"""
fields = super()._str_fields()
# if this is a generic Block instance, include what type of block it is
if type(self) is Block:
fields.append("type")
return fields