Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions notion/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from copy import deepcopy
from datetime import datetime, date
from tzlocal import get_localzone
from uuid import uuid1

from .block import Block, PageBlock, Children, CollectionViewBlock
from .logger import logger
Expand Down Expand Up @@ -89,6 +90,33 @@ def to_notion(self):
return [["‣", [["d", data]]]]


class NotionSelect(object):
valid_colors = ["default", "gray", "brown", "orange", "yellow",
"green", "blue", "purple", "pink", "red"]
id = None
color = "default"
value = None

def __init__(self, value, color="default"):
self.id = str(uuid1())
self.color = self.set_color(color)
self.value = value

def set_color(self, color):
if color not in self.valid_colors:
if self.color:
return self.color
return "default"
return color

def to_dict(self):
return {
"id": self.id,
"value": self.value,
"color": self.color
}


class Collection(Record):
"""
A "collection" corresponds to what's sometimes called a "database" in the Notion UI.
Expand Down Expand Up @@ -126,6 +154,20 @@ def get_schema_properties(self):
properties.append(prop)
return properties

def check_schema_select_options(self, prop, values):
"""
Check and update the prop dict with new values
"""
schema_update = False
current_options = list([p["value"].lower() for p in prop["options"]])
if not isinstance(values, list):
values = [values]
for v in values:
if v.lower() not in current_options:
schema_update = True
prop["options"].append(NotionSelect(v).to_dict())
return schema_update, prop

def get_schema_property(self, identifier):
"""
Look up a property in the collection's schema, by "property id" (generally a 4-char string),
Expand Down Expand Up @@ -491,6 +533,10 @@ def set_property(self, identifier, val):
raise AttributeError(
"Object does not have property '{}'".format(identifier)
)
if prop["type"] in ["select"] or prop["type"] in ["multi_select"]:
schema_update, prop = self.collection.check_schema_select_options(prop, val)
if schema_update:
self.collection.set("schema.{}.options".format(prop["id"]), prop["options"])

path, val = self._convert_python_to_notion(val, prop, identifier=identifier)

Expand Down