Bläddra i källkod

_LeObject.get() enhancement

Yann Weber 9 år sedan
förälder
incheckning
4d445e876e
1 ändrade filer med 53 tillägg och 33 borttagningar
  1. 53
    33
      leobject/leobject.py

+ 53
- 33
leobject/leobject.py Visa fil

@@ -71,10 +71,11 @@ class _LeObject(object):
71 71
 
72 72
     ## @brief make a search to retrieve a collection of LeObject
73 73
     # @param query_filters list : list of string of query filters (or tuple (FIELD, OPERATOR, VALUE) )
74
+    # @param field_list list|None : list of string representing fields
74 75
     # @param typename str : The name of the LeType we want
75 76
     # @param classname str : The name of the LeClass we want
76 77
     # @return responses ({string:*}): a list of dict with field:value
77
-    def get(self, query_filters, typename = None, classname = None):
78
+    def get(self, query_filters, field_list = None, typename = None, classname = None):
78 79
         filters = list()
79 80
         for query in query_filters:
80 81
             if len(query) == 3 and not isinstance(query, str):
@@ -82,42 +83,53 @@ class _LeObject(object):
82 83
             else:
83 84
                 filters.append(self._split_filter(query))
84 85
         #Now filters is a list of tuple (FIELD, OPERATOR, VALUE
85
-        #Begining to check the filters
86 86
         
87
-        #Fetching EmType
87
+        #Fetching LeType
88 88
         if typename is None:
89 89
             letype = None
90 90
         else:
91 91
             letype = leobject.lefactory.LeFactory.leobj_from_name(typename)
92 92
 
93
-        #Fetching EmClass
93
+        #Fetching LeClass
94 94
         if classname is None:
95 95
             leclass = None
96 96
         else:
97 97
             leclass = leobject.lefactory.LeFactory.leobj_from_name(classname)
98 98
             if not emclass:
99 99
                 raise LeObjectQueryError("No such EmClass : '%s'"%classname)
100
+
101
+        #Checking field_list
102
+        if field_list is None:
103
+            if not (letype is None):
104
+                flist = letype._fields
105
+            elif not (leclass is None):
106
+                flist = leclass._fieldtypes.keys()
107
+            else:
108
+                flist = EditorialModel.classtype.common_fields.keys()
109
+        else:
110
+            LeFactory._check_fields(letype, leclass, field_list)
100 111
         
101 112
         #Checking relational filters (for the moment fields like superior.NATURE)
102
-        relational_filters = list()
103
-        
104
-        for i in range(len(filters),0,-1): #reverse iteration to be able to delete relational filters from the list
105
-            field,_,_ = filters[i]
106
-            if field.startwith('superior.'):
107
-                #relationnal field
108
-                spl = field.split('.')
109
-                if len(spl) != 2:
110
-                    raise LeObjectQueryError("Not a valid relationnal field : '%s'"%(field))
111
-                nature = spl[1]
112
-                if nature not in EditorialModel.classtypes.EmNature.getall():
113
-                    raise LeObjectQueryError("'%s' is not a valid nature in the field %s"%(nature, field))
114
-                relational_filters.append(nature)
113
+        relational_filters = [ LeFactory._nature_from_relational_field(field), operator, value for field, operator, value in filters if LeFactory._field_is_relational(field)]
114
+        filters = [f for f in filters if not self._field_is_relational(f[0])]
115
+        #Checking the rest of the fields
116
+        LeFactory._check_fields(letype, leclass, [ f[0] for f in filters ])
117
+
118
+        return self._datasource.get(emclass, emtype, filters, relational_filters)
115 119
 
120
+    ## @brief Check if a fieldname is valid
121
+    # @param letype LeType|None : The concerned type (or None)
122
+    # @param leclass LeClass|None : The concerned class (or None)
123
+    # @param fields list : List of string representing fields
124
+    # @throw LeObjectQueryError if their is some problems
125
+    # @throw AttributeError if letype is not from the leclass class
126
+    @staticmethod
127
+    def _check_fields(letype, leclass, fields):
116 128
         #Checking that fields in the query_filters are correct
117 129
         if letype is None and leclass is None:
118 130
             #Only fields from the object table are allowed
119
-            for field,_,_ in filters:
120
-                if field not in EditorialModel.classtype.common_fields:
131
+            for field in fields:
132
+                if field not in EditorialModel.classtype.common_fields.keys():
121 133
                     raise LeObjectQueryError("Not typename and no classname given, but the field %s is not in the common_fields list"%field)
122 134
         else:
123 135
             if letype is None:
@@ -125,26 +137,34 @@ class _LeObject(object):
125 137
             else:
126 138
                 if not (leclass is None):
127 139
                     if letype._leclass != leclass:
128
-                        raise LeObjectQueryError("The EmType %s is not a specialisation of the EmClass %s"%(typename, classname))
129
-                else:
130
-                    #Set emclass (to query the db ?
131
-                    leclass = emtype._leclass
140
+                        raise AttributeError("The EmType %s is not a specialisation of the EmClass %s"%(typename, classname))
132 141
                 field_l = letype._fields
133 142
             #Checks that fields are in this type
134
-            for field,_,_ in filters:
143
+            for field in fields:
135 144
                 if field not in fields_l:
136 145
                     raise LeObjectQueryError("No field named '%s' in '%s'"%(field, typename))
146
+        pass
137 147
 
138
-        return self._datasource.get(emclass, emtype, filters, relational_filters)
139
-
140
-    ## @brief check if data dict fits with the model
141
-    # @param data dict: dictionnary of field:value to check
142
-    # @return checked_data ({string:*}): a list of dict with field:value
143
-    # @todo implent !
144
-    def _check_data(self, data):
145
-        checked_data = data
146
-        return checked_data
148
+    ## @brief Check if a field is relational or not
149
+    # @param field str : the field to test
150
+    # @return True if the field is relational else False
151
+    @staticmethod
152
+    def _field_is_relational(field):
153
+        return field.startwith('superior.')
147 154
     
155
+    ## @brief Check that a relational field is valid
156
+    # @param fields str : a relational field
157
+    # @return a nature
158
+    @staticmethod
159
+    def _nature_from_relational_field(field):
160
+        spl = field.split('.')
161
+        if len(spl) != 2:
162
+            raise LeObjectQueryError("The relationalfield '%s' is not valid"%field)
163
+        nature = spl[-1]
164
+        if nature not in EditorialModel.classtypes.EmNature.getall():
165
+            raise LeObjectQueryError("'%s' is not a valid nature in the field %s"%(nature, field))
166
+        return nature
167
+
148 168
     ## @brief Check and split a query filter
149 169
     # @note The query_filter format is "FIELD OPERATOR VALUE"
150 170
     # @param query_filter str : A query_filter string

Loading…
Avbryt
Spara