-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes_service.py
More file actions
1373 lines (1268 loc) · 51.5 KB
/
Copy pathnotes_service.py
File metadata and controls
1373 lines (1268 loc) · 51.5 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import base64
import hashlib
import json
import math
import mimetypes
import re
import sqlite3
from contextlib import contextmanager
from dataclasses import dataclass
from datetime import datetime
from decimal import Decimal, InvalidOperation, ROUND_HALF_UP
from pathlib import Path
from typing import Callable, Iterator, Literal
RecordKind = Literal["note", "expense"]
RecordScope = Literal["active", "deleted"]
class ServiceError(Exception):
"""Base class for privacy-safe service errors."""
class ValidationError(ServiceError):
pass
class NotFoundError(ServiceError):
pass
class InvalidCursorError(ValidationError):
pass
class DatabaseBusyError(ServiceError):
pass
class AttachmentAccessError(ServiceError):
pass
@dataclass(frozen=True, slots=True)
class NoteCreate:
content: str
raw_text: str = ""
category: str = "其他"
tags: str = ""
source: str = "admin"
priority: int = 0
pinned: bool = False
note_date: str | None = None
note_time: str | None = None
attachment_summary: str = ""
@dataclass(frozen=True, slots=True)
class ExpenseCreate:
amount: Decimal | str | float | int
expense_type: str = "expense"
category: str = "其他"
content: str = ""
raw_text: str = ""
payer: str = ""
people: str = ""
split_with: int = 0
source: str = "admin"
exp_date: str | None = None
exp_time: str | None = None
@dataclass(frozen=True, slots=True)
class RecordQuery:
kind: RecordKind | None = None
scope: RecordScope = "active"
search: str = ""
category: str = ""
direction: str = ""
start_date: str | None = None
end_date: str | None = None
has_attachment: bool | None = None
limit: int = 20
cursor: str | None = None
@dataclass(frozen=True, slots=True)
class RecordView:
kind: RecordKind
id: int
display_at: str
title: str
summary: str
category: str
tags: tuple[str, ...]
amount: Decimal | None
direction: str
split_with: int
split_amount: Decimal
payer: str
people: tuple[str, ...]
has_attachment: bool
attachment_count: int
deleted: bool
pinned: bool
source: str
raw_text: str
record_date: str
record_time: str
priority: int
attachment_summary: str
@dataclass(frozen=True, slots=True)
class RecordPage:
items: tuple[RecordView, ...]
next_cursor: str | None
@dataclass(frozen=True, slots=True)
class DashboardSummary:
note_count: int
expense_total: Decimal
income_total: Decimal
attachment_count: int
@dataclass(frozen=True, slots=True)
class CalendarDaySummary:
date: str
note_count: int
expense_count: int
@dataclass(frozen=True, slots=True)
class MutationResult:
kind: RecordKind
id: int
changed: bool
@dataclass(frozen=True, slots=True)
class AuditEvent:
id: int
action: str
record_type: RecordKind
record_id: int
result: str
created_at: str
@dataclass(frozen=True, slots=True)
class AttachmentRef:
path: Path
download_name: str
mime_type: str
inline: bool
NOTE_COLUMN_MIGRATIONS: dict[str, str] = {
"raw_text": "TEXT DEFAULT ''",
"category": "TEXT DEFAULT ''",
"tags": "TEXT DEFAULT ''",
"source": "TEXT DEFAULT 'telegram'",
"priority": "INTEGER DEFAULT 0",
"pinned": "INTEGER DEFAULT 0",
"note_date": "TEXT DEFAULT ''",
"note_time": "TEXT DEFAULT ''",
"has_attachment": "INTEGER DEFAULT 0",
"attachments": "TEXT DEFAULT ''",
"attachment_summary": "TEXT DEFAULT ''",
"ocr_text": "TEXT DEFAULT ''",
"search_text": "TEXT DEFAULT ''",
"ocr_status": "TEXT DEFAULT 'none'",
"ocr_engine": "TEXT DEFAULT ''",
"ocr_updated_at": "TIMESTAMP",
"receipt_text": "TEXT DEFAULT ''",
"receipt_prices": "TEXT DEFAULT ''",
"receipt_total": "REAL",
"receipt_ocr_status": "TEXT DEFAULT 'none'",
"receipt_ocr_engine": "TEXT DEFAULT ''",
"gps_lat": "REAL",
"gps_lon": "REAL",
"gps_alt": "REAL",
"location_name": "TEXT DEFAULT ''",
"location_address": "TEXT DEFAULT ''",
"location_source": "TEXT DEFAULT ''",
"location_maps_url": "TEXT DEFAULT ''",
"location_updated_at": "TIMESTAMP",
"deleted": "INTEGER DEFAULT 0",
"deleted_at": "TIMESTAMP",
"created_at": "TIMESTAMP",
"updated_at": "TIMESTAMP",
}
EXPENSE_COLUMN_MIGRATIONS: dict[str, str] = {
"expense_type": "TEXT DEFAULT 'expense'",
"category": "TEXT DEFAULT '其他'",
"content": "TEXT DEFAULT ''",
"raw_text": "TEXT DEFAULT ''",
"payer": "TEXT DEFAULT ''",
"people": "TEXT DEFAULT ''",
"split_with": "INTEGER DEFAULT 0",
"split_amount": "REAL DEFAULT 0",
"source": "TEXT DEFAULT 'telegram'",
"exp_date": "TEXT DEFAULT ''",
"exp_time": "TEXT DEFAULT ''",
"deleted": "INTEGER DEFAULT 0",
"deleted_at": "TIMESTAMP",
"created_at": "TIMESTAMP",
"updated_at": "TIMESTAMP",
}
class QuickNotesService:
SCHEMA_VERSION = 1
MAX_NOTE_LENGTH = 5000
MAX_TEXT_LENGTH = 1000
MAX_AMOUNT = Decimal("999999999999.99")
SAFE_INLINE_MIME_TYPES = {
"image/png",
"image/jpeg",
"image/webp",
"image/gif",
"application/pdf",
}
def __init__(
self,
root: str | Path,
*,
busy_timeout_ms: int = 5000,
now_fn: Callable[[], datetime] | None = None,
) -> None:
self.root = Path(root).resolve()
self.db_path = self.root / "notes.db"
self.attachments_dir = self.root / "attachments"
self.exports_dir = self.root / "exports"
self.busy_timeout_ms = max(1, int(busy_timeout_ms))
self._now_fn = now_fn or datetime.now
self._ready = False
def _ensure_dirs(self) -> None:
self.root.mkdir(parents=True, exist_ok=True)
self.attachments_dir.mkdir(parents=True, exist_ok=True)
self.exports_dir.mkdir(parents=True, exist_ok=True)
@contextmanager
def _connect(self) -> Iterator[sqlite3.Connection]:
self._ensure_dirs()
con = sqlite3.connect(
self.db_path,
timeout=self.busy_timeout_ms / 1000,
isolation_level=None,
)
con.row_factory = sqlite3.Row
try:
con.execute("PRAGMA foreign_keys=ON")
con.execute(f"PRAGMA busy_timeout={self.busy_timeout_ms}")
yield con
finally:
con.close()
@contextmanager
def _transaction(self) -> Iterator[sqlite3.Connection]:
with self._connect() as con:
try:
con.execute("BEGIN IMMEDIATE")
yield con
con.commit()
except sqlite3.OperationalError as exc:
con.rollback()
if self._is_busy_error(exc):
raise DatabaseBusyError("資料庫忙碌,請稍後重試") from exc
raise
except Exception:
con.rollback()
raise
@staticmethod
def _is_busy_error(exc: sqlite3.OperationalError) -> bool:
text = str(exc).lower()
return "locked" in text or "busy" in text
@contextmanager
def _read_connection(self) -> Iterator[sqlite3.Connection]:
try:
with self._connect() as con:
yield con
except sqlite3.OperationalError as exc:
if self._is_busy_error(exc):
raise DatabaseBusyError("資料庫忙碌,請稍後重試") from exc
raise
@staticmethod
def _execute_statements(con: sqlite3.Connection, script: str) -> None:
"""Execute a semicolon-terminated SQL batch without implicit commits."""
pending: list[str] = []
for line in script.splitlines(keepends=True):
pending.append(line)
statement = "".join(pending).strip()
if statement and sqlite3.complete_statement(statement):
con.execute(statement)
pending.clear()
if "".join(pending).strip():
raise ValueError("migration SQL contains an incomplete statement")
@staticmethod
def _ensure_column(con: sqlite3.Connection, table: str, column: str, definition: str) -> None:
existing = {row["name"] for row in con.execute(f"PRAGMA table_info({table})")}
if column not in existing:
con.execute(f"ALTER TABLE {table} ADD COLUMN {column} {definition}")
def migrate(
self,
*,
before_migrate: Callable[[], object] | None = None,
) -> object | None:
prepared: object | None = None
with self._transaction() as con:
if before_migrate is not None:
prepared = before_migrate()
self._execute_statements(con,
"""
CREATE TABLE IF NOT EXISTS notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
content TEXT NOT NULL,
raw_text TEXT DEFAULT '',
category TEXT DEFAULT '',
tags TEXT DEFAULT '',
source TEXT DEFAULT 'telegram',
priority INTEGER DEFAULT 0,
pinned INTEGER DEFAULT 0,
note_date TEXT NOT NULL,
note_time TEXT NOT NULL,
has_attachment INTEGER DEFAULT 0,
attachments TEXT DEFAULT '',
attachment_summary TEXT DEFAULT '',
ocr_text TEXT DEFAULT '',
search_text TEXT DEFAULT '',
ocr_status TEXT DEFAULT 'none',
ocr_engine TEXT DEFAULT '',
ocr_updated_at TIMESTAMP,
receipt_text TEXT DEFAULT '',
receipt_prices TEXT DEFAULT '',
receipt_total REAL,
receipt_ocr_status TEXT DEFAULT 'none',
receipt_ocr_engine TEXT DEFAULT '',
gps_lat REAL,
gps_lon REAL,
gps_alt REAL,
location_name TEXT DEFAULT '',
location_address TEXT DEFAULT '',
location_source TEXT DEFAULT '',
location_maps_url TEXT DEFAULT '',
location_updated_at TIMESTAMP,
deleted INTEGER DEFAULT 0,
deleted_at TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS expenses (
id INTEGER PRIMARY KEY AUTOINCREMENT,
amount REAL NOT NULL,
expense_type TEXT DEFAULT 'expense',
category TEXT DEFAULT '其他',
content TEXT DEFAULT '',
raw_text TEXT DEFAULT '',
payer TEXT DEFAULT '',
people TEXT DEFAULT '',
split_with INTEGER DEFAULT 0,
split_amount REAL DEFAULT 0,
source TEXT DEFAULT 'telegram',
exp_date TEXT NOT NULL,
exp_time TEXT NOT NULL,
deleted INTEGER DEFAULT 0,
deleted_at TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS admin_audit (
id INTEGER PRIMARY KEY AUTOINCREMENT,
action TEXT NOT NULL,
record_type TEXT NOT NULL,
record_id INTEGER NOT NULL,
result TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS schema_migrations (
version INTEGER PRIMARY KEY,
name TEXT NOT NULL,
applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
"""
)
for column, definition in NOTE_COLUMN_MIGRATIONS.items():
self._ensure_column(con, "notes", column, definition)
for column, definition in EXPENSE_COLUMN_MIGRATIONS.items():
self._ensure_column(con, "expenses", column, definition)
self._execute_statements(con,
"""
CREATE INDEX IF NOT EXISTS idx_date ON notes(note_date);
CREATE INDEX IF NOT EXISTS idx_cat ON notes(category);
CREATE INDEX IF NOT EXISTS idx_pinned ON notes(pinned);
CREATE INDEX IF NOT EXISTS idx_tags ON notes(tags);
CREATE INDEX IF NOT EXISTS idx_deleted ON notes(deleted);
CREATE INDEX IF NOT EXISTS idx_search_text ON notes(search_text);
CREATE INDEX IF NOT EXISTS idx_ocr_status ON notes(ocr_status);
CREATE INDEX IF NOT EXISTS idx_exp_date ON expenses(exp_date);
CREATE INDEX IF NOT EXISTS idx_exp_cat ON expenses(category);
CREATE INDEX IF NOT EXISTS idx_exp_deleted ON expenses(deleted);
CREATE INDEX IF NOT EXISTS idx_exp_payer ON expenses(payer);
CREATE INDEX IF NOT EXISTS idx_admin_audit_created ON admin_audit(created_at);
"""
)
con.execute(
"INSERT OR IGNORE INTO schema_migrations(version, name) VALUES (?, ?)",
(self.SCHEMA_VERSION, "admin_service_baseline"),
)
self._ready = True
return prepared
def migrate_with_preflight(self, preflight: Callable[[], object]) -> object | None:
"""Run a preflight after acquiring the same writer lock used by migration."""
return self.migrate(before_migrate=preflight)
@classmethod
def _schema_ready_on_connection(cls, con: sqlite3.Connection) -> bool:
required = {"notes", "expenses", "admin_audit", "schema_migrations"}
names = {
row["name"]
for row in con.execute(
"SELECT name FROM sqlite_master WHERE type='table'"
)
if row["name"] in required
}
if names != required:
return False
version = con.execute(
"SELECT 1 FROM schema_migrations WHERE version=?",
(cls.SCHEMA_VERSION,),
).fetchone()
note_columns = {row["name"] for row in con.execute("PRAGMA table_info(notes)")}
expense_columns = {row["name"] for row in con.execute("PRAGMA table_info(expenses)")}
return bool(version) and set(NOTE_COLUMN_MIGRATIONS) <= note_columns and set(
EXPENSE_COLUMN_MIGRATIONS
) <= expense_columns
def schema_ready(self) -> bool:
"""Return schema readiness without creating or migrating the database."""
if not self.db_path.is_file():
return False
uri = self.db_path.resolve().as_uri() + "?mode=ro"
con: sqlite3.Connection | None = None
try:
con = sqlite3.connect(
uri,
uri=True,
timeout=self.busy_timeout_ms / 1000,
isolation_level=None,
)
con.row_factory = sqlite3.Row
con.execute(f"PRAGMA busy_timeout={self.busy_timeout_ms}")
return self._schema_ready_on_connection(con)
except sqlite3.OperationalError as exc:
if self._is_busy_error(exc):
raise DatabaseBusyError("資料庫忙碌,請稍後重試") from exc
if not self.db_path.exists():
return False
raise
finally:
if con is not None:
con.close()
def _ensure_ready(self) -> None:
if self._ready:
return
if not self.db_path.exists():
self.migrate()
return
schema_ready = False
try:
with self._connect() as con:
names = {
row["name"]
for row in con.execute(
"""
SELECT name FROM sqlite_master
WHERE type='table'
AND name IN ('notes','expenses','admin_audit','schema_migrations')
"""
)
}
if names == {"notes", "expenses", "admin_audit", "schema_migrations"}:
version = con.execute(
"SELECT 1 FROM schema_migrations WHERE version=?",
(self.SCHEMA_VERSION,),
).fetchone()
note_columns = {
row["name"] for row in con.execute("PRAGMA table_info(notes)")
}
expense_columns = {
row["name"] for row in con.execute("PRAGMA table_info(expenses)")
}
schema_ready = bool(version) and set(NOTE_COLUMN_MIGRATIONS) <= note_columns and set(
EXPENSE_COLUMN_MIGRATIONS
) <= expense_columns
except sqlite3.OperationalError as exc:
if self._is_busy_error(exc):
raise DatabaseBusyError("資料庫忙碌,請稍後重試") from exc
raise
if schema_ready:
self._ready = True
else:
self.migrate()
@staticmethod
def _clean_text(value: object, field: str, max_length: int, *, required: bool = False) -> str:
text = str(value or "").strip()
if required and not text:
raise ValidationError(f"{field} 不可空白")
if len(text) > max_length:
raise ValidationError(f"{field} 超過長度限制")
return text
@staticmethod
def _normalize_date(value: str | None, fallback: str) -> str:
text = value or fallback
try:
return datetime.strptime(text, "%Y-%m-%d").strftime("%Y-%m-%d")
except (TypeError, ValueError) as exc:
raise ValidationError("日期格式必須是 YYYY-MM-DD") from exc
@staticmethod
def _normalize_time(value: str | None, fallback: str) -> str:
text = value or fallback
for fmt in ("%H:%M:%S", "%H:%M"):
try:
return datetime.strptime(text, fmt).strftime("%H:%M:%S")
except (TypeError, ValueError):
continue
raise ValidationError("時間格式必須是 HH:MM 或 HH:MM:SS")
@classmethod
def _normalize_amount(cls, value: Decimal | str | float | int) -> Decimal:
try:
amount = Decimal(str(value))
except (InvalidOperation, ValueError) as exc:
raise ValidationError("金額格式不正確") from exc
if not amount.is_finite() or amount <= 0 or amount > cls.MAX_AMOUNT:
raise ValidationError("金額必須大於 0 且在允許範圍內")
return amount.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
@staticmethod
def _split_values(value: str) -> tuple[str, ...]:
return tuple(part.strip() for part in (value or "").split(",") if part.strip())
@staticmethod
def _parse_attachments(value: str) -> list[dict[str, object]]:
if not value:
return []
try:
parsed = json.loads(value)
except (TypeError, json.JSONDecodeError):
return []
return [item for item in parsed if isinstance(item, dict)] if isinstance(parsed, list) else []
@staticmethod
def _compose_search_text(*values: object) -> str:
return " ".join(str(value).strip() for value in values if value and str(value).strip())
def _insert_audit(
self,
con: sqlite3.Connection,
action: str,
kind: RecordKind,
record_id: int,
result: str,
) -> None:
con.execute(
"INSERT INTO admin_audit(action, record_type, record_id, result) VALUES (?, ?, ?, ?)",
(action, kind, record_id, result),
)
@staticmethod
def _validate_kind(kind: str) -> RecordKind:
if kind not in {"note", "expense"}:
raise ValidationError("record kind 不正確")
return kind # type: ignore[return-value]
@staticmethod
def _validate_id(record_id: int) -> int:
try:
value = int(record_id)
except (TypeError, ValueError) as exc:
raise ValidationError("record ID 必須是正整數") from exc
if value <= 0:
raise ValidationError("record ID 必須是正整數")
return value
def create_note(self, request: NoteCreate) -> RecordView:
self._ensure_ready()
now = self._now_fn()
content = self._clean_text(request.content, "內容", self.MAX_NOTE_LENGTH, required=True)
raw_text = self._clean_text(request.raw_text or content, "原始文字", self.MAX_NOTE_LENGTH)
category = self._clean_text(request.category or "其他", "類別", 100)
tags = self._clean_text(request.tags, "標籤", self.MAX_TEXT_LENGTH)
source = self._clean_text(request.source or "admin", "來源", 32, required=True)
attachment_summary = self._clean_text(
request.attachment_summary,
"附件摘要",
self.MAX_TEXT_LENGTH,
)
try:
priority = int(request.priority)
except (TypeError, ValueError) as exc:
raise ValidationError("優先度必須是 0、1 或 2") from exc
if priority not in {0, 1, 2}:
raise ValidationError("優先度必須是 0、1 或 2")
note_date = self._normalize_date(request.note_date, now.strftime("%Y-%m-%d"))
note_time = self._normalize_time(request.note_time, now.strftime("%H:%M:%S"))
search_text = self._compose_search_text(
content,
raw_text,
category,
tags,
attachment_summary,
)
with self._transaction() as con:
cursor = con.execute(
"""
INSERT INTO notes (
content, raw_text, category, tags, source, priority, pinned,
note_date, note_time, has_attachment, attachments,
attachment_summary, search_text, created_at, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 0, '', ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
""",
(
content,
raw_text,
category,
tags,
source,
priority,
1 if request.pinned else 0,
note_date,
note_time,
attachment_summary,
search_text,
),
)
note_id = int(cursor.lastrowid)
self._insert_audit(con, "create", "note", note_id, "ok")
return self.get_record("note", note_id)
def create_expense(self, request: ExpenseCreate) -> RecordView:
self._ensure_ready()
now = self._now_fn()
amount = self._normalize_amount(request.amount)
if request.expense_type not in {"expense", "income"}:
raise ValidationError("記帳類型必須是 expense 或 income")
category = self._clean_text(request.category or "其他", "類別", 100)
content = self._clean_text(request.content, "說明", self.MAX_NOTE_LENGTH)
raw_text = self._clean_text(request.raw_text or content, "原始文字", self.MAX_NOTE_LENGTH)
payer = self._clean_text(request.payer, "先付者", 200)
people = self._clean_text(request.people, "參與者", self.MAX_TEXT_LENGTH)
source = self._clean_text(request.source or "admin", "來源", 32, required=True)
try:
split_with = int(request.split_with or 0)
except (TypeError, ValueError) as exc:
raise ValidationError("分攤人數必須是整數") from exc
if split_with < 0 or split_with > 100:
raise ValidationError("分攤人數必須介於 0 到 100")
split_amount = (
(amount / split_with).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
if split_with > 0
else Decimal("0.00")
)
exp_date = self._normalize_date(request.exp_date, now.strftime("%Y-%m-%d"))
exp_time = self._normalize_time(request.exp_time, now.strftime("%H:%M:%S"))
with self._transaction() as con:
cursor = con.execute(
"""
INSERT INTO expenses (
amount, expense_type, category, content, raw_text, payer,
people, split_with, split_amount, source, exp_date, exp_time,
created_at, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
""",
(
float(amount),
request.expense_type,
category,
content,
raw_text,
payer,
people,
split_with,
float(split_amount),
source,
exp_date,
exp_time,
),
)
expense_id = int(cursor.lastrowid)
self._insert_audit(con, "create", "expense", expense_id, "ok")
return self.get_record("expense", expense_id)
@staticmethod
def _note_select(pin_sort: bool = False) -> str:
pin_expression = "pinned" if pin_sort else "0"
return f"""
SELECT
'note' AS kind,
id,
{pin_expression} AS pin_sort,
note_date || ' ' || note_time AS display_at,
content AS title,
CASE
WHEN attachment_summary <> '' THEN attachment_summary
WHEN raw_text <> content THEN raw_text
ELSE ''
END AS summary,
category,
tags,
NULL AS amount,
'' AS direction,
0 AS split_with,
0 AS split_amount,
'' AS payer,
'' AS people,
has_attachment,
attachments,
deleted,
pinned,
source,
raw_text,
note_date AS record_date,
note_time AS record_time,
priority,
attachment_summary
FROM notes
"""
@staticmethod
def _expense_select() -> str:
return """
SELECT
'expense' AS kind,
id,
0 AS pin_sort,
exp_date || ' ' || exp_time AS display_at,
CASE
WHEN content <> '' THEN content
WHEN raw_text <> '' THEN raw_text
ELSE category
END AS title,
CASE WHEN raw_text <> content THEN raw_text ELSE '' END AS summary,
category,
'' AS tags,
amount,
expense_type AS direction,
split_with,
split_amount,
payer,
people,
0 AS has_attachment,
'' AS attachments,
deleted,
0 AS pinned,
source,
raw_text,
exp_date AS record_date,
exp_time AS record_time,
0 AS priority,
'' AS attachment_summary
FROM expenses
"""
def _row_to_record(self, row: sqlite3.Row) -> RecordView:
kind = self._validate_kind(str(row["kind"]))
attachment_items = self._parse_attachments(str(row["attachments"] or ""))
amount = None
if row["amount"] is not None:
amount = Decimal(str(row["amount"])).quantize(Decimal("0.01"))
return RecordView(
kind=kind,
id=int(row["id"]),
display_at=str(row["display_at"]),
title=str(row["title"] or ""),
summary=str(row["summary"] or ""),
category=str(row["category"] or ""),
tags=self._split_values(str(row["tags"] or "")),
amount=amount,
direction=str(row["direction"] or ""),
split_with=int(row["split_with"] or 0),
split_amount=Decimal(str(row["split_amount"] or 0)).quantize(Decimal("0.01")),
payer=str(row["payer"] or ""),
people=self._split_values(str(row["people"] or "")),
has_attachment=bool(row["has_attachment"]),
attachment_count=len(attachment_items),
deleted=bool(row["deleted"]),
pinned=bool(row["pinned"]),
source=str(row["source"] or ""),
raw_text=str(row["raw_text"] or ""),
record_date=str(row["record_date"] or ""),
record_time=str(row["record_time"] or ""),
priority=int(row["priority"] or 0),
attachment_summary=str(row["attachment_summary"] or ""),
)
def get_record(
self,
kind: str,
record_id: int,
*,
include_deleted: bool = False,
) -> RecordView:
self._ensure_ready()
valid_kind = self._validate_kind(kind)
valid_id = self._validate_id(record_id)
select = self._note_select() if valid_kind == "note" else self._expense_select()
deleted_clause = "" if include_deleted else " AND deleted=0"
try:
with self._connect() as con:
row = con.execute(
f"SELECT * FROM ({select}) WHERE id=?{deleted_clause}",
(valid_id,),
).fetchone()
except sqlite3.OperationalError as exc:
if self._is_busy_error(exc):
raise DatabaseBusyError("資料庫忙碌,請稍後重試") from exc
raise
if row is None:
raise NotFoundError("找不到記錄")
return self._row_to_record(row)
@staticmethod
def _escape_like(value: str) -> str:
return value.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
def search_suggestions(
self,
prefix: str,
*,
kind: RecordKind | None = None,
scope: RecordScope = "active",
limit: int = 8,
start_date: str | None = None,
end_date: str | None = None,
) -> tuple[str, ...]:
"""Return a small, authenticated-UI-friendly set of matching values."""
self._ensure_ready()
if kind is not None:
self._validate_kind(kind)
if scope not in {"active", "deleted"}:
raise ValidationError("scope 必須是 active 或 deleted")
if type(limit) is not int or not 1 <= limit <= 20:
raise ValidationError("suggestion limit 必須介於 1 到 20")
value = self._clean_text(prefix, "搜尋字串", 100).strip()
if len(value) < 2:
return ()
deleted_value = 1 if scope == "deleted" else 0
normalized_start = self._normalize_date(start_date, start_date) if start_date else None
normalized_end = self._normalize_date(end_date, end_date) if end_date else None
if normalized_start and normalized_end and normalized_start > normalized_end:
raise ValidationError("日期範圍不正確")
like = f"%{self._escape_like(value)}%"
selects: list[str] = []
params: list[object] = []
def add_select(table: str, date_column: str, column: str) -> None:
clauses = ["deleted=?", column + " LIKE ? ESCAPE ?"]
local_params: list[object] = [deleted_value, like, "\\"]
if normalized_start:
clauses.append(date_column + ">=?")
local_params.append(normalized_start)
if normalized_end:
clauses.append(date_column + "<=?")
local_params.append(normalized_end)
selects.append(
"SELECT " + column + " AS value FROM " + table + " WHERE " + " AND ".join(clauses)
)
params.extend(local_params)
if kind in {None, "note"}:
for column in ("content", "category", "tags", "attachment_summary"):
add_select("notes", "note_date", column)
if kind in {None, "expense"}:
for column in ("content", "category", "payer", "people"):
add_select("expenses", "exp_date", column)
if not selects:
return ()
sql = f"""
SELECT value
FROM ({' UNION ALL '.join(selects)})
WHERE value IS NOT NULL AND TRIM(value) <> ''
GROUP BY value
ORDER BY value COLLATE NOCASE
LIMIT ?
"""
params.append(limit)
try:
with self._read_connection() as con:
rows = con.execute(sql, params).fetchall()
except sqlite3.OperationalError as exc:
if self._is_busy_error(exc):
raise DatabaseBusyError("資料庫忙碌,請稍後重試") from exc
raise
return tuple(str(row["value"])[:160] for row in rows)
@staticmethod
def _query_fingerprint(query: RecordQuery) -> str:
payload = {
"kind": query.kind,
"scope": query.scope,
"search": query.search,
"category": query.category,
"direction": query.direction,
"start_date": query.start_date,
"end_date": query.end_date,
"has_attachment": query.has_attachment,
}
raw = json.dumps(payload, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
return hashlib.sha256(raw.encode("utf-8")).hexdigest()[:20]
@staticmethod
def _encode_cursor(row: sqlite3.Row, fingerprint: str) -> str:
payload = {
"v": 1,
"f": fingerprint,
"p": int(row["pin_sort"] or 0),
"d": str(row["display_at"]),
"k": str(row["kind"]),
"i": int(row["id"]),
}
raw = json.dumps(payload, separators=(",", ":")).encode("utf-8")
return base64.urlsafe_b64encode(raw).decode("ascii").rstrip("=")
@staticmethod
def _decode_cursor(cursor: str, fingerprint: str) -> tuple[int, str, str, int]:
try:
padded = cursor + "=" * (-len(cursor) % 4)
payload = json.loads(base64.urlsafe_b64decode(padded.encode("ascii")).decode("utf-8"))
raw_pin_sort = payload["p"]
raw_record_id = payload["i"]
if type(raw_pin_sort) is not int or type(raw_record_id) is not int:
raise ValueError("cursor field types invalid")
pin_sort = raw_pin_sort
display_at = str(payload["d"])
kind = str(payload["k"])
record_id = raw_record_id
if payload.get("v") != 1 or payload.get("f") != fingerprint:
raise ValueError("cursor query mismatch")
if kind not in {"note", "expense"} or record_id <= 0 or pin_sort not in {0, 1}:
raise ValueError("cursor fields invalid")
datetime.strptime(display_at, "%Y-%m-%d %H:%M:%S")
return pin_sort, display_at, kind, record_id
except Exception as exc:
if isinstance(exc, InvalidCursorError):
raise
raise InvalidCursorError("無效或不相容的 cursor") from exc
def _validated_query(self, query: RecordQuery) -> RecordQuery:
if query.kind is not None:
self._validate_kind(query.kind)
if query.scope not in {"active", "deleted"}:
raise ValidationError("scope 必須是 active 或 deleted")
if query.direction not in {"", "expense", "income"}:
raise ValidationError("direction 不正確")
if query.has_attachment is not None and type(query.has_attachment) is not bool:
raise ValidationError("附件篩選必須是布林值或空值")
if type(query.limit) is not int:
raise ValidationError("limit 必須是 1 到 100 的整數")
if not 1 <= query.limit <= 100:
raise ValidationError("limit 必須介於 1 到 100")
if query.start_date:
self._normalize_date(query.start_date, query.start_date)
if query.end_date:
self._normalize_date(query.end_date, query.end_date)
if query.start_date and query.end_date and query.start_date > query.end_date:
raise ValidationError("日期範圍不正確")
self._clean_text(query.search, "搜尋字串", 300)
self._clean_text(query.category, "類別", 100)
return query
def list_records(self, query: RecordQuery | None = None) -> RecordPage:
self._ensure_ready()
query = self._validated_query(query or RecordQuery())
deleted_value = 1 if query.scope == "deleted" else 0