Browse Source

[untested, dev] Updated the code for leobject, leclass and letype

This commit is in "work in progress state" all the code is not consistent
Yann Weber 9 years ago
parent
commit
215b51a566
3 changed files with 40 additions and 21 deletions
  1. 4
    5
      leobject/leclass.py
  2. 11
    6
      leobject/leobject.py
  3. 25
    10
      leobject/letype.py

+ 4
- 5
leobject/leclass.py View File

1
 #-*- coding: utf-8 -*-
1
 #-*- coding: utf-8 -*-
2
 
2
 
3
-from leobject.dyn import LeObject
3
+import leobject
4
 
4
 
5
 ## @brief Represent an EmClass data instance
5
 ## @brief Represent an EmClass data instance
6
-class LeClass(LeObject):
6
+# @note Is not a derivated class of LeObject because the concrete class will be a derivated class from LeObect
7
+class LeClass(object):
7
     
8
     
8
     ## @brief Stores fieldtypes by field name
9
     ## @brief Stores fieldtypes by field name
9
     _fieldtypes = dict()
10
     _fieldtypes = dict()
14
     # @param model Model : The editorial model
15
     # @param model Model : The editorial model
15
     # @param datasource ? : The datasource
16
     # @param datasource ? : The datasource
16
     def __init__(self, **kwargs):
17
     def __init__(self, **kwargs):
17
-        if self._cls_field is None:
18
-            raise NotImplementedError("Abstract class")
19
-        super(LeClass, self).__init__(**kwargs)
18
+        raise NotImplementedError("Abstract class")
20
     
19
     
21
     ## @brief Get the linked objects
20
     ## @brief Get the linked objects
22
     # @return an array of LeType derivated class instance
21
     # @return an array of LeType derivated class instance

+ 11
- 6
leobject/leobject.py View File

5
 
5
 
6
 from EditorialModel.types import EmType
6
 from EditorialModel.types import EmType
7
 
7
 
8
-class LeObject(object):
9
-
10
-    ## Instantiate with a Model and a DataSource
11
-    def __init__(self, model, datasource):
12
-        self.model = model
13
-        self.datasource = datasource
8
+class _LeObject(object):
9
+    
10
+    ## @brief The editorial model
11
+    _model = None
12
+    ## @brief The datasource
13
+    _datasource = None
14
+    
15
+    ## @brief Instantiate with a Model and a DataSource
16
+    # @param **kwargs dict : datas usefull to instanciate a _LeObject
17
+    def __init__(self, **kwargs):
18
+        raise NotImplementedError("Abstract constructor")
14
 
19
 
15
     ## @brief create a new LeObject
20
     ## @brief create a new LeObject
16
     # @param data dict: a dictionnary of field:value to save
21
     # @param data dict: a dictionnary of field:value to save

+ 25
- 10
leobject/letype.py View File

3
 from leobject.leclass import LeClass
3
 from leobject.leclass import LeClass
4
 
4
 
5
 ## @brief Represent an EmType data instance
5
 ## @brief Represent an EmType data instance
6
-class LeType(LeClass):
6
+# @note Is not a derivated class of LeClass because the concrete class will be a derivated class from LeClass
7
+class LeType(object):
7
     
8
     
8
     ## @brief Stores selected fields with key = name
9
     ## @brief Stores selected fields with key = name
9
     _fields = list()
10
     _fields = list()
11
     _leclass = None
12
     _leclass = None
12
     
13
     
13
     ## @brief Instanciate a new LeType
14
     ## @brief Instanciate a new LeType
14
-    # @param model Model : The editorial model
15
-    # @param datasource ? : The datasource
16
-    def __init__(self, **kwargs):
17
-        if self._typename is None or self._leclass is None:
15
+    # @param lodel_id : The lodel id
16
+    # @param **kwargs : Datas used to populate a LeType
17
+    def __init__(self, lodel_id, **kwargs):
18
+        if self._leclass is None:
18
             raise NotImplementedError("Abstract class")
19
             raise NotImplementedError("Abstract class")
19
-        super(LeType, self).__init__(**kwargs)
20
+
21
+        self.lodel_id = lodel_id
22
+        ## Populate the object from the datas received in kwargs
23
+        for name, value in kwargs.items():
24
+            if name not in self._fields:
25
+                raise AttributeError("No such field '%s' for %s"%(name, self.__class__.__name__)
26
+            setattr(self, name, value)
20
     
27
     
21
     ## @brief Insert a new LeType in the datasource
28
     ## @brief Insert a new LeType in the datasource
22
-    # @param datas dict : A dict containing the datas
29
+    # @param **datas : A dict containing the datas
23
     # @return The lodel id of the new LeType or False
30
     # @return The lodel id of the new LeType or False
24
     # @thorw A leo exception if invalid stuff
31
     # @thorw A leo exception if invalid stuff
25
     # @throw InvalidArgumentError if invalid argument
32
     # @throw InvalidArgumentError if invalid argument
26
     @classmethod
33
     @classmethod
27
-    def insert(cls, datas):
34
+    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
+        super(LeType, self).insert(typename=self.__class__.__name__, classname=self._leclass.__name__, **datas)
28
         pass
43
         pass
29
     
44
     
30
     ## @brief Delete a LeType from the datasource
45
     ## @brief Delete a LeType from the datasource
52
     @classmethod
67
     @classmethod
53
     def check_datas(cls, datas):
68
     def check_datas(cls, datas):
54
         for dname, dval in datas.items():
69
         for dname, dval in datas.items():
55
-            if dname not in cls._fields.keys():
56
-                raise Exception()
70
+            if dname not in cls._fields:
71
+                raise AttributeError("No such field '%s' for %s"%(dname, self.__class__.__name__)
57
             cls._fields[dname].check_or_raise(dval)
72
             cls._fields[dname].check_or_raise(dval)
58
                 
73
                 
59
 
74
 

Loading…
Cancel
Save