Skip to content

Releases: thrau/notion-objects

notion-objects 0.6.0

31 Mar 16:57
Compare
Choose a tag to compare

This release brings additional querying facilities, and support for relation property, rich text, and setting object IDs.

Querying by value

Database now adds two additional querying methods:

  • query_by_value
  • find_unique_by_value

With a database like this:

class TestRecord(Page):
    name = TitleText()
    some_id = Integer()
    some_value = Text()

database: Database[TestRecord] = Database(TestRecord, ...)

You can perform the following search operation to return all records that have foobar set in their text property some_value.

for record in database.query_by_value(TestRecord.some_value, "foobar"):
    print(record.to_dict())

If you expect a unique value (like some ID value), then you can use:

record = database.find_unique_by_value(TestRecord.some_id, 12345)
assert record is not None

Relation properties

A simple implementation of the Relation property is now available. It simply outputs and accepts UUIDs of other pages.

class Task(Page):
    Project = Relation()

task = Task.new()
task.Project = "3682af82-1898-41dd-a0b3-28ba55c8fc13"  # uuid references a different page

Rich Text

You can now use rich text for text fields using the rich_text module:

from notion_objects import rich_text

class Task(Page):
    title = TitleText()
    description = RichTextProperty()
    description_plain = Text("description")  # can be used to access the plain text easily

task = Task.new()
task.description = rich_text.RichText("my description", bold=True, color="red")

# or set a list of you have multiple text items
task.description = [
    rich_text.RichText("Here is a link: "),
    rich_text.RichText("example.com", "http://example.com"),
]

# you can also access the plain text through proxy attributes
assert task.description_plain == "Here is a link: example.com"

Setting object IDs

Some times you may want to create a new record but then use it to update an existing one. To that end you can now set the id field of a on object:

task = Task.new()
task.id = "3056c466-1920-4291-bc20-74c1f53290dc"

database.update(task)

Changelog

Full Changelog: v0.5.0...v0.6.0

notion-objects 0.5.0

14 Oct 14:38
Compare
Choose a tag to compare

This release brings update and create operations for notion database pages. Previously, notion-objects supported only read-only operations.

Updating records

You can update database records by simply calling attributes with normal python assignments.
The data mapper will map the types correctly to Notion's internal format.
You can then call Database.update(...) to run an update API call.
notion-objects keeps track of all the changes that were made to the object, and only sends the changes.

class Task(NotionObject):
    name = TitlePlainText("Name")
    status = Status("Status")
    closed_at = DateTime("Closed at")
    assigned_to = Person("Assigned to")

database: Database[Task] = Database(Task, ...)

task = database.find_by_id("...")
task.status = "Done"
task.closed_at = datetime.utcnow()
database.update(task)

Note not all properties can be set yet.

Creating records

Similarly, you can also create new pages.
You can use NotionObject.new() on any subclass to create new unmanaged instances of that type.
Then, call Database.create(...) to create a new item in the database.

database: Database[Task] = Database(Task, ...)

task = Task.new()
task.task = "My New Task"
task.status = "In progress"
task.assigned_to = "6aa4d3cd-3928-4f61-9072-f74a3ebfc3ca"

task = database.create(task)
print(task.id)  # it now has a database id

v0.4.0

16 Oct 23:22
Compare
Choose a tag to compare
v0.4.0 Pre-release
Pre-release

Summary

  • Database(...).title will now return the name of the database
  • You can now do basic updating of pages. If you use the database without type mapping, you currently use subscripts to set the property values:
      db = Database(database_id, Client(auth=notion_token))
      page = db.find_by_id("b59fec7c9a4b43b1b169bd10aa843053")
      page['MyAttribute'] = "foo"
      db.update(page)
    if you use custom models, you need at least an id field + use attribute setters (page.MyAttribute = "foo")

What's changed

Full Changelog: v0.3.0...v0.4.0