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,7 +228,7 @@ class _LeObject(object):
228 228
                 filters.append(_LeObject._split_filter(fil))
229 229
 
230 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 232
         filters = [f for f in filters if not _LeObject._field_is_relational(f[0])]
233 233
         #Checking the rest of the fields
234 234
         _LeObject._check_fields(letype, leclass, [ f[0] for f in filters ])
@@ -241,20 +241,26 @@ class _LeObject(object):
241 241
     # @return True if the field is relational else False
242 242
     @staticmethod
243 243
     def _field_is_relational(field):
244
-        return field.startswith('superior.')
244
+        return field.startswith('superior.') or field.startswith('subordinate')
245 245
     
246 246
     ## @brief Check that a relational field is valid
247 247
     # @param field str : a relational field
248 248
     # @return a nature
249 249
     @staticmethod
250
-    def _nature_from_relational_field(field):
250
+    def _prepare_relational_field(field):
251 251
         spl = field.split('.')
252 252
         if len(spl) != 2:
253 253
             raise LeObjectQueryError("The relationalfield '%s' is not valid"%field)
254 254
         nature = spl[-1]
255 255
         if nature not in EditorialModel.classtypes.EmNature.getall():
256 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 265
     ## @brief Check and split a query filter
260 266
     # @note The query_filter format is "FIELD OPERATOR VALUE"
@@ -284,7 +290,7 @@ class _LeObject(object):
284 290
         for operator in cls._query_operators[1:]:
285 291
             op_re_piece += '|(%s)'%operator.replace(' ', '\s')
286 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 294
         pass
289 295
 
290 296
 class LeObjectError(Exception):

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

@@ -159,7 +159,7 @@ class LeObjectTestCase(TestCase):
159 159
 
160 160
         filt, rfilt = LeObject._prepare_filters(filters, Numero, None)
161 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 164
         #All fields, no relationnal and class given
165 165
         filters = []
@@ -175,12 +175,12 @@ class LeObjectTestCase(TestCase):
175 175
         #Mixed type filters (tuple and string)
176 176
         filters = [
177 177
             ('lodel_id', '<=', '0'),
178
-            'superior.parent = 2',
178
+            'subordinate.parent = 2',
179 179
         ]
180 180
         
181 181
         filt, rfilt = LeObject._prepare_filters(filters, Numero, None)
182 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 185
     def test_prepare_filters_invalid(self):
186 186
         """ Testing the _prepare_filters() method """

Loading…
Cancel
Save