Created
December 11, 2019 11:17
-
-
Save lenolib/20e0ad99bdadc0e291dfb7679346390e to your computer and use it in GitHub Desktop.
Create a unique sequential id for items (tasks) in a notion database / view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from notion.client import NotionClient | |
def enumerate_notion_items( | |
view_url, id_col, created_at_col="created_time", token=None, client=None | |
): | |
""" | |
Given that a property with name e.g. "Item ID" exists for a notion dataset, | |
and that at least one item has a value for that property, this function | |
will then add c2, c3, etc to the rest of the items, as sorted by their | |
creation time. | |
view_url: | |
like 'https://www.notion.so/[account-name]/[uuid]?v=[uuid]' | |
id_col: | |
name of the id column in snake_case | |
created_at_col: | |
name of the id column containing the created at time (snake_case) | |
token: | |
Will be prompted if not given and client is not supplied | |
client: | |
[NotionClient], optional | |
""" | |
if client is None: | |
if token is None: | |
token = input("Please provide token: ") | |
client = NotionClient(token_v2=token) | |
view = client.get_collection_view(view_url) | |
items = view.collection.get_rows() | |
sorted_items = sorted( | |
items, key=lambda i: (getattr(i, id_col), getattr(i, created_at_col)) | |
) | |
with_id = [item for item in sorted_items if getattr(item, id_col)] | |
without_id = [item for item in sorted_items if not getattr(item, id_col, None)] | |
if len(with_id) == 0: | |
raise Exception("At least one item with id is required, for safety") | |
if not getattr(with_id[0], created_at_col): | |
raise Exception("Item does not have '%s' creation time field" % created_at_col) | |
if len(set(getattr(item, id_col) for item in with_id)) != len(with_id): | |
# TODO also log which ids are duplicated | |
raise Exception("Duplicate ids detected, please fix manually") | |
prefix, counter_str = re.match( | |
"([a-zA-Z\s]*)([0-9]*)", getattr(with_id[-1], id_col) | |
).groups() | |
if not counter_str: | |
raise Exception("At least one item id ending with a number is required") | |
next_counter = int(counter_str) + 1 | |
for item in without_id: | |
id_col_value = "%s%s" % (prefix, next_counter) | |
setattr(item, id_col, id_col_value) | |
print('Setting new id value: "%s" for item: %s' % (id_col_value, item.title)) | |
next_counter += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have you an example?