-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.py
More file actions
59 lines (43 loc) · 1.54 KB
/
Copy pathdb.py
File metadata and controls
59 lines (43 loc) · 1.54 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
"""
Database helpers
"""
from clay.linq import extend
from clay.models import Abstract, Model
from clay.text import uncapitalize, consume_next
from clay.utils import qualify
db_repos = extend([])
class RelationalModel(Model, Abstract):
def __init__(self, *initial_data, **kwargs):
self.raise_if_base(RelationalModel)
super().__init__(*initial_data, **kwargs)
def can_delete(self):
child_name = self._child_name(self)
navigation = child_name + 'Id'
# check all tables to see if the ID is referenced
# TODO: improve performance
has_records = any(db_repos \
.where(lambda repo:
repo.read().where(lambda x: navigation in x and \
x[navigation] == self['id'])
)
)
return not has_records
def _child_name(self, model):
model_name = qualify(model)
if '.' in model_name:
model_name = consume_next(model_name, '.')
child_name = uncapitalize(model_name)
return child_name
def child_name(self, repo):
return self._child_name(repo.model)
def foreign_key(self, repo):
child_name = self.child_name(repo)
model = repo.read() \
.where(lambda x: x['id'] == self[child_name + 'Id']) \
.first_or_default()
return model
def inverse_prop(self, repo):
child_name = uncapitalize(qualify(self))
# try and read the entities
return repo.read() \
.where(lambda x: x[child_name + 'Id'] == self['id'])