File size: 1,147 Bytes
c60446c
 
b4c7867
 
 
c60446c
 
 
 
 
b4c7867
 
c60446c
b4c7867
 
c60446c
 
 
b4c7867
c60446c
b4c7867
 
 
c60446c
b4c7867
 
c60446c
b4c7867
 
c60446c
b4c7867
 
 
c60446c
b4c7867
c60446c
 
 
 
 
 
b4c7867
c60446c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
import shutil
import uuid
from datetime import datetime

from src.storage.index_store import load_index, save_index
from src.storage.paths import nb_root, ensure_tree


def _now():
    return datetime.utcnow().isoformat() + "Z"


def create_notebook(username: str, name: str) -> str:
    nb_id = str(uuid.uuid4())

    idx = load_index(username)
    idx.append({"id": nb_id, "name": name or "Untitled", "created_at": _now(), "updated_at": _now()})
    save_index(username, idx)

    ensure_tree(username, nb_id)
    return nb_id


def rename_notebook(username: str, notebook_id: str, new_name: str):
    idx = load_index(username)
    for nb in idx:
        if nb["id"] == notebook_id:
            nb["name"] = new_name
            nb["updated_at"] = _now()
            break
    save_index(username, idx)


def delete_notebook(username: str, notebook_id: str):
    # remove folder
    p = nb_root(username, notebook_id)
    if os.path.exists(p):
        shutil.rmtree(p, ignore_errors=True)

    # remove from index
    idx = load_index(username)
    idx = [nb for nb in idx if nb.get("id") != notebook_id]
    save_index(username, idx)