create starter notebook on get all notebooks request - if not created
This commit is contained in:
parent
236a84dd65
commit
7ccd312c2c
2 changed files with 30 additions and 2 deletions
|
|
@ -6,6 +6,8 @@ from cognee.infrastructure.databases.relational import with_async_session
|
||||||
|
|
||||||
from ..models.Notebook import Notebook, NotebookCell
|
from ..models.Notebook import Notebook, NotebookCell
|
||||||
|
|
||||||
|
TUTORIAL_NOTEBOOK_NAME = "Python Development with Cognee Tutorial 🧠"
|
||||||
|
|
||||||
|
|
||||||
async def _create_tutorial_notebook(
|
async def _create_tutorial_notebook(
|
||||||
user_id: UUID, session: AsyncSession, force_refresh: bool = False
|
user_id: UUID, session: AsyncSession, force_refresh: bool = False
|
||||||
|
|
@ -24,7 +26,7 @@ async def _create_tutorial_notebook(
|
||||||
zip_url=TUTORIAL_ZIP_URL,
|
zip_url=TUTORIAL_ZIP_URL,
|
||||||
owner_id=user_id,
|
owner_id=user_id,
|
||||||
notebook_filename="tutorial.ipynb",
|
notebook_filename="tutorial.ipynb",
|
||||||
name="Python Development with Cognee Tutorial 🧠",
|
name=TUTORIAL_NOTEBOOK_NAME,
|
||||||
deletable=False,
|
deletable=False,
|
||||||
force=force_refresh,
|
force=force_refresh,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,16 @@
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
from typing import List
|
from typing import List
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select, and_
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from cognee.infrastructure.databases.relational import with_async_session
|
from cognee.infrastructure.databases.relational import with_async_session
|
||||||
|
|
||||||
from ..models.Notebook import Notebook
|
from ..models.Notebook import Notebook
|
||||||
|
from .create_notebook import _create_tutorial_notebook, TUTORIAL_NOTEBOOK_NAME
|
||||||
|
|
||||||
|
from cognee.shared.logging_utils import get_logger
|
||||||
|
|
||||||
|
logger = get_logger()
|
||||||
|
|
||||||
|
|
||||||
@with_async_session
|
@with_async_session
|
||||||
|
|
@ -13,6 +18,27 @@ async def get_notebooks(
|
||||||
user_id: UUID,
|
user_id: UUID,
|
||||||
session: AsyncSession,
|
session: AsyncSession,
|
||||||
) -> List[Notebook]:
|
) -> List[Notebook]:
|
||||||
|
# Check if tutorial notebook already exists for this user
|
||||||
|
tutorial_query = select(Notebook).where(
|
||||||
|
and_(
|
||||||
|
Notebook.owner_id == user_id,
|
||||||
|
Notebook.name == TUTORIAL_NOTEBOOK_NAME,
|
||||||
|
~Notebook.deletable,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
tutorial_result = await session.execute(tutorial_query)
|
||||||
|
tutorial_notebook = tutorial_result.scalar_one_or_none()
|
||||||
|
|
||||||
|
# If tutorial notebook doesn't exist, create it
|
||||||
|
if tutorial_notebook is None:
|
||||||
|
logger.info(f"Tutorial notebook not found for user {user_id}, creating it")
|
||||||
|
try:
|
||||||
|
await _create_tutorial_notebook(user_id, session, force_refresh=False)
|
||||||
|
except Exception as e:
|
||||||
|
# Log the error but continue to return existing notebooks
|
||||||
|
logger.error(f"Failed to create tutorial notebook for user {user_id}: {e}")
|
||||||
|
|
||||||
|
# Get all notebooks for the user
|
||||||
result = await session.execute(select(Notebook).where(Notebook.owner_id == user_id))
|
result = await session.execute(select(Notebook).where(Notebook.owner_id == user_id))
|
||||||
|
|
||||||
return list(result.scalars().all())
|
return list(result.scalars().all())
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue