-
Notifications
You must be signed in to change notification settings - Fork 0
/
dao.py
50 lines (37 loc) · 1015 Bytes
/
dao.py
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
from datetime import datetime
from pony.orm import (
Database,
Optional,
PrimaryKey,
Required,
db_session,
)
import model
DB_FILE = "dida.db3"
db = Database()
class Task(db.Entity):
id = PrimaryKey(int, auto=True)
title = Required(str)
content = Optional(str, default="")
schedule = Optional(datetime)
created_at = Optional(datetime, default=datetime.now())
col_created_at = Optional(datetime, default=datetime.now().astimezone())
_table_ = "taskn"
@db_session
def create_task(t: model.Task):
Task(title=t.title, content=t.content, schedule=t.schedule, created_at=t.created_at)
@db_session
def random(limit=10) -> list[model.Task]:
q = Task.select_random(limit)
return [
model.Task(
id=t.id,
title=t.title,
content=t.content,
schedule=t.schedule,
created_at=t.created_at,
)
for t in q
]
db.bind(provider="sqlite", filename=DB_FILE)
db.generate_mapping()