Skip to content

Commit

Permalink
Refactor configuration handling to separate schema and tasks for Noti…
Browse files Browse the repository at this point in the history
…on database operations

- Extracted schema from task-specific configurations to improve modularity and reusability.
- Introduced `load_schema` and `load_tasks` functions for better separation of concerns.
- Updated `create_database.py` and `update_database.py` to reflect changes in configuration loading.
- Renamed `fashion_recommender_tasks_schema.json` to `fashion_recommender_tasks.json` to better match its content.
- Improved error handling and code readability across the board.
  • Loading branch information
atxtechbro committed Aug 13, 2024
1 parent 2724410 commit 1642c48
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 145 deletions.
54 changes: 1 addition & 53 deletions config/database_configs/email-capture-validation-task.json
Original file line number Diff line number Diff line change
@@ -1,57 +1,5 @@

{
"schema": {
"title": "Fashion Recommender App Development",
"properties": {
"Name": {
"title": {}
},
"Description": {
"rich_text": {}
},
"Status": {
"select": {
"options": [
{ "name": "To Do", "color": "default" },
{ "name": "In Progress", "color": "blue" },
{ "name": "Done", "color": "green" }
]
}
},
"Due Date": {
"date": {}
},
"Task Type": {
"select": {
"options": [
{ "name": "Cataloging", "color": "red" },
{ "name": "Database Design", "color": "green" },
{ "name": "Backend Development", "color": "yellow" },
{ "name": "Frontend Development", "color": "blue" },
{ "name": "Testing", "color": "purple" }
]
}
},
"Priority": {
"select": {
"options": [
{ "name": "High", "color": "red" },
{ "name": "Medium", "color": "orange" },
{ "name": "Low", "color": "green" }
]
}
},
"Estimated Time": {
"select": {
"options": [
{ "name": "Less than 1 hour", "color": "yellow" },
{ "name": "1-2 hours", "color": "orange" },
{ "name": "2-3 hours", "color": "red" },
{ "name": "More than 3 hours", "color": "purple" }
]
}
}
}
},
"tasks": [
{
"properties": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,4 @@
{
"schema": {
"title": "Fashion Recommender App Development",
"properties": {
"Name": {
"title": {}
},
"Description": {
"rich_text": {}
},
"Status": {
"select": {
"options": [
{ "name": "To Do", "color": "default" },
{ "name": "In Progress", "color": "blue" },
{ "name": "Done", "color": "green" }
]
}
},
"Due Date": {
"date": {}
},
"Task Type": {
"select": {
"options": [
{ "name": "Cataloging", "color": "red" },
{ "name": "Database Design", "color": "green" },
{ "name": "Backend Development", "color": "yellow" },
{ "name": "Frontend Development", "color": "blue" },
{ "name": "Testing", "color": "purple" }
]
}
},
"Priority": {
"select": {
"options": [
{ "name": "High", "color": "red" },
{ "name": "Medium", "color": "orange" },
{ "name": "Low", "color": "green" }
]
}
},
"Estimated Time": {
"select": {
"options": [
{ "name": "Less than 1 hour", "color": "yellow" },
{ "name": "1-2 hours", "color": "orange" },
{ "name": "2-3 hours", "color": "red" },
{ "name": "More than 3 hours", "color": "purple" }
]
}
}
}
},
"tasks": [
{
"properties": {
Expand Down
53 changes: 53 additions & 0 deletions config/database_configs/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"title": "Fashion Recommender App Development",
"properties": {
"Name": {
"title": {}
},
"Description": {
"rich_text": {}
},
"Status": {
"select": {
"options": [
{ "name": "To Do", "color": "default" },
{ "name": "In Progress", "color": "blue" },
{ "name": "Done", "color": "green" }
]
}
},
"Due Date": {
"date": {}
},
"Task Type": {
"select": {
"options": [
{ "name": "Cataloging", "color": "red" },
{ "name": "Database Design", "color": "green" },
{ "name": "Backend Development", "color": "yellow" },
{ "name": "Frontend Development", "color": "blue" },
{ "name": "Testing", "color": "purple" }
]
}
},
"Priority": {
"select": {
"options": [
{ "name": "High", "color": "red" },
{ "name": "Medium", "color": "orange" },
{ "name": "Low", "color": "green" }
]
}
},
"Estimated Time": {
"select": {
"options": [
{ "name": "Less than 1 hour", "color": "yellow" },
{ "name": "1-2 hours", "color": "orange" },
{ "name": "2-3 hours", "color": "red" },
{ "name": "More than 3 hours", "color": "purple" }
]
}
}
}
}
23 changes: 12 additions & 11 deletions scripts/create_database.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
# create_database.py
import argparse
from notion_utils.NotionUtils import load_database_config, create_database, create_task
from notion_utils.NotionUtils import load_schema, load_tasks, create_database, create_task

def main(config_name):
def main(schema_name, tasks_name):
try:
config = load_database_config(config_name)
database_id = create_database(config)
schema = load_schema(schema_name) # Load schema configuration
tasks = load_tasks(tasks_name) # Load tasks configuration

database_id = create_database(schema)
if database_id:
tasks = config["tasks"]
schema_properties = config["schema"]["properties"]
for task in tasks:
schema_properties = schema["properties"]
for task in tasks["tasks"]:
create_task(database_id, task, schema_properties)
except FileNotFoundError as e:
print(e)
except ValueError as e:
print(f"Validation Error: {e}")

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Create a Notion database using a specified template.")
parser.add_argument('template', type=str, help="The name of the template to use (without .json extension).")
parser = argparse.ArgumentParser(description="Create a Notion database using a specified schema and tasks template.")
parser.add_argument('schema', type=str, help="The name of the schema template to use (without .json extension).")
parser.add_argument('tasks', type=str, help="The name of the tasks template to use (without .json extension).")

args = parser.parse_args()
main(args.template)
main(args.schema, args.tasks)
41 changes: 24 additions & 17 deletions scripts/notion_utils/NotionUtils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# notion_utils.py
import os
import json
import requests
Expand All @@ -16,14 +15,22 @@
"Notion-Version": "2021-08-16"
}

def load_database_config(config_name):
"""Load the database configuration from a JSON file."""
config_path = f'config/database_configs/{config_name}.json'
if not os.path.exists(config_path):
raise FileNotFoundError(f"Template file '{config_path}' not found.")
with open(config_path, 'r') as file:
config = json.load(file)
return config
def load_json_file(file_path):
"""Load a JSON file and return its content."""
if not os.path.exists(file_path):
raise FileNotFoundError(f"File '{file_path}' not found.")
with open(file_path, 'r') as file:
return json.load(file)

def load_schema(schema_name="schema"):
"""Load the schema configuration from a JSON file."""
schema_path = f'config/database_configs/{schema_name}.json'
return load_json_file(schema_path)

def load_tasks(tasks_name="tasks"):
"""Load the tasks configuration from a JSON file."""
tasks_path = f'config/database_configs/{tasks_name}.json'
return load_json_file(tasks_path)

def get_property_type(property_config):
"""Determine the type of a property based on its configuration."""
Expand All @@ -50,8 +57,8 @@ def create_database(config):
create_url = 'https://api.notion.com/v1/databases'
data = {
"parent": {"type": "page_id", "page_id": NOTION_PAGE_ID},
"title": [{"type": "text", "text": {"content": config["schema"]["title"]}}],
"properties": config["schema"]["properties"]
"title": [{"type": "text", "text": {"content": config["title"]}}],
"properties": config["properties"]
}
response = requests.post(create_url, headers=HEADERS, json=data)
if response.status_code == 200:
Expand All @@ -69,11 +76,11 @@ def update_database(database_id, config):
data = {}

# Only include title if it's provided in the config
if "schema" in config and "title" in config["schema"]:
data["title"] = [{"type": "text", "text": {"content": config["schema"]["title"]}}]
if "title" in config:
data["title"] = [{"type": "text", "text": {"content": config["title"]}}]

# Check if properties exist in the database schema before updating
if "schema" in config and "properties" in config["schema"]:
if "properties" in config:
# Get the existing properties from the database
existing_schema_response = requests.get(update_url, headers=HEADERS)
if existing_schema_response.status_code != 200:
Expand All @@ -82,14 +89,14 @@ def update_database(database_id, config):
existing_properties = existing_schema_response.json().get("properties", {})

# Filter properties to only include those that exist in the current schema
data["properties"] = {k: v for k, v in config["schema"]["properties"].items() if k in existing_properties}
data["properties"] = {k: v for k, v in config["properties"].items() if k in existing_properties}

# Log missing properties
missing_properties = [k for k in config["schema"]["properties"] if k not in existing_properties]
missing_properties = [k for k in config["properties"] if k not in existing_properties]
if missing_properties:
print(f"Warning: The following properties are missing in the current database schema and won't be updated: {', '.join(missing_properties)}")
else:
raise ValueError("The configuration must include 'properties' under 'schema' to update the database.")
raise ValueError("The configuration must include 'properties' to update the database.")

if not data.get("properties"):
raise ValueError("No valid properties found to update in the database schema.")
Expand Down
28 changes: 17 additions & 11 deletions scripts/update_database.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
# update_database.py
import argparse
from notion_utils.NotionUtils import create_task, load_database_config, update_database
from notion_utils.NotionUtils import load_schema, load_tasks, update_database, create_task

def main(database_id, config_name):
def main(database_id, schema_name, tasks_name):
try:
config = load_database_config(config_name)
update_database(database_id, config)
tasks = config["tasks"]
schema_properties = config["schema"]["properties"]
for task in tasks:
# Load schema and tasks configuration
schema = load_schema(schema_name)
tasks = load_tasks(tasks_name)

# Update the database with the provided schema
update_database(database_id, schema)

# Add tasks to the updated database
schema_properties = schema["properties"]
for task in tasks["tasks"]:
create_task(database_id, task, schema_properties)

except FileNotFoundError as e:
print(e)
except ValueError as e:
print(f"Validation Error: {e}")

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Update an existing Notion database using a specified template.")
parser = argparse.ArgumentParser(description="Update an existing Notion database using specified schema and tasks templates.")
parser.add_argument('database_id', type=str, help="The ID of the database to update.")
parser.add_argument('template', type=str, help="The name of the template to use (without .json extension).")
parser.add_argument('schema', type=str, help="The name of the schema template to use (without .json extension).")
parser.add_argument('tasks', type=str, help="The name of the tasks template to use (without .json extension).")

args = parser.parse_args()
main(args.database_id, args.template)
main(args.database_id, args.schema, args.tasks)

0 comments on commit 1642c48

Please sign in to comment.