Browse Source

Implements delete in leapidatasource for leobjects and lerelations

Yann Weber 9 years ago
parent
commit
dc143a3376

+ 11
- 37
DataSource/MySQL/leapidatasource.py View File

@@ -192,45 +192,19 @@ class LeDataSourceSQL(DummyDatasource):
192 192
 
193 193
     ## @brief delete lodel editorial components given filters
194 194
     # @param target_cls LeCrud(class): The component class concerned by the delete (a LeCrud child class (not instance !) )
195
-    # @param filters list : List of filters (see @ref leobject_filters)
196
-    # @param rel_filters list : List of relational filters (see @ref leobject_filters)
195
+    # @param uid int : A uniq ID to identify the object we want to delete
197 196
     # @return the number of deleted components
198
-    def delete(self, target_cls, filters, rel_filters):
199
-        query_table_name = self.datasource_utils.get_table_name_from_class(target_cls.__name__)
200
-        prep_filters = self._prepare_filters(filters, query_table_name)
201
-        prep_rel_filters = self._prepare_rel_filters(rel_filters)
202
-
203
-        if len(prep_rel_filters) > 0:
204
-            query = "DELETE %s FROM" % query_table_name
205
-            for prep_rel_filter in prep_rel_filters:
206
-                query += "%s INNER JOIN %s ON (%s.%s = %s.%s)" % (
207
-                    self.datasource_utils.relations_table_name,
208
-                    query_table_name,
209
-                    self.datasource_utils.relations_table_name,
210
-                    prep_rel_filter['position'],
211
-                    query_table_name,
212
-                    self.datasource_utils.field_lodel_id
213
-                )
214
-
215
-                if prep_rel_filter['condition_key'][0] is not None:
216
-                    prep_filters[("%s.%s" % (self.datasource_utils.relations_table_name, prep_rel_filter['condition_key'][0]), prep_rel_filter['condition_key'][1])] = prep_rel_filter['condition_value']
217
-
218
-            if prep_filters is not None and len(prep_filters) > 0:
219
-                query += " WHERE "
220
-                filter_counter = 0
221
-                for filter_item in prep_filters:
222
-                    if filter_counter > 1:
223
-                        query += " AND "
224
-                    query += "%s %s %s" % (filter_item[0][0], filter_item[0][1], filter_item[1])
197
+    def delete(self, target_cls, uid):
198
+        if target_cls.implements_leobject():
199
+            tablename = utils.common_tables['object']
200
+        elif target_cls.implements_lerelation():
201
+            tablename = utils.common_tables['relation']
225 202
         else:
226
-            query = delete(query_table_name, prep_filters)
227
-
228
-        query_delete_from_object = delete(self.datasource_utils.objects_table_name, {'lodel_id': filters['lodel_id']})
229
-        with self.connection as cur:
230
-            result = cur.execute(query)
231
-            cur.execute(query_delete_from_object)
232
-
233
-        return result
203
+            raise AttributeError("'%s' is not a LeObject nor a LeRelation, it's not possible to delete it")
204
+        uidname = target_cls.uidname()
205
+        sql = delete(tablename, ((uidname, uid),))
206
+        utils.query(self.connection, sql) #No way to check the result ?
207
+        return True
234 208
 
235 209
     ## @brief update ONE existing lodel editorial component
236 210
     # @param target_cls LeCrud(class) : Instance of the object concerned by the update

+ 2
- 3
DataSource/dummy/leapidatasource.py View File

@@ -25,10 +25,9 @@ class DummyDatasource(object):
25 25
 
26 26
     ## @brief delete lodel editorial components given filters
27 27
     # @param target_cls LeCrud(class) : The component class concerned by the insert (a LeCrud child class (not instance !) )
28
-    # @param filters list : List of filters (see @ref leobject_filters )
29
-    # @param rel_filters list : List of relationnal filters (see @ref leobject_filters )
28
+    # @param leo_id int : The component ID (lodel_id or relation_id)
30 29
     # @return the number of deleted components
31
-    def delete(self, target_cls, filters, rel_filters):
30
+    def delete(self, target_cls, leo_id):
32 31
         pass
33 32
 
34 33
     ## @brief update an existing lodel editorial component

+ 3
- 17
leapi/lecrud.py View File

@@ -289,17 +289,11 @@ class _LeCrud(object):
289 289
             #ERROR HANDLING
290 290
             return False
