Selaa lähdekoodia

Started some method implementation form _LeObject and LeType

Methods are _LeObject.get() and LeType.check_datas()
Yann Weber 9 vuotta sitten
vanhempi
commit
482378fb18
2 muutettua tiedostoa jossa 81 lisäystä ja 19 poistoa
  1. 46
    7
      leobject/leobject.py
  2. 35
    12
      leobject/letype.py

+ 46
- 7
leobject/leobject.py Näytä tiedosto

@@ -81,13 +81,46 @@ class _LeObject(object):
81 81
             else:
82 82
                 filters.append(self._split_filter(query))
83 83
         #Now filters is a list of tuple (FIELD, OPERATOR, VALUE
84
-
85
-        try:
86
-            responses = self.datasource.get(datasource_filters)
87
-        except:
88
-            raise
89
-
90
-        return responses
84
+        #Begining to check the filters
85
+        
86
+        #Fetching EmType
87
+        if typename is None:
88
+            emtype = None
89
+        else:
90
+            emtype = self._model.component_from_name(typename, 'EmType')
91
+            if not emtype:
92
+                raise LeObjectQueryError("No such EmType : '%s'"%typename)
93
+        #Fetching EmClass
94
+        if classname is None:
95
+            emclass = None
96
+        else:
97
+            emclass = self._model.component_from_name(classname, 'EmClass')
98
+            if not emclass:
99
+                raise LeObjectQueryError("No such EmClass : '%s'"%classname)
100
+
101
+        #Checking that fields in the query_filters are correct
102
+        if emtype is None and emclass is None:
103
+            #Only fields from the object table are allowed
104
+            for field,_,_ in filters:
105
+                if field not in EditorialModel.classtype.common_fields:
106
+                    raise LeObjectQueryError("Not typename and no classname given, but the field %s is not in the common_fields list"%field)
107
+        else:
108
+            if emtype is None:
109
+                field_l = emclass.fields()
110
+            else:
111
+                if not (emclass is None):
112
+                    if emtype.em_class != emclass:
113
+                        raise LeObjectQueryError("The EmType %s is not a specialisation of the EmClass %s"%(typename, classname))
114
+                else:
115
+                    #Set emclass (to query the db ?
116
+                    emclass = emtype.em_class
117
+                field_l = emtype.fields()
118
+            #Checks that fields are in this type
119
+            for field,_,_ in filters:
120
+                if field not in [ f.name for f in fields_l ]:
121
+                    raise LeObjectQueryError("No field named '%s' in '%s'"%(field, typename))
122
+
123
+        return self._datasource.get(emclass, emtype, filters)
91 124
 
92 125
     ## @brief check if data dict fits with the model
93 126
     # @param data dict: dictionnary of field:value to check
@@ -126,3 +159,9 @@ class _LeObject(object):
126 159
         op_re_piece += ')'
127 160
         cls._query_re = re.compile('^\s*(?P<field>[a-z_][a-z0-9\-_]*)\s*'+op_re_piece+'\s*(?P<value>[^<>=!].*)\s*$', flags=re.IGNORECASE)
128 161
         pass
162
+
163
+class LeObjectError(Exception):
164
+    pass
165
+
166
+class LeObjectQueryError(LeObjectError):
167
+    pass

+ 35
- 12
leobject/letype.py Näytä tiedosto

@@ -1,6 +1,8 @@
1 1
 #-*- coding: utf-8 -*-
2 2
 
3
+from Lodel.utils.classinstancemethod import classinstancemethod
3 4
 from leobject.leclass import LeClass
5
+from leobject.leobject import LeObjectError
4 6
 
5 7
 ## @brief Represent an EmType data instance
6 8
 # @note Is not a derivated class of LeClass because the concrete class will be a derivated class from LeClass
@@ -25,6 +27,14 @@ class LeType(object):
25 27
                 raise AttributeError("No such field '%s' for %s"%(name, self.__class__.__name__)
26 28
             setattr(self, name, value)
27 29
     
30
+    @classinstancemethod
31
+    def delete(self):
32
+        pass
33
+    
34
+    @delete.classmethod
35
+    def delete(cls, uid):
36
+        pass
37
+
28 38
     ## @brief Insert a new LeType in the datasource
29 39
     # @param **datas : A dict containing the datas
30 40
     # @return The lodel id of the new LeType or False
@@ -32,13 +42,6 @@ class LeType(object):
32 42
     # @throw InvalidArgumentError if invalid argument
33 43
     @classmethod
34 44
     def insert(cls, **datas):
35
-        #check datas
36
-        autom_fields = ['lodel_id', 'typename', 'classname' ]
37
-        for forbiden in autom_fields:
38
-            if forbiden in datas:
39
-                raise AttributeError("Not allowed to give explicitly '%s' to insert method"%(forbiden))
40
-        self.check_datas(datas)
41
-
42 45
         super(LeType, self).insert(typename=self.__class__.__name__, classname=self._leclass.__name__, **datas)
43 46
         pass
44 47
     
@@ -63,14 +66,34 @@ class LeType(object):
63 66
     
64 67
     ## @brief Check that datas are valid for this type
65 68
     # @param datas dict : key == field name value are field values
66
-    # @throw If the datas are not valids
69
+    # @param complete bool : if True expect that datas provide values for all non internal fields
70
+    # @throw TypeError If the datas are not valids
71
+    # @throw AttributeError if datas provides values for automatic fields
72
+    # @throw AttributeError if datas provides values for fields that doesn't exists
67 73
     @classmethod
68
-    def check_datas(cls, datas):
74
+    def check_datas_or_raise(cls, datas, complete = False):
75
+        autom_fields = [f.name for f in cls._fieldtypes if f.internal]
69 76
         for dname, dval in datas.items():
77
+            if dname in autom_fields:
78
+                raise AttributeError("The field '%s' is internal"%(dname)
70 79
             if dname not in cls._fields:
71 80
                 raise AttributeError("No such field '%s' for %s"%(dname, self.__class__.__name__)
72
-            cls._fields[dname].check_or_raise(dval)
73
-                
81
+            cls._fieldtypess[dname].check_or_raise(dval)
82
+        
83
+        fields = [f.name for f in cls._fieldtypes if not f.internal]
84
+        if complete and len(datas) < len(fields):
85
+            raise LeObjectError("The argument complete was True but some fields are missing : %s"%(set(fields) - set(datas.keys())))
86
+    
87
+    ## @brief Check that datas are valid for this type
88
+    # @param datas dict : key == field name value are field values
89
+    # @param complete bool : if True expect that datas provide values for all non internal fields
90
+    # @return True if check pass else False
91
+    def check_datas(cls, datas, complete = False):
92
+        try:
93
+            cls.check_datas_or_raise(datas, complete)
94
+        except (TypeError, AttributeError, LeObjectError):
95
+            return False
96
+        return True
74 97
 
75 98
     ## @brief Implements the automatic checks of attributes
76 99
     # @note Run data check from fieldtypes if we try to modify an field attribute of the LeType
@@ -80,7 +103,7 @@ class LeType(object):
80 103
         if name in self._fields.keys():
81 104
             self._fields[name].check_or_raise(value)
82 105
         return super(LeType, self).__setattr__(name, value)
83
-
106
+    
84 107
     ## @brief Delete the LeType
85 108
     # @return True if deleted False if not
86 109
     def delete(self):

Loading…
Peruuta
Tallenna