Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API to create new select item #51

Closed
aqzas opened this issue Sep 17, 2019 · 7 comments
Closed

API to create new select item #51

aqzas opened this issue Sep 17, 2019 · 7 comments
Labels
enhancement New feature or request

Comments

@aqzas
Copy link

aqzas commented Sep 17, 2019

I want to add some new select item/tag to the select/multi-select property of a page. Is there anyway to do that ?

@bkiac
Copy link

bkiac commented Oct 2, 2019

Maybe it's not the most efficient, but I used the set schema method to implement this for myself with the following code:

from uuid import uuid1
from random import choice

from notion.client import NotionClient

notion_client = NotionClient(token_v2=NOTION_TOKEN)
collection_view = notion_client.get_collection_view(NOTION_PAGE)
collection = collection_view.collection

colors = [
    "default",
    "gray",
    "brown",
    "orange",
    "yellow",
    "green",
    "blue",
    "purple",
    "pink",
    "red",
]

def add_new_multi_select_value(prop, value, color=None):
    """`prop` is the name of the multi select property."""
    if color is None:
        color = choice(colors)

    collection_schema = collection.get("schema")
    prop_schema = next(
        (v for k, v in collection_schema.items() if v["name"] == prop), None
    )
    if not prop_schema:
        raise ValueError(
            f'"{prop}" property does not exist on the collection!'
        )
    if prop_schema["type"] != "multi_select":
        raise ValueError(f'"{prop}" is not a multi select property!')

    dupe = next(
        (o for o in prop_schema["options"] if o["value"] == value), None
    )
    if dupe:
        raise ValueError(f'"{value}" already exists in the schema!')

    prop_schema["options"].append(
        {"id": str(uuid1()), "value": value, "color": color}
    )
    collection.set("schema", collection_schema)

Tagging #28 for duplicate.

@callixte-girard
Copy link

callixte-girard commented Oct 5, 2019

Hi,
I'm encountering the same problem as you. Thanks for your code.
But it's not working for me. It doesn't recognise prop_schema["options"].
When I print the schema here :
prop_schema = next( (v for k, v in collection_schema.items() if v["name"] == prop), None ) print(prop_schema, end=cst.line)
I only get two attributes : name and type.
Do you have an idea why ?

@callixte-girard
Copy link

Okay so I managed to fix it. I think there were two problems :

  1. I needed to add force_refresh=True to the line where I get the collection view, like this :
    .get_collection_view(collection_url, force_refresh=True), because it was not refreshing correctly.
  2. I needed to create the options field if it didn't exist, like this :
    if "options" not in prop_schema: prop_schema["options"] = []
    Hope it helps !

@jamalex jamalex added the enhancement New feature or request label Jan 2, 2020
@nagamotoK
Copy link

Caution: running this crashed my table (beyond repair?). All I get now is the "oops! something went wrong"- page when I try to access it

@bkiac
Copy link

bkiac commented Jul 8, 2020

@nagamotoK That is weird, I still use a slight changed version of this in my bot every day.
https://github.com/bkiac/wickie/blob/173fbfd4660ca5ac9107a67f3a87a588c58de000/wickie/notionutils/add.py#L35-L63

@nagamotoK
Copy link

@bkiac thank you for the link/code. It seems, that the error was on my side. I used your implementation and now it works --

@jamalex
Copy link
Owner

jamalex commented Jan 25, 2021

Automatic adding of missing options is now merged via #252

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants