Browse Source

Implements delete() and update() methods in LeType and _LeObject

Yann Weber 9 years ago
parent
commit
25c7f607af
2 changed files with 47 additions and 51 deletions
  1. 31
    37
      leobject/leobject.py
  2. 16
    14
      leobject/letype.py

+ 31
- 37
leobject/leobject.py View File

@@ -59,43 +59,37 @@ class _LeObject(object):
59 59
         letype,leclass = cls._prepare_targets(letype)
60 60
         if letype is None:
61 61
             raise ValueError("letype argument cannot be None")
62
-        return cls._datasource.insert(letype, leclass, datas)
63 62
 
64
-    ## @brief update an existing LeObject
65
-    # @param lodel_id int | (int): lodel_id of the object(s) where to apply changes
66
-    # @param data dict: dictionnary of field:value to save
67
-    # @param update_filters string | (string): list of string of update filters
68
-    # @return okay bool: True on success, it will raise on failure
69
-    def update(self, lodel_id, data, update_filters=None):
70
-        if not lodel_id:
71
-            lodel_id = ()
72
-        elif isinstance(lodel_id, int):
73
-            lodel_id = (lodel_id)
74
-
75
-        try:
76
-            checked_data = self._check_data(data)
77
-            datasource_filters = self._prepare_filters(update_filters)
78
-            okay = self.datasource.update(lodel_id, checked_data, datasource_filters)
79
-        except:
80
-            raise
81
-        return okay
82
-
83
-    ## @brief delete an existing LeObject
84
-    # @param lodel_id int | (int): lodel_id of the object(s) to delete
85
-    # @param delete_filters string | (string): list of string of delete filters
86
-    # @return okay bool: True on success, it will raise on failure
87
-    def delete(self, lodel_id, delete_filters=None):
88
-        if not lodel_id:
89
-            lodel_id = ()
90
-        elif isinstance(lodel_id, int):
91
-            lodel_id = (lodel_id)
92
-
93
-        try:
94
-            datasource_filters = self._prepare_filters(delete_filters)
95
-            okay = self.datasource.delete(lodel_id, datasource_filters)
96
-        except:
97
-            raise
98
-        return okay
63
+        for data in datas:
64
+            letype.check_datas_or_raise(data, complete = True)
65
+
66
+        return cls._datasource.insert(letype, leclass, datas)
67
+    
68
+    ## @brief Delete LeObjects given filters
69
+    # @param cls
70
+    # @param letype LeType|str : LeType child class or name
71
+    # @param leclass LeClass|str : LeClass child class or name
72
+    # @param filters list : list of filters (see @ref leobject_filters)
73
+    # @return bool
74
+    @classmethod
75
+    def delete(cls, letype, leclass, filters):
76
+        filters,relationnal_filters = leobject.leobject._LeObject._prepare_filters(filters, cls, cls._leclass)
77
+        letype, leclass = cls._prepare_targets(letype, leclass)
78
+        return cls._datasource(letype, leclass, filters, relationnal_filters)
79
+    
80
+    ## @brief Update LeObjects given filters and datas
81
+    # @param cls
82
+    # @param letype LeType|str : LeType child class or name
83
+    # @param leclass LeClass|str : LeClass child class or name
84
+    # @param filters list : list of filters (see @ref leobject_filters)
85
+    @classmethod
86
+    def update(cls, letype, leclass, filters, datas):
87
+        filters,relationnal_filters = leobject.leobject._LeObject._prepare_filters(filters, cls, cls._leclass)
88
+        letype, leclass = cls._prepare_targets(letype, leclass)
89
+        if letype is None:
90
+            raise ValueError("Argument letype cannot be None")
91
+        letype.check_datas_or_raise(datas, False)
92
+        return cls._datasource(letype, leclass, filters, relationnal_filters, datas)
99 93
 
100 94
     ## @brief make a search to retrieve a collection of LeObject
101 95
     # @param query_filters list : list of string of query filters (or tuple (FIELD, OPERATOR, VALUE) ) see @ref leobject_filters
