|
@@ -0,0 +1,91 @@
|
|
1
|
+#-*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+## Main class to handle objects defined by the types of an Editorial Model
|
|
4
|
+# an instance of these objects is pedantically called LeObject !
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+class LeObject(object):
|
|
8
|
+
|
|
9
|
+ ## Instantiate with a Model and a DataSource
|
|
10
|
+ def __init__(self, model, datasource):
|
|
11
|
+ self.model = model
|
|
12
|
+ self.datasource = datasource
|
|
13
|
+
|
|
14
|
+ ## @brief create a new LeObject
|
|
15
|
+ # @param data dict: a dictionnary of field:value to save
|
|
16
|
+ # @return lodel_id int: new lodel_id of the newly created LeObject
|
|
17
|
+ def insert(self, data):
|
|
18
|
+ try:
|
|
19
|
+ checked_data = self._check_data(data)
|
|
20
|
+ lodel_id = self.datasource.insert(checked_data)
|
|
21
|
+ except:
|
|
22
|
+ raise
|
|
23
|
+ return lodel_id
|
|
24
|
+
|
|
25
|
+ ## @brief update an existing LeObject
|
|
26
|
+ # @param lodel_id int | (int): lodel_id of the object(s) where to apply changes
|
|
27
|
+ # @param data dict: dictionnary of field:value to save
|
|
28
|
+ # @param update_filters string | (string): list of string of update filters
|
|
29
|
+ # @return okay bool: True on success, it will raise on failure
|
|
30
|
+ def update(self, lodel_id, data, update_filters=None):
|
|
31
|
+ if not lodel_id:
|
|
32
|
+ lodel_id = ()
|
|
33
|
+ elif isinstance(lodel_id, int):
|
|
34
|
+ lodel_id = (lodel_id)
|
|
35
|
+
|
|
36
|
+ try:
|
|
37
|
+ checked_data = self._check_data(data)
|
|
38
|
+ datasource_filters = self._prepare_filters(update_filters)
|
|
39
|
+ okay = self.datasource.update(lodel_id, checked_data, datasource_filters)
|
|
40
|
+ except:
|
|
41
|
+ raise
|
|
42
|
+ return okay
|
|
43
|
+
|
|
44
|
+ ## @brief delete an existing LeObject
|
|
45
|
+ # @param lodel_id int | (int): lodel_id of the object(s) to delete
|
|
46
|
+ # @param delete_filters string | (string): list of string of delete filters
|
|
47
|
+ # @return okay bool: True on success, it will raise on failure
|
|
48
|
+ def delete(self, lodel_id, delete_filters=None):
|
|
49
|
+ if not lodel_id:
|
|
50
|
+ lodel_id = ()
|
|
51
|
+ elif isinstance(lodel_id, int):
|
|
52
|
+ lodel_id = (lodel_id)
|
|
53
|
+
|
|
54
|
+ try:
|
|
55
|
+ datasource_filters = self._prepare_filters(delete_filters)
|
|
56
|
+ okay = self.datasource.delete(lodel_id, datasource_filters)
|
|
57
|
+ except:
|
|
58
|
+ raise
|
|
59
|
+ return okay
|
|
60
|
+
|
|
61
|
+ ## @brief make a search to retrieve a collection of LeObject
|
|
62
|
+ # @param query_filters string | (string): list of string of query filters
|
|
63
|
+ # @return responses ({string:*}): a list of dict with field:value
|
|
64
|
+ def get(self, query_filters):
|
|
65
|
+ try:
|
|
66
|
+ datasource_filters = self._prepare_filters(query_filters)
|
|
67
|
+ responses = self.datasource.get(datasource_filters)
|
|
68
|
+ except:
|
|
69
|
+ raise
|
|
70
|
+
|
|
71
|
+ return responses
|
|
72
|
+
|
|
73
|
+ ## @brief check if data dict fits with the model
|
|
74
|
+ # @param data dict: dictionnary of field:value to check
|
|
75
|
+ # @return checked_data ({string:*}): a list of dict with field:value
|
|
76
|
+ # @todo implent !
|
|
77
|
+ def _check_data(self, data):
|
|
78
|
+ checked_data = data
|
|
79
|
+ return checked_data
|
|
80
|
+
|
|
81
|
+ ## @brief check and prepare query for the datasource
|
|
82
|
+ # @param query_filters (string): list of string of query filters
|
|
83
|
+ # @todo implent !
|
|
84
|
+ def _prepare_filters(self, query_filters):
|
|
85
|
+ if query_filters is None:
|
|
86
|
+ query_filters = ()
|
|
87
|
+ elif isinstance(query_filters, str):
|
|
88
|
+ query_filters = (query_filters)
|
|
89
|
+
|
|
90
|
+ prepared_filters = query_filters
|
|
91
|
+ return prepared_filters
|