How to use the notion.block.CollectionViewBlock 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 Cobertos / md2notion / tests / test_upload.py View on Github external
}
    schema = blockDescriptor['schema']
    rows = blockDescriptor['rows']
    notionBlock = Mock()
    newBlock = Mock(spec=blockDescriptor['type'])
    notionBlock.children.add_new = Mock(return_value=newBlock)

    collection = Mock()
    notionBlock._client.create_record = Mock(return_value=collection)
    notionBlock._client.get_collection = Mock(return_value=collection)

    #act
    uploadBlock(blockDescriptor, notionBlock, '')

    #assert
    notionBlock.children.add_new.assert_called_with(CollectionViewBlock)
    notionBlock._client.create_record.assert_called_with("collection", parent=newBlock, schema=schema)
    notionBlock._client.get_collection.assert_called_with(collection)
    #TODO: This is incomplete...
github Cobertos / md2notion / tests / test_NotionPyRenderer.py View on Github external
def test_table():
    '''it should render a table'''
    #arrange/act
    output = mistletoe.markdown(\
"""
|  Awoo   |  Awooo  |  Awoooo |
|---------|---------|---------|
| Test100 | Test200 | Test300 |
|         | Test400 |         |
""", NotionPyRenderer)

    #assert
    assert len(output) == 1
    output = output[0]
    assert isinstance(output, dict)
    assert output['type'] == notion.block.CollectionViewBlock

    assert isinstance(output['schema'], dict)
    assert len(output['schema']) == 3 #3 properties
    assert list(output['schema'].keys())[2] == 'title' #Last one is 'title'
    assert list(output['schema'].values())[0] == {
            'type': 'text',
            'name': 'Awoo'
        }
    assert list(output['schema'].values())[1] == {
            'type': 'text',
            'name': 'Awooo'
        }
    assert list(output['schema'].values())[2] == {
            'type': 'title',
            'name': 'Awoooo'
        }
github Cobertos / md2notion / tests / test_upload.py View on Github external
def test_uploadBlock_collection():
    #arrange
    blockDescriptor = {
        'type': CollectionViewBlock,
        'schema': {
            'J=}2': {
                'type': 'text',
                'name': 'Awoooo'
            },
            'J=}x': {
                'type': 'text',
                'name': 'Awooo'
            },
            'title': {
                'type': 'text',
                'name': 'Awoo'
            }
        },
        'rows': [
            ['Test100', 'Test200', 'Test300'],
github Cobertos / md2notion / md2notion / upload.py View on Github external
if isinstance(newBlock, ImageBlock):
        imgRelSrc = blockDescriptor["source"]
        if '://' in imgRelSrc:
            return #Don't upload images that are external urls

        if imagePathFunc: #Transform by imagePathFunc
            imgSrc = imagePathFunc(imgRelSrc, mdFilePath)
        else:
            imgSrc = Path(mdFilePath).parent / Path(imgRelSrc)

        if not imgSrc.exists():
            print(f"ERROR: Local image '{imgSrc}' not found to upload. Skipping...")
            return
        print(f"Uploading file '{imgSrc}'")
        newBlock.upload_file(str(imgSrc))
    elif isinstance(newBlock, CollectionViewBlock):
        #We should have generated a schema and rows for this one
        notionClient = blockParent._client #Hacky internals stuff...
        newBlock.collection = notionClient.get_collection(
            #Low-level use of the API
            #TODO: Update when notion-py provides a better interface for this
            notionClient.create_record("collection", parent=newBlock, schema=collectionSchema)
        )
        view = newBlock.views.add_new(view_type="table")
        for row in collectionRows:
            newRow = newBlock.collection.add_row()
            for idx, propName in enumerate(prop["name"] for prop in collectionSchema.values()):
                # TODO: If rows aren't uploading, check to see if there's special
                # characters that don't map to propName in notion-py
                propName = propName.lower() #The actual prop name in notion-py is lowercase
                propVal = row[idx]
                setattr(newRow, propName, propVal)
github Cobertos / md2notion / md2notion / NotionPyRenderer.py View on Github external
#The last one needs to be named 'Title' and is type title
        # 'title': {
        #     'name': 'Name',
        #     'type': 'title'
        # }
        schema.update({
                'title' : {
                    'name': headerRow[-1],
                    'type': 'title'
                }
            })

        #CollectionViewBlock, and it's gonna be a bit hard to do because this
        #isn't fully fleshed out in notion-py yet but we can still use create_record
        return {
            'type': CollectionViewBlock,
            'rows': rows, #everything except the initial row
            'schema': schema
        }
github kevinjalbert / notion-toolbox / shared / notionscripts / notion_api.py View on Github external
def current_week_lights(self):
        found_lights = None

        for block in self.current_week().children:
            if type(block) == CollectionViewBlock and block.title.endswith("Lights"):
                found_lights = block
                break
            else:
                continue

        return found_lights
github jamalex / notion-py / notion / block.py View on Github external
)
        view = self._client.get_collection_view(
            record_id, collection=self._parent._collection
        )
        view.set("collection_id", self._parent._collection.id)
        view_ids = self._parent.get(CollectionViewBlockViews.child_list_key, [])
        view_ids.append(view.id)
        self._parent.set(CollectionViewBlockViews.child_list_key, view_ids)

        # At this point, the view does not see to be completely initialized yet.
        # Hack: wait a bit before e.g. setting a query.
        time.sleep(3)
        return view


class CollectionViewPageBlock(CollectionViewBlock):

    icon = field_map(
        "format.page_icon",
        api_to_python=add_signed_prefix_as_needed,
        python_to_api=remove_signed_prefix_as_needed,
    )

    _type = "collection_view_page"


class FramerBlock(EmbedBlock):

    _type = "framer"


class TweetBlock(EmbedBlock):