Browse Source

LeObject: begining of implementation

ArnAud 9 years ago
parent
commit
535c8b7e22
2 changed files with 136 additions and 0 deletions
  1. 45
    0
      leobject/datasources/dummy.py
  2. 91
    0
      leobject/leobject.py

+ 45
- 0
leobject/datasources/dummy.py View File

@@ -0,0 +1,45 @@
1
+#-*- coding: utf-8 -*-
2
+
3
+
4
+## dummy datasource for LeObject
5
+# This class has to be extended to apply to a real datasource
6
+# But it can be used as an empty and debug datasource
7
+
8
+class DummyDatasource(object):
9
+
10
+    def __init__(self, options=None):
11
+        self.options = options
12
+
13
+    ## @brief create a new LeObject
14
+    # @param data dict: a dictionnary of field:value to save
15
+    # @return lodel_id int: new lodel_id of the newly created LeObject
16
+    def insert(self, data):
17
+        print("DummyDatasource.insert: ", data)
18
+        return 42
19
+
20
+    ## @brief update an existing LeObject
21
+    # @param lodel_id int | (int): lodel_id of the object(s) where to apply changes
22
+    # @param data dict: dictionnary of field:value to save
23
+    # @param update_filters string | (string): list of string of update filters
24
+    # @return okay bool: True on success, it will raise on failure
25
+    def update(self, lodel_id, data, update_filters=None):
26
+        print("DummyDatasource.update: ", lodel_id, data, update_filters)
27
+
28
+        return True
29
+
30
+    ## @brief delete an existing LeObject
31
+    # @param lodel_id int | (int): lodel_id of the object(s) to delete
32
+    # @param delete_filters string | (string): list of string of delete filters
33
+    # @return okay bool: True on success, it will raise on failure
34
+    def delete(self, lodel_id, delete_filters=None):
35
+        print("DummyDatasource.delete: ", lodel_id, delete_filters)
36
+
37
+        return True
38
+
39
+    ## @brief make a search to retrieve a collection of LeObject
40
+    # @param query_filters string | (string): list of string of query filters
41
+    # @return responses ({string:*}): a list of dict with field:value
42
+    def get(self, query_filters):
43
+        print("DummyDatasource.get: ", query_filters)
44
+
45
+        return False

+ 91
- 0
leobject/leobject.py View File

@@ -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

Loading…
Cancel
Save