diff --git a/notion/collection.py b/notion/collection.py index 748cc06..9d0f757 100644 --- a/notion/collection.py +++ b/notion/collection.py @@ -150,6 +150,27 @@ class Collection(Record): python_to_api=markdown_to_notion, ) cover = field_map("cover") + _schema_types = [ + "text", + "number", + "select", + "multi_select", + "date", + "person", + "file", + "checkbox", + "url", + "email", + "phone_number", + "created_time", + "created_by", + "last_edited_time", + "last_edited_by", + "formula", + "rollup", + # "relation" # another contract for this type, requires another db at creation time + ] + @property def templates(self): @@ -159,6 +180,29 @@ def templates(self): self._templates = Templates(parent=self) return self._templates + def update_schema_properties(self, props): + """ + Update current schema with props. + """ + schema = self.get("schema") + # check that props has a valid type + for _id, prop in props.items(): + if prop["type"] not in self._schema_types: + logger.error("The type {} is unsupported.".format(prop["type"])) + return + + logger.debug("Update current schema: {} with {}".format(schema, props)) + schema.update(props) + self._client.submit_transaction( + build_operation( + id=self.id, + path=[], + args=dict(schema=schema), + command="update", + table="collection", + ) + ) + def get_schema_properties(self): """ Fetch a flattened list of all properties in the collection's schema. diff --git a/notion/smoke_test.py b/notion/smoke_test.py index d9ff048..4e5ef40 100644 --- a/notion/smoke_test.py +++ b/notion/smoke_test.py @@ -200,6 +200,20 @@ def run_live_smoke_test(token_v2, parent_page_url_or_id): assert row1.some_date.timezone == timezone assert row1.some_date.reminder == reminder + # Test update schema with new props + for type_ in ["number", "select", "multi_select", "date", "person", "file", "checkbox", "url", "email", + "phone_number", "created_time", "created_by", "last_edited_time", "last_edited_by", "formula", + "rollup", "text", + # "relation" # another contract for this type, requires another db at creation time + ]: + cvb.collection.update_schema_properties({ + type_: dict( + name=type_, + type=type_ + ) + }) + assert cvb.collection.get_schema_property(type_) + print( "Check it out and make sure it looks good, then press any key here to delete it..." )