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,9 +1,10 @@
1 1
 #-*- coding: utf-8 -*-
2 2
 
3
-from leobject.dyn import LeObject
3
+import leobject
4 4
 
5 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 9
     ## @brief Stores fieldtypes by field name
9 10
     _fieldtypes = dict()
@@ -14,9 +15,7 @@ class LeClass(LeObject):
14 15
     # @param model Model : The editorial model
15 16
     # @param datasource ? : The datasource
16 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 20
     ## @brief Get the linked objects
22 21
     # @return an array of LeType derivated class instance

+ 11
- 6
leobject/leobject.py View File

@@ -5,12 +5,17 @@
5 5
 
6 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 20
     ## @brief create a new LeObject
16 21
     # @param data dict: a dictionnary of field:value to save

+ 25
- 10
leobject/letype.py View File

@@ -3,7 +3,8 @@
3 3
 from leobject.leclass import LeClass
4 4
 
5 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 9
     ## @brief Stores selected fields with key = name
9 10
     _fields = list()
@@ -11,20 +12,34 @@ class LeType(LeClass):
11 12
     _leclass = None
12 13
     
13 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 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 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 30
     # @return The lodel id of the new LeType or False
24 31
     # @thorw A leo exception if invalid stuff
25 32
     # @throw InvalidArgumentError if invalid argument
26 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 43
         pass
29 44
     
30 45
     ## @brief Delete a LeType from the datasource
@@ -52,8 +67,8 @@ class LeType(LeClass):
52 67
     @classmethod
53 68
     def check_datas(cls, datas):
54 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 72
             cls._fields[dname].check_or_raise(dval)
58 73
                 
59 74
 

Loading…
Cancel
Save