291 291
     
292
-    ## @brief Delete a component (instance method)
292
+    ## @brief Delete a component
293 293
     # @return True if success
294 294
     # @todo better error handling
295
-    def _delete(self):
296
-        filters = [self._id_filter()]
297
-        ret = _LeCrud.delete(self.__class__, filters)
298
-        if ret == 1:
299
-            return True
300
-        else:
301
-            #ERROR HANDLING
302
-            return False
295
+    def delete(self):
296
+        self._datasource.delete(self.__class__, self.uidget())
303 297
 
304 298
     ## @brief Check that datas are valid for this type
305 299
     # @param datas dict : key == field name value are field values
@@ -345,14 +339,6 @@ class _LeCrud(object):
345 339
             raise LeApiDataCheckError("Error while checking datas", err_l)
346 340
         return checked_datas
347 341
     
348
-    ## @brief Given filters delete editorial components
349
-    # @param filters list : 
350
-    # @return The number of deleted components
351
-    @staticmethod
352
-    def delete(cls, filters):
353
-        filters, rel_filters = cls._prepare_filters(filters)
354
-        return cls._datasource.delete(cls, filters, rel_filters)
355
-
356 342
     ## @brief Retrieve a collection of lodel editorial components
357 343
     #
358 344
     # @param query_filters list : list of string of query filters (or tuple (FIELD, OPERATOR, VALUE) ) see @ref leobject_filters

+ 0
- 13
leapi/leobject.py View File

@@ -96,19 +96,6 @@ class _LeObject(_LeCrud):
96 96
             return ('class_id', '=', cls._class_id)
97 97
         else:
98 98
             raise ValueError("Cannot generate a typefilter with %s class"%cls.__name__)
99
-    
100
-    ## @brief Delete LeObjects from db given filters and a classname
101
-    # @note if no classname given, take the caller class
102
-    # @param filters list : 
103
-    # @param classname None|str : the classname or None
104
-    # @return number of deleted LeObjects
105
-    # @see leapi.lecrud._LeCrud.delete()
106
-    @classmethod
107
-    def delete(cls, filters, classname = None):
108
-        ccls = cls if classname is None else cls.name2class(classname)
109
-        new_filters = copy.copy(filters)
110
-        new_filters.append(ccls.typefilter())
111
-        return _LeCrud.delete(ccls, new_filters)
112 99
 
113 100
     ## @brief Check that a relational field is valid
114 101
     # @param field str : a relational field

+ 0
- 20
leapi/lerelation.py View File

@@ -72,18 +72,6 @@ class _LeRelation(lecrud._LeCrud):
72 72
             res_filters.append( (field, op, value) )
73 73
         return res_filters, rel_filters
74 74
 
75
-    @classmethod
76
-    ## @brief deletes a relation between two objects
77
-    # @param filters_list list
78
-    # @param target_class str
79
-    def delete(cls, filters_list, target_class):
80
-        filters, rel_filters = cls._prepare_filters(filters_list)
81
-        if isinstance(target_class, str):
82
-            target_class = cls.name2class(target_class)
83
-
84
-        ret = cls._datasource.delete(target_class, filters)
85
-        return True if ret == 1 else False
86
-
87 75
     ## @brief move to the first rank
88 76
     # @return True in case of success, False in case of failure
89 77
     def move_first(self):
@@ -143,10 +131,6 @@ class _LeRelation(lecrud._LeCrud):
143 131
 ## @brief Abstract class to handle hierarchy relations
144 132
 class _LeHierarch(_LeRelation):
145 133
     
146
-    ## @brief Delete current instance from DB
147
-    def delete(self):
148
-        lecrud._LeCrud._delete(self)
149
-    
150 134
     ## @brief modify a LeHierarch rank
151 135
     # @param new_rank int|str : The new rank can be an integer > 1 or strings 'first' or 'last'
152 136
     # @return True in case of success, False in case of failure
@@ -205,10 +189,6 @@ class _LeRel2Type(_LeRelation):
205 189
     ## @brief Stores the relation name for a rel2type
206 190
     _relation_name = None
207 191
 
208
-    ## @brief Delete current instance from DB
209
-    def delete(self):
210
-        lecrud._LeCrud._delete(self)
211
-    
212 192
     ## @brief modify a LeRel2Type rank
213 193
     # @param new_rank int|str : The new rank can be an integer > 1 or strings 'first' or 'last'
