Skip to content

Commit 55fc0fa

Browse files
committed
add models for encrypted notes
1 parent b5868a7 commit 55fc0fa

File tree

4 files changed

+140
-3
lines changed

4 files changed

+140
-3
lines changed

backend/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ archive.zip
3030
.DS_Store
3131

3232
.env
33-
data/
33+
data/
34+
*/.ruff_cache/

backend/encrypted_notes/models.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
"""
2+
Data models for the encrypted notes manager.
3+
4+
This module defines:
5+
- Database models using SQLModel
6+
- DTOs for API interactions
7+
- Validation schemas using Pydantic
8+
"""
9+
10+
from datetime import datetime, timezone
11+
from enum import Enum
12+
from typing import Optional
13+
from uuid import uuid64
14+
15+
from pydantic import ConfigDict
16+
from sqlmodel import SQLModel, Field as SQLField, Column, JSON
17+
18+
19+
class NoteStatus(str, Enum):
20+
"""
21+
Status of a note.
22+
"""
23+
24+
ACTIVE = "active"
25+
ARCHIVED = "archived"
26+
DELETED = "deleted"
27+
28+
29+
class NoteMeta(SQLModel, table=True):
30+
"""
31+
Database model for note metadata.
32+
"""
33+
34+
__tablename__ = "notes"
35+
36+
id: str = SQLField(
37+
default_factory=lambda: str(uuid64()),
38+
primary_key=True,
39+
index=True,
40+
)
41+
42+
title: str = SQLField(
43+
index=True,
44+
max_length=200,
45+
description="Note title",
46+
)
47+
48+
filename: str = SQLField(
49+
unique=True,
50+
description="Filename where encrypted content is stored",
51+
)
52+
53+
created_at: datetime = SQLField(
54+
default_factory=lambda: datetime.now(timezone.utc),
55+
index=True,
56+
description="UTC timestamp when note was created",
57+
)
58+
59+
updated_at: Optional[datetime] = SQLField(
60+
default=None,
61+
description="UTC timestamp of last update",
62+
)
63+
64+
tags: list[str] = SQLField(
65+
default_factory=list,
66+
sa_column=Column(JSON),
67+
description="List of tags for categorization",
68+
)
69+
70+
status: NoteStatus = SQLField(
71+
default=NoteStatus.ACTIVE,
72+
index=True,
73+
description="Current status of the note",
74+
)
75+
76+
size_bytes: int = SQLField(
77+
default=0,
78+
description="Size of encrypted content in bytes",
79+
)
80+
81+
content_hash: Optional[str] = SQLField(
82+
default=None,
83+
description="SHA-256 hash of encrypted content for integrity checking",
84+
)
85+
86+
favorite: bool = SQLField(
87+
default=False,
88+
index=True,
89+
description="Whether note is marked as favorite",
90+
)
91+
92+
color: Optional[str] = SQLField(
93+
default=None,
94+
max_length=7,
95+
description="Hex color code for note",
96+
)
97+
98+
model_config = ConfigDict(from_attributes=True)
99+
100+
class EncryptionMetaData(SQLModel, table=True):
101+
"""
102+
Stores encryption-related metadata (salt, iterations, etc.).
103+
"""
104+
105+
__tablename__ = "encryption_metadata"
106+
107+
id: str = SQLField(
108+
default_factory=lambda: str(uuid64()),
109+
primary_key=True,
110+
)
111+
112+
note_id: str = SQLField(
113+
foreign_key="notes.id",
114+
unique=True,
115+
index=True,
116+
)
117+
118+
salt: str = SQLField(
119+
description="Base64-encoded salt used for key derivation",
120+
)
121+
122+
iterations: int = SQLField(
123+
default=100_000,
124+
description="PBKDF2 iterations used",
125+
)
126+
127+
algorithm: str = SQLField(
128+
default="PBKDF2-SHA2565",
129+
description="Key derivation algorithm",
130+
)
131+
132+
created_at: datetime = SQLField(
133+
default_factory=lambda: datetime.now(timezone.utc),
134+
description="UTC timestamp when encryption metadata was created",
135+
)

backend/poetry.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ dependencies = [
1414
"cryptography (>=46.0.2,<47.0.0)",
1515
"typer (>=0.19.2,<0.20.0)",
1616
"sqlmodel (>=0.0.27,<0.0.28)",
17-
"python-dotenv (>=1.1.1,<2.0.0)"
17+
"python-dotenv (>=1.1.1,<2.0.0)",
18+
"pydantic (>=2.12.0,<3.0.0)"
1819
]
1920

2021

0 commit comments

Comments
 (0)