Browse Source

Update the relationnal filters parse/storage and associated tests

Yann Weber 9 years ago
parent
commit
ec033f63fb
2 changed files with 14 additions and 8 deletions
  1. 11
    5
      leobject/leobject.py
  2. 3
    3
      leobject/test/test_leobject.py

+ 11
- 5
leobject/leobject.py View File

228
                 filters.append(_LeObject._split_filter(fil))
228
                 filters.append(_LeObject._split_filter(fil))
229
 
229
 
230
         #Checking relational filters (for the moment fields like superior.NATURE)
230
         #Checking relational filters (for the moment fields like superior.NATURE)
231
-        relational_filters = [ (_LeObject._nature_from_relational_field(field), operator, value) for field, operator, value in filters if _LeObject._field_is_relational(field)]
231
+        relational_filters = [ (_LeObject._prepare_relational_field(field), operator, value) for field, operator, value in filters if _LeObject._field_is_relational(field)]
232
         filters = [f for f in filters if not _LeObject._field_is_relational(f[0])]
232
         filters = [f for f in filters if not _LeObject._field_is_relational(f[0])]
233
         #Checking the rest of the fields
233
         #Checking the rest of the fields
234
         _LeObject._check_fields(letype, leclass, [ f[0] for f in filters ])
234
         _LeObject._check_fields(letype, leclass, [ f[0] for f in filters ])
241
     # @return True if the field is relational else False
241
     # @return True if the field is relational else False
242
     @staticmethod
242
     @staticmethod
243
     def _field_is_relational(field):
243
     def _field_is_relational(field):
244
-        return field.startswith('superior.')
244
+        return field.startswith('superior.') or field.startswith('subordinate')
245
     
245
     
246
     ## @brief Check that a relational field is valid
246
     ## @brief Check that a relational field is valid
247
     # @param field str : a relational field
247
     # @param field str : a relational field
248
     # @return a nature
248
     # @return a nature
249
     @staticmethod
249
     @staticmethod
250
-    def _nature_from_relational_field(field):
250
+    def _prepare_relational_field(field):
251
         spl = field.split('.')
251
         spl = field.split('.')
252
         if len(spl) != 2:
252
         if len(spl) != 2:
253
             raise LeObjectQueryError("The relationalfield '%s' is not valid"%field)
253
             raise LeObjectQueryError("The relationalfield '%s' is not valid"%field)
254
         nature = spl[-1]
254
         nature = spl[-1]
255
         if nature not in EditorialModel.classtypes.EmNature.getall():
255
         if nature not in EditorialModel.classtypes.EmNature.getall():
256
             raise LeObjectQueryError("'%s' is not a valid nature in the field %s"%(nature, field))
256
             raise LeObjectQueryError("'%s' is not a valid nature in the field %s"%(nature, field))
257
-        return nature
257
+        
258
+        if spl[0] == 'superior':
259
+            return (REL_SUP, nature)
260
+        elif spl[0] == 'subordinate':
261
+            return (REL_SUB, nature)
262
+        else:
263
+            raise LeObjectQueryError("Invalid preffix for relationnal field : '%s'"%spl[0])
258
 
264
 
259
     ## @brief Check and split a query filter
265
     ## @brief Check and split a query filter
260
     # @note The query_filter format is "FIELD OPERATOR VALUE"
266
     # @note The query_filter format is "FIELD OPERATOR VALUE"
284
         for operator in cls._query_operators[1:]:
290
         for operator in cls._query_operators[1:]:
285
             op_re_piece += '|(%s)'%operator.replace(' ', '\s')
291
             op_re_piece += '|(%s)'%operator.replace(' ', '\s')
286
         op_re_piece += ')'
292
         op_re_piece += ')'
287
-        cls._query_re = re.compile('^\s*(?P<field>(superior\.)?[a-z_][a-z0-9\-_]*)\s*'+op_re_piece+'\s*(?P<value>[^<>=!].*)\s*$', flags=re.IGNORECASE)
293
+        cls._query_re = re.compile('^\s*(?P<field>(((superior)|(subordinate))\.)?[a-z_][a-z0-9\-_]*)\s*'+op_re_piece+'\s*(?P<value>[^<>=!].*)\s*$', flags=re.IGNORECASE)
288
         pass
294
         pass
289
 
295
 
290
 class LeObjectError(Exception):
296
 class LeObjectError(Exception):

+ 3
- 3
leobject/test/test_leobject.py View File

159
 
159
 
160
         filt, rfilt = LeObject._prepare_filters(filters, Numero, None)
160
         filt, rfilt = LeObject._prepare_filters(filters, Numero, None)
161
         self.assertEqual(filt, [('lodel_id', '=', '1')])
161
         self.assertEqual(filt, [('lodel_id', '=', '1')])
162
-        self.assertEqual(rfilt, [('parent', '>', '2')])
162
+        self.assertEqual(rfilt, [((leobject.leobject.REL_SUP,'parent'), '>', '2')])
163
         
163
         
164
         #All fields, no relationnal and class given
164
         #All fields, no relationnal and class given
165
         filters = []
165
         filters = []
175
         #Mixed type filters (tuple and string)
175
         #Mixed type filters (tuple and string)
176
         filters = [
176
         filters = [
177
             ('lodel_id', '<=', '0'),
177
             ('lodel_id', '<=', '0'),
178
-            'superior.parent = 2',
178
+            'subordinate.parent = 2',
179
         ]
179
         ]
180
         
180
         
181
         filt, rfilt = LeObject._prepare_filters(filters, Numero, None)
181
         filt, rfilt = LeObject._prepare_filters(filters, Numero, None)
182
         self.assertEqual(filt, [('lodel_id', '<=', '0')])
182
         self.assertEqual(filt, [('lodel_id', '<=', '0')])
183
-        self.assertEqual(rfilt, [('parent', '=', '2')])
183
+        self.assertEqual(rfilt, [((leobject.leobject.REL_SUB,'parent'), '=', '2')])
184
 
184
 
185
     def test_prepare_filters_invalid(self):
185
     def test_prepare_filters_invalid(self):
186
         """ Testing the _prepare_filters() method """
186
         """ Testing the _prepare_filters() method """

Loading…
Cancel
Save