-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
75 lines (57 loc) · 1.81 KB
/
db.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
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
from datetime import datetime
from pony.orm import Database, Required, PrimaryKey, commit, db_session, Set
from typing import Type
from uuid import uuid4
from base import BaseRepository
COLORS = {
"orange": "#f07a19",
"brown": "#896415",
"black": "#000000",
"white": "#ffffff",
"red": "#e53838",
"grey": "#989090",
}
db = Database()
class Bird(db.Entity):
"""Bird model."""
id = PrimaryKey(str, default=lambda: str(uuid4()))
name = Required(str)
description = Required(str)
feather_color = Required(str)
seen_acts = Set("SeenAct")
picture = Required(str) # represents file path
class BirdRepository(BaseRepository):
"""Bird repository class."""
entity: Type[db.Entity] = Bird
@classmethod
@db_session
def create(cls, name: str, feather_color: str, description: str, picture=None) -> db.Entity:
bird = cls.entity(
name=name,
feather_color=feather_color,
description=description,
picture=picture
)
commit()
return bird
class SeenAct(db.Entity):
"""Seen act model."""
id = PrimaryKey(str, default=lambda: str(uuid4()))
bird = Required(Bird)
time_seen = Required(datetime, default=lambda: datetime.now())
class SeenActRepository(BaseRepository):
"""Seen bird act repository class."""
entity: Type[db.Entity] = SeenAct
@classmethod
@db_session
def create(cls, bird_id: str) -> db.Entity:
seen_act = cls.entity(bird=bird_id)
commit()
return seen_act
@classmethod
@db_session
def get_acts_by_bird(cls, bird_id: str) -> db.Entity:
return cls.entity.select(lambda act: act.bird == bird_id)
# bing engine and create schemas
db.bind('sqlite', 'db4.sqlite', create_db=True)
db.generate_mapping(create_tables=True)