-
Notifications
You must be signed in to change notification settings - Fork 68
/
activerecord.py
101 lines (84 loc) · 2.86 KB
/
activerecord.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
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
from .utils import classproperty
from .session import SessionMixin
from .inspection import InspectionMixin
class ModelNotFoundError(ValueError):
pass
class ActiveRecordMixin(InspectionMixin, SessionMixin):
__abstract__ = True
@classproperty
def settable_attributes(cls):
return cls.columns + cls.hybrid_properties + cls.settable_relations
def fill(self, **kwargs):
for name in kwargs.keys():
if name in self.settable_attributes:
setattr(self, name, kwargs[name])
else:
raise KeyError("Attribute '{}' doesn't exist".format(name))
return self
def save(self, commit=True):
"""Saves the updated model to the current entity db.
:param commit: where to commit the transaction
"""
self.session.add(self)
if commit:
self._commit_or_fail()
return self
@classmethod
def create(cls, commit=True, **kwargs):
"""Create and persist a new record for the model
:param commit: where to commit the transaction
:param kwargs: attributes for the record
:return: the new model instance
"""
return cls().fill(**kwargs).save(commit=commit)
def update(self, commit=True, **kwargs):
"""Same as :meth:`fill` method but persists changes to database.
:param commit: where to commit the transaction
"""
return self.fill(**kwargs).save(commit=commit)
def delete(self, commit=True):
"""Removes the model from the current entity session and mark for deletion.
:param commit: where to commit the transaction
"""
self.session.delete(self)
if commit:
self._commit_or_fail()
def _commit_or_fail(self):
try:
self.session.commit()
except:
self.session.rollback()
raise
@classmethod
def destroy(cls, *ids, commit=True):
"""Delete the records with the given ids
:type ids: list
:param ids: primary key ids of records
:param commit: where to commit the transaction
"""
for pk in ids:
obj = cls.find(pk)
if obj:
obj.delete(commit=commit)
cls.session.flush()
@classmethod
def all(cls):
return cls.query.all()
@classmethod
def first(cls):
return cls.query.first()
@classmethod
def find(cls, id_):
"""Find record by the id
:param id_: the primary key
"""
return cls.query.get(id_)
@classmethod
def find_or_fail(cls, id_):
# assume that query has custom get_or_fail method
result = cls.find(id_)
if result:
return result
else:
raise ModelNotFoundError("{} with id '{}' was not found"
.format(cls.__name__, id_))