From 9f51adca04ce011a976180637652463a84d0f92f Mon Sep 17 00:00:00 2001 From: Denis Novikov <45520816+de-novikov@users.noreply.github.com> Date: Wed, 9 Sep 2020 10:56:00 -0700 Subject: [PATCH] select/multi_select: support for adding new tags if some of the tags don't exist --- notion/collection.py | 48 +++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/notion/collection.py b/notion/collection.py index 9243fd5..04fec55 100644 --- a/notion/collection.py +++ b/notion/collection.py @@ -506,33 +506,27 @@ def _convert_python_to_notion(self, val, prop, identifier=""): ) ) val = [[str(val)]] - if prop["type"] in ["select"]: - if not val: - val = None - else: - valid_options = [p["value"].lower() for p in prop["options"]] - val = val.split(",")[0] - if val.lower() not in valid_options: - raise ValueError( - "Value '{}' not acceptable for property '{}' (valid options: {})".format( - val, identifier, valid_options - ) - ) - val = [[val]] - if prop["type"] in ["multi_select"]: - if not val: - val = [] - valid_options = [p["value"].lower() for p in prop["options"]] - if not isinstance(val, list): - val = [val] - for v in val: - if v.lower() not in valid_options: - raise ValueError( - "Value '{}' not acceptable for property '{}' (valid options: {})".format( - v, identifier, valid_options - ) - ) - val = [[",".join(val)]] + if prop["type"] in ["select", "multi_select"]: + colors = ["default", "gray", "brown", "orange", "yellow", + "green", "blue", "purple", "pink", "red"] + if val: + if prop.get("options", None) == None: + prop["options"] = [] + valid_options = list([p["value"].lower() for p in prop["options"]]) + if not isinstance(val, list): + val = [val] + schema_need_update = False + for vid, v in enumerate(val): + val[vid] = v = v.replace(',', '') + if v.lower() not in valid_options: + schema_need_update = True + prop["options"].append({"id": str(uuid1()), "value": v, "color": choice(colors)}) + valid_options.append(v.lower()) + val = [[",".join(val)]] + if schema_need_update: + schema = self.collection.get("schema") + schema[prop['id']] = prop + self.collection.set("schema", schema) if prop["type"] in ["person"]: userlist = [] if not isinstance(val, list):