@@ -105,7 +99,7 @@ class _LeObject(object):
105 99
     # @return responses ({string:*}): a list of dict with field:value
106 100
     def get(self, query_filters, field_list = None, typename = None, classname = None):
107 101
 
108
-        letype,leclass = self._prepare_target(typename, classname)
102
+        letype,leclass = self._prepare_targets(typename, classname)
109 103
 
110 104
         #Fetching LeType
111 105
         if typename is None:

+ 16
- 14
leobject/letype.py View File

@@ -10,13 +10,14 @@
10 10
 #
11 11
 # @note LeObject will be generated by leobject.lefactory.LeFactory
12 12
 
13
-from Lodel.utils.classinstancemethod import classinstancemethod
13
+import leobject
14 14
 from leobject.leclass import LeClass
15 15
 from leobject.leobject import LeObjectError
16
+from Lodel.utils.classinstancemethod import classinstancemethod
16 17
 
17 18
 ## @brief Represent an EmType data instance
18 19
 # @note Is not a derivated class of LeClass because the concrete class will be a derivated class from LeClass
19
-class LeType(object):
20
+class LeType(leobject.leobject._LeObject):
20 21
     
21 22
     ## @brief Stores selected fields with key = name
22 23
     _fields = list()
@@ -68,33 +69,37 @@ class LeType(object):
68 69
             
69 70
 
70 71
     ## @brief Delete the LeType from Db
71
-    # @note equivalent to LeType::delete(this.lodel_id)
72
+    # @note equivalent to LeType::delete(filters=['lodel_id = %s'%self.lodel_id)
72 73
     @classinstancemethod
73 74
     def delete(self):
74
-        return self.__class__.delete(('lodel_id','=',self.lodel_id))
75
+        return self.__class__.delete([('lodel_id','=',self.lodel_id)])
75 76
     
76 77
     ## @brief Delete a LeType from the datasource
77
-    # @param filters list : List of filters
78
+    # @param filters list : list of filters (see @ref leobject_filters)
78 79
     # @param cls
79 80
     # @return True if deleted False if not existing
80 81
     # @throw InvalidArgumentError if invalid parameters
81 82
     # @throw Leo exception if the lodel_id identify an object from another type
82 83
     @delete.classmethod
83 84
     def delete(cls, filters):
84
-        pass
85
+        return super(LeType, cls).delete(letype, leclass, filters)
85 86
         
86 87
 
87 88
     @classinstancemethod
88 89
     ## @brief Update a LeType in db
89 90
     def update(self):
90
-        pass
91
+        return self.__class__.update(filters=[('lodel_id', '=', self.lodel_id)], datas = self.datas)
92
+        
91 93
 
92 94
     @update.classmethod
93 95
     ## @brief Update some LeType in db
94 96
     # @param datas : keys are lodel_id and value are dict of fieldname => value
95
-    # @throw Some exception for some errors in datas
96
-    def update(cls, datas):
97
-        pass
97
+    # @param filters list : list of filters (see @ref leobject_filters)
98
+    # @param cls
99
+    # return bool
100
+    def update(cls, filters, datas):
101
+        return super(LeType, cls).update(letype = cls, leclass = cls._leclass, filters = filters, datas = datas)
102
+        
98 103
 
99 104
     ## @brief Insert a new LeType in the datasource
100 105
     # @param **datas list : A list of dict containing the datas
@@ -103,10 +108,7 @@ class LeType(object):
103 108
     # @throw InvalidArgumentError if invalid argument
104 109
     @classmethod
105 110
     def insert(cls, datas):
106
-        for data in datas:
107
-            cls.check_datas_or_raise(data, complete=True)
108
-        super(LeType, cls).insert(letype=cls, datas=datas)
109
-        pass
111
+        return super(LeType, cls).insert(letype=cls, datas=datas)
110 112
     
111 113
     ## @brief Check that datas are valid for this type
112 114
     # @param datas dict : key == field name value are field values

Loading…
Cancel
Save