2
2
3
3
from sqlalchemy import delete
4
4
from sqlalchemy import select
5
+ from sqlalchemy import update
5
6
from sqlalchemy .engine import Result
6
7
7
8
8
9
class CreateMixin :
9
- """Create object in database."""
10
+ """Create object in database.
11
+
12
+ This mixin supports the following options in the Meta class:
13
+ ```
14
+ class CustomController(CreateMixin, BaseCRUD):
15
+ class Meta:
16
+ session = Session
17
+ model = Model
18
+ input_schema_of_create = InputSchema
19
+ output_schema_of_create = OutputSchema
20
+
21
+ custom_controller = CustomController()
22
+ ```
23
+
24
+ `input_schema_of_create` - marshmallow schema for validating and deserialization input data.
25
+ `output_schema_of_create` - marshmallow schema for serialization output data.
26
+ """
27
+
28
+ def create_object (self , ** data ) -> Result :
29
+ """If this method does not suit you, simply override it in your class."""
10
30
11
- def _create_object (self , ** data ) -> Result :
12
31
session = self ._get_option_from_meta ('session' )
13
32
model = self ._get_option_from_meta ('model' )
14
33
@@ -17,69 +36,70 @@ def _create_object(self, **data) -> Result:
17
36
session .commit ()
18
37
return new_obj
19
38
20
- def create (self , data : dict , validating : bool = True , jsonify : bool = False ) -> Result or dict :
21
- if validating :
22
- self ._deserialize_data ('input_schema_of_create' , data )
23
-
24
- new_object = self ._create_object (** data )
39
+ def create (self , data : dict , serialize : bool = False ) -> Result or dict :
40
+ deserialized_data = self .deserialize_data ('input_schema_of_create' , data )
41
+ new_object = self .create_object (** deserialized_data )
25
42
26
- if jsonify :
27
- return self ._data_to_json ('output_schema_of_create' , new_object )
43
+ if serialize :
44
+ return self .serialize_data ('output_schema_of_create' , new_object )
28
45
29
46
return new_object
30
47
31
48
32
49
class ReadMixin :
33
50
"""Read object from database."""
34
51
35
- def _read_object (self , id : Any ) -> Result :
52
+ def get_object (self , id : Any ) -> Result :
53
+ """If this method does not suit you, simply override it in your class."""
54
+
36
55
session = self ._get_option_from_meta ('session' )
37
56
model = self ._get_option_from_meta ('model' )
38
57
return session .scalars (select (model ).where (model .id == id )).one ()
39
58
40
- def read (self , id , jsonify : bool = False ) -> Result or dict :
41
- obj = self ._read_object (id )
59
+ def read (self , id , serialize : bool = False ) -> Result or dict :
60
+ obj = self .get_object (id )
42
61
43
- if jsonify :
44
- return self ._data_to_json ('output_schema_of_read' , obj )
62
+ if serialize :
63
+ return self .serialize_data ('output_schema_of_read' , obj )
45
64
46
65
return obj
47
66
48
67
49
68
class UpdateMixin :
50
69
"""Update object in database."""
51
70
52
- def _update_object (self , id : Any , ** data ) -> Result :
71
+ def update_object (self , id : Any , ** data ) -> Result :
72
+ """If this method does not suit you, simply override it in your class."""
73
+
53
74
session = self ._get_option_from_meta ('session' )
54
75
model = self ._get_option_from_meta ('model' )
55
76
77
+ stmt = update (model ).where (model .id == id ).values (** data )
78
+ session .execute (stmt )
79
+
56
80
obj = session .scalars (select (model ).where (model .id == id )).one ()
57
- for k , v in data .items ():
58
- setattr (obj , k , v )
59
- session .commit ()
60
81
return obj
61
82
62
- def update (self , data : dict , validating : bool = True , jsonify : bool = False ) -> Result or dict :
63
- if validating :
64
- self ._deserialize_data ( 'input_schema_of_update' , data )
83
+ def update (self , data : dict , serialize : bool = False ) -> Result or dict :
84
+ deserialized_data = self . deserialize_data ( 'input_schema_of_update' , data )
85
+ updated_object = self .update_object ( ** deserialized_data )
65
86
66
- updated_object = self ._update_object (** data )
67
-
68
- if jsonify :
69
- return self ._data_to_json ('output_schema_of_update' , updated_object )
87
+ if serialize :
88
+ return self .serialize_data ('output_schema_of_update' , updated_object )
70
89
71
90
return updated_object
72
91
73
92
74
93
class DeleteMixin :
75
94
"""Delete object from database."""
76
95
77
- def _delete_object (self , id : Any ) -> None :
96
+ def delete_object (self , id : Any ) -> None :
97
+ """If this method does not suit you, simply override it in your class."""
98
+
78
99
session = self ._get_option_from_meta ('session' )
79
100
model = self ._get_option_from_meta ('model' )
80
101
81
102
session .execute (delete (model ).where (model .id == id ))
82
- session .commit ()
83
103
84
104
def delete (self , id : Any ) -> None :
85
- self ._delete_object (id )
105
+ self .delete_object (id )
0 commit comments