瀏覽代碼

[LeFactory] changed the way LeType and LeClass stores fieldgroups, fields and fieldtypes

Yann Weber 9 年之前
父節點
當前提交
896c061327
共有 3 個檔案被更改,包括 155 行新增21 行删除
  1. 32
    0
      leobject/leclass.py
  2. 24
    21
      leobject/lefactory.py
  3. 99
    0
      leobject/letype.py

+ 32
- 0
leobject/leclass.py 查看文件

@@ -0,0 +1,32 @@
1
+#-*- coding: utf-8 -*-
2
+
3
+from leobject.dyn import LeObject
4
+
5
+## @brief Represent an EmClass data instance
6
+class LeClass(LeObject):
7
+    
8
+    ## @brief Stores fieldtypes by field name
9
+    _fieldtypes = dict()
10
+    ## @brief Stores fieldgroups and the fields they contains
11
+    _fieldgroups = dict()
12
+
13
+    ## @brief Instanciate a new LeClass
14
+    # @param model Model : The editorial model
15
+    # @param datasource ? : The datasource
16
+    def __init__(self, **kwargs):
17
+        if self._cls_field is None:
18
+            raise NotImplementedError("Abstract class")
19
+        super(LeClass, self).__init__(**kwargs)
20
+    
21
+    ## @brief Get the linked objects
22
+    # @return an array of LeType derivated class instance
23
+    def linked(self):
24
+        pass
25
+    
26
+    ## @brief Link this class with an LeObject
27
+    # @param leo LeObject : The object to be linked with
28
+    # @return True if success False allready done
29
+    # @throw A Leo exception if the link is not allowed
30
+    def link_to(self, leo):
31
+        pass
32
+        

+ 24
- 21
leobject/lefactory.py 查看文件

@@ -11,6 +11,17 @@ class LeFactory(object):
11 11
     
12 12
     def __init__(self):raise NotImplementedError("Not designed (yet?) to be implemented")
13 13
 
14
+    ## @brief Return a call to a FieldType constructor given an EmField
15
+    # @param emfield EmField : An EmField
16
+    # @return a string representing the python code to instanciate a EmFieldType
17
+    @staticmethod
18
+    def fieldtype_construct_from_field(emfield):    
19
+        return '%s.EmFieldType(**%s)'%(
20
+            GenericFieldType.module_name(emfield.fieldtype),
21
+            emfield._fieldtype_args.__repr__(),
22
+        )
23
+            
24
+        
14 25
     ## @brief Generate python code containing the LeObject API
15 26
     # @param model_args dict : Dict of Model __init__ method arguments
16 27
     # @param datasource_args dict : Dict of datasource __init__ method arguments
@@ -46,33 +57,25 @@ class LeObject(_LeObject):\n\
46 57
         
47 58
         for emclass in model.components(EditorialModel.classes.EmClass):
48 59
             cls_fields = dict()
49
-            fieldgroups = emclass.fieldgroups()
50
-            for fieldgroup in fieldgroups:
51
-                cls_fields[fieldgroup.name] = dict()
60
+            for field in emclass.fields():
61
+                cls_fields[field.name] = LeFactory.fieldtype_construct_from_field(field)
62
+            cls_fieldgroup = dict()
63
+            for fieldgroup in emclass.fieldgroups():
64
+                cls_fieldgroup[fieldgroup.name] = list()
52 65
                 for field in fieldgroup.fields():
53
-                    fieldtype_constructor = '%s.EmFieldType(**%s)'%(
54
-                        GenericFieldType.module_name(field.fieldtype),
55
-                        field._fieldtype_args.__repr__(),
56
-                    )
57
-                    cls_fields[fieldgroup.name][field.name] = fieldtype_constructor
58
-            
66
+                    cls_fieldgroup[fieldgroup.name].append(field.name)
67
+
59 68
             result += "\n\
60 69
 class %s(LeObject, LeClass):\n\
61
-    _cls_fields = %s\n\
70
+    _fieldtypes = %s\n\
71
+    _fieldgroups = %s\n\
62 72
 \n\
63
-"%(emclass.name.title(), cls_fields.__repr__())
73
+"%(emclass.name.title(), cls_fields.__repr__(), cls_fieldgroup.__repr__())
64 74
 
65 75
         for emtype in model.components(EditorialModel.types.EmType):
