Feat/create starter notebook on get notebooks (#1441)

<!-- .github/pull_request_template.md -->

## Description
<!-- 
Please provide a clear, human-generated description of the changes in
this PR.
DO NOT use AI-generated descriptions. We want to understand your thought
process and reasoning.
-->

## Type of Change
<!-- Please check the relevant option -->
- [ ] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Code refactoring
- [ ] Performance improvement
- [ ] Other (please specify):

## Changes Made
<!-- List the specific changes made in this PR -->
- 
- 
- 

## Testing
<!-- Describe how you tested your changes -->

## Screenshots/Videos (if applicable)
<!-- Add screenshots or videos to help explain your changes -->

## Pre-submission Checklist
<!-- Please check all boxes that apply before submitting your PR -->
- [ ] **I have tested my changes thoroughly before submitting this PR**
- [ ] **This PR contains minimal changes necessary to address the
issue/feature**
- [ ] My code follows the project's coding standards and style
guidelines
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have added necessary documentation (if applicable)
- [ ] All new and existing tests pass
- [ ] I have searched existing PRs to ensure this change hasn't been
submitted already
- [ ] I have linked any relevant issues in the description
- [ ] My commits have clear and descriptive messages

## Related Issues
<!-- Link any related issues using "Fixes #issue_number" or "Relates to
#issue_number" -->

## Additional Notes
<!-- Add any additional notes, concerns, or context for reviewers -->

## DCO Affirmation
I affirm that all code in every commit of this pull request conforms to
the terms of the Topoteretes Developer Certificate of Origin.
This commit is contained in:
Vasilije 2025-09-18 19:54:57 +02:00 committed by GitHub
commit a6d49627fe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 30 additions and 4 deletions

View file

@ -6,6 +6,8 @@ from cognee.infrastructure.databases.relational import with_async_session
from ..models.Notebook import Notebook, NotebookCell
TUTORIAL_NOTEBOOK_NAME = "Python Development with Cognee Tutorial 🧠"
async def _create_tutorial_notebook(
user_id: UUID, session: AsyncSession, force_refresh: bool = False
@ -24,7 +26,7 @@ async def _create_tutorial_notebook(
zip_url=TUTORIAL_ZIP_URL,
owner_id=user_id,
notebook_filename="tutorial.ipynb",
name="Python Development with Cognee Tutorial 🧠",
name=TUTORIAL_NOTEBOOK_NAME,
deletable=False,
force=force_refresh,
)

View file

@ -1,11 +1,16 @@
from uuid import UUID
from typing import List
from sqlalchemy import select
from sqlalchemy import select, and_
from sqlalchemy.ext.asyncio import AsyncSession
from cognee.infrastructure.databases.relational import with_async_session
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
@ -13,6 +18,27 @@ async def get_notebooks(
user_id: UUID,
session: AsyncSession,
) -> 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))
return list(result.scalars().all())

View file

@ -61,8 +61,6 @@ async def create_user(
if auto_login:
await session.refresh(user)
await _create_tutorial_notebook(user.id, session)
return user
except UserAlreadyExists as error:
print(f"User {email} already exists")