瀏覽代碼

New way to instanciate LeCrud child classes

Note : This commit is not complete. It brakes the partial instanciation of classes etc.
Yann Weber 9 年之前
父節點
當前提交
0b3f2bae1a
共有 5 個文件被更改,包括 38 次插入37 次删除
  1. 0
    6
      leapi/leclass.py
  2. 35
    2
      leapi/lecrud.py
  3. 0
    10
      leapi/leobject.py
  4. 1
    1
      leapi/lerelation.py
  5. 2
    18
      leapi/letype.py

+ 0
- 6
leapi/leclass.py 查看文件

@@ -19,12 +19,6 @@ class _LeClass(_LeObject):
19 19
     ## @brief Stores the classtype
20 20
     _classtype = None
21 21
 
22
-    ## @brief Instanciate a new LeClass
23
-    # @note Abstract method
24
-    # @param **kwargs
25
-    def __init__(self, lodel_id, **kwargs):
26
-        _LeObject.__init__(self, lodel_id, **kwargs)
27
-
28 22
     @classmethod
29 23
     def fieldtypes(cls):
30 24
         ret = dict()

+ 35
- 2
leapi/lecrud.py 查看文件

@@ -55,8 +55,41 @@ class _LeCrud(object):
55 55
     ## @brief Stores Query filters operators
56 56
     _query_operators = ['=', '<=', '>=', '!=', '<', '>', ' in ', ' not in ', ' like ', ' not like ']
57 57
 
58
-    def __init__(self):
59
-        raise NotImplementedError("Abstract class")
58
+    
59
+    ## @brief Asbtract constructor for every child classes
60
+    # @param uid int : lodel_id if LeObject, id_relation if its a LeRelation
61
+    # @param **kwargs : datas !
62
+    # @raise NotImplementedError if trying to instanciate a class that cannot be instanciated
63
+    def __init__(self, uid, **kwargs):
64
+        if not self.implements_leobject() and not self.implements_lerelation():
65
+            raise NotImplementedError("Abstract class !")
66
+        # Try to get the name of the uid field (lodel_id for objects, id_relation for relations)
67
+        try:
68
+            uid_name = self.uidname()
69
+        except NotImplementedError: #Should never append
70
+            raise NotImplementedError("Abstract class !")
71
+        
72
+        # Checking uid value
73
+        uid, err = self._uid_fieldtype[uid_name].check_data_value(uid)
74
+        if isinstance(err, Exception):
75
+            raise err
76
+        setattr(self, uid_name, uid)
77
+        
78
+        # Populating the object with given datas
79
+        errors = dict()
80
+        for name, value in kwargs.items():
81
+            if name not in self.fieldlist():
82
+                errors[name] = AttributeError("No such field '%s' for %s"%(name, self.__class__.__name__))
83
+            else:
84
+                cvalue, err = self.fieldtypes()[name].check_data_value(value)
85
+                if isinstance(err, Exception):
86
+                    errors[name] = err
87
+                else:
88
+                    setattr(self, name, cvalue)
89
+        if len(errors) > 0:
90
+            raise LeApiDataCheckError("Invalid arguments given to constructor", errors)
91
+
92
+
60 93
  
61 94
     ## @brief Given a dynamically generated class name return the corresponding python Class
62 95
     # @param name str : a concrete class name

+ 0
- 10
leapi/leobject.py 查看文件

@@ -33,16 +33,6 @@ class _LeObject(_LeCrud):
33 33
     ## @brief Stores the names of the fields storing the EM class uid and EM type uid
34 34
     _me_uid_field_names = (None, None)
35 35
     
36
-    ## @brief Instanciate a partial LeObject with a lodel_id
37
-    # @note use the get_instance method to fetch datas and instanciate a concret LeObject
38
-    def __init__(self, lodel_id):
39
-        #Warning ! Handles only single pk
40
-        uid_fname, uid_ft = list(self._uid_fieldtype.items())[0]
41
-        new_id, err = uid_ft.check_data_value(lodel_id)
42
-        if not (err is None):
43
-            raise err
44
-        setattr(self, uid_fname, lodel_id)
45
-    
46 36
     ## @return Corresponding populated LeObject
47 37
     def get_instance(self):
48 38
         uid_fname = self.uidname()

+ 1
- 1
leapi/lerelation.py 查看文件

@@ -17,7 +17,7 @@ class _LeRelation(lecrud._LeCrud):
17 17
     _rel_fieldtypes = dict()
18 18
 
19 19
     def __init__(self, id_relation, **kwargs):
20
-        self.id_relation = id_relation
20
+        super().__init__(id_relation, **kwargs)
21 21
     
22 22
     ## @brief Forge a filter to match the superior
23 23
     @classmethod

+ 2
- 18
leapi/letype.py 查看文件

@@ -35,30 +35,14 @@ class _LeType(_LeClass):
35 35
         if self._leclass is None:
36 36
             raise NotImplementedError("Abstract class")
37 37
 
38
-        self.lodel_id, err = self._uid_fieldtype['lodel_id'].check_data_value(lodel_id)
39
-        if isinstance(err, Exception):
40
-            raise err
41
-
42 38
         if 'type_id' in kwargs:
43 39
             if self.__class__._type_id != int(kwargs['type_id']):
44 40
                 raise RuntimeError("Trying to instanciate a %s with an type_id that is not correct"%self.__class__.__name__)
45 41
         if 'class_id' in kwargs:
46 42
             if self.__class__._class_id != int(kwargs['class_id']):
47 43
                 raise RuntimeError("Trying to instanciate a %s with a clas_id that is not correct"%self.__class__.__name__)
48
-
49
-        ## Populate the object from the datas received in kwargs
50
-        err_l = dict()
51
-        for name, value in kwargs.items():
52
-            if name not in self._fields:
53
-                err_l[name] = AttributeError("No such field '%s' for %s"%(name, self.__class__.__name__))
54
-            else:
55
-                cvalue, err =  self.fieldtypes()[name].check_data_value(value)
56
-                if isinstance(err, Exception):
57
-                    err_l[name] = err
58
-                else:
59
-                    setattr(self, name, value)
60
-        if len(err_l) > 0:
61
-            raise LeApiDataCheckError("Invalid arguments given to constructor", err_l)
44
+        
45
+        super().__init__(lodel_id, **kwargs)
62 46
 
63 47
     @classmethod
64 48
     def leo_class(cls):

Loading…
取消
儲存