214 194
     # @return True in case of success, False in case of failure

+ 0
- 4
leapi/letype.py View File

@@ -68,10 +68,6 @@ class _LeType(_LeClass):
68 68
         self.populate()
69 69
         return self.datas()
70 70
     
71
-    ## @brief Delete current instance from DB
72
-    def delete(self):
73
-        _LeCrud._delete(self)
74
-    
75 71
     ## @brief Add a superior
76 72
     # @param lesup LeObject : LeObject child class instance
77 73
     # @param nature str : Relation nature

+ 9
- 24
leapi/test/test_leobject.py View File

@@ -159,32 +159,17 @@ class LeObjectMockDatasourceTestCase(TestCase):
159 159
     
160 160
     @patch('DataSource.dummy.leapidatasource.DummyDatasource.delete')
161 161
     def test_delete(self, dsmock):
162
-        from dyncode import Publication, Numero, LeObject, LeType
163
-
162
+        from dyncode import Publication, Numero, LeObject, LeType, LeRelation
163
+        
164 164
         args = [
165
-            (
166
-                'Numero',
167
-                ['lodel_id=1'],
168
-                [('lodel_id', '=', '1'), ('type_id', '=', Numero._type_id)],
169
-                []
170
-            ),
171
-            (
172
-                'Publication',
173
-                ['subordinate.parent not in [1,2,3]', 'titre = "titre nul"'],
174
-                [('titre','=', '"titre nul"'), ('class_id', '=', Publication._class_id)],
175
-                [( (leapi.leobject.REL_SUB, 'parent'), ' not in ', '[1,2,3]')]
176
-            ),
165
+            Publication(1),
166
+            Numero(5),
167
+            LeObject(51),
168
+            LeRelation(1337),
177 169
         ]
178 170
 
179
-        for classname, filters, ds_filters, ds_relfilters in args:
180
-            ccls = LeObject.name2class(classname)
181
-
182
-            LeObject.delete(filters, classname)
183
-            dsmock.assert_called_once_with(ccls, ds_filters, ds_relfilters)
171
+        for instance in args:
172
+            instance.delete()
173
+            dsmock.assert_called_once_with(instance.__class__, instance.uidget())
184 174
             dsmock.reset_mock()
185 175
             
186
-            if not (LeType in ccls.__bases__): #tests for calls with a LeClass child
187
-                ccls.delete(filters)
188
-                dsmock.assert_called_once_with(ccls, ds_filters, ds_relfilters)
189
-                dsmock.reset_mock()
190
-        

+ 3
- 3
leapi/test/test_lerelation.py View File

@@ -74,8 +74,8 @@ class LeRelationTestCase(TestCase):
74 74
         """ Testing LeHierarch insert method """
75 75
         from dyncode import LeCrud, Publication, Numero, Personnes, LeObject, Rubrique, LeHierarch, LeRelation
76 76
         
77
-        LeRelation.delete([LeRelation.sup_filter(Numero(42)), 'nature = "parent"'], 'LeHierarch')
78
-        dsmock.assert_called_once_with(LeHierarch, [('superior', '=', Numero(42)), ('nature','=','"parent"')])
77
+        LeHierarch(42).delete()
78
+        dsmock.assert_called_once_with(LeHierarch, 42)
79 79
         dsmock.reset_mock()
80 80
 
81 81
 
@@ -200,7 +200,7 @@ class LeHierarch(LeRelationTestCase):
200 200
         from dyncode import LeCrud, Publication, Numero, Personnes, LeObject, Rubrique, LeHierarch, LeRelation
201 201
         rel = LeHierarch(10)
202 202
         rel.delete()
203
-        dsmock.assert_called_once_with(LeHierarch, [(LeHierarch.uidname(), '=', 10)], [])
203
+        dsmock.assert_called_once_with(LeHierarch, 10)
204 204
         
205 205
     
206 206
     @unittest.skip("Wait for LeRelation.update() to unskip")

+ 1
- 1
leapi/test/test_letype.py View File

@@ -129,5 +129,5 @@ class LeTypeMockDsTestCase(TestCase):
129 129
         
130 130
         num = Numero(lodel_id = 1)
131 131
         num.delete()
132
-        dsmock.assert_called_once_with(Numero, [('lodel_id','=',1)], [])
132
+        dsmock.assert_called_once_with(Numero, 1)
133 133
 

Loading…
Cancel
Save