66
-            type_fields = dict()
67
-            fieldgroups = emtype.fieldgroups()
68
-            for fieldgroup in fieldgroups:
69
-                type_fields[fieldgroup.name] = dict()
70
-                for field in fieldgroup.fields(emtype.uid):
71
-                    fieltype_constructor = '%s.EmFieldType(**%s)'%(
72
-                        GenericFieldType.module_name(field.fieldtype),
73
-                        field._fieldtype_args.__repr__(),
74
-                    )
75
-                    type_fields[fieldgroup.name][field.name] = fieldtype_constructor
76
+            type_fields = list()
77
+            for field in emtype.fields():
78
+                type_fields.append(field.name)
76 79
 
77 80
             result += "\n\
78 81
 class %s(%s,LeType):\n\

+ 99
- 0
leobject/letype.py 查看文件

@@ -0,0 +1,99 @@
1
+#-*- coding: utf-8 -*-
2
+
3
+from leobject.leclass import LeClass
4
+
5
+## @brief Represent an EmType data instance
6
+class LeType(LeClass):
7
+    
8
+    ## @brief Stores selected fields with key = name
9
+    _fields = list()
10
+    ## @brief Stores the class of LeClass
11
+    _leclass = None
12
+    
13
+    ## @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:
18
+            raise NotImplementedError("Abstract class")
19
+        super(LeType, self).__init__(**kwargs)
20
+    
21
+    ## @brief Insert a new LeType in the datasource
22
+    # @param datas dict : A dict containing the datas
23
+    # @return The lodel id of the new LeType or False
24
+    # @thorw A leo exception if invalid stuff
25
+    # @throw InvalidArgumentError if invalid argument
26
+    @classmethod
27
+    def insert(cls, datas):
28
+        pass
29
+    
30
+    ## @brief Delete a LeType from the datasource
31
+    # @param lodel_id int : The lodel_id identifying the LeType
32
+    # @param return True if deleted False if not existing
33
+    # @throw InvalidArgumentError if invalid parameters
34
+    # @throw Leo exception if the lodel_id identify an object from another type
35
+    @classmethod
36
+    def c_delete(cls, lodel_id):
37
+        pass
38
+    
39
+    ## @brief Update some objects in db
40
+    # @param lodel_id_l list : A list of lodel_id to update
41
+    # @param data dict : Represent the datas to update
42
+    # @return True if updated else False
43
+    # @throw InvalidArgumentError if invalid parameters
44
+    # @throw other Leo exceptions
45
+    @classmethod
46
+    def c_update(cls, lodel_id_l, datas):
47
+        pass
48
+    
49
+    ## @brief Check that datas are valid for this type
50
+    # @param datas dict : key == field name value are field values
51
+    # @throw If the datas are not valids
52
+    @classmethod
53
+    def check_datas(cls, datas):
54
+        for dname, dval in datas.items():
55
+            if dname not in cls._fields.keys():
56
+                raise Exception()
57
+            cls._fields[dname].check_or_raise(dval)
58
+                
59
+
60
+    ## @brief Implements the automatic checks of attributes
61
+    # @note Run data check from fieldtypes if we try to modify an field attribute of the LeType
62
+    # @param name str : The attribute name
63
+    # @param value * : The value
64
+    def __setattr__(self, name, value):
65
+        if name in self._fields.keys():
66
+            self._fields[name].check_or_raise(value)
67
+        return super(LeType, self).__setattr__(name, value)
68
+
69
+    ## @brief Delete the LeType
70
+    # @return True if deleted False if not
71
+    def delete(self):
72
+        return self.__class__.delete(self.lodel_id)
73
+    
74
+    ## @brief Update a LeType
75
+    # @return True if ok else False
76
+    def update(self):
77
+        return self.__class__.update(self.lodel_id, self._datas)
78
+        
79
+    ## @brief Fetch superiors
80
+    # @param nature str : The relation nature
81
+    # @return if no nature given return a dict with nature as key and arrays of LeObject as value. Else return an array of LeObject
82
+    def superiors(self, nature = None):
83
+        pass
84
+
85
+    ## @brief Fetch subordinates
86
+    # @param nature str : The relation nature
87
+    # @return if no nature given return a dict with nature as key and arrays of LeObject as value. Else return an array of LeObject
88
+    def subordinates(self, nature = None):
89
+        pass
90
+
91
+    ## @brief Add a superior
92
+    # @param nature str : The raltion nature
93
+    # @param leo LeObject : The superior
94
+    # @param return True if done False if already done
95
+    # @throw A Leo exception if trying to link with an invalid leo
96
+    # @throw InvalidArgumentError if invalid argument
97
+    def add_superior(self, nature, leo):
98
+        pass
99
+    

Loading…
取消
儲存