浏览代码

Added rank handling at component creation

At creation if no rank specified the component will be place last, else you can pass an integer representing an absolute rank or two specials strings 'last' or 'first'
Yann Weber 9 年前
父节点
当前提交
c9f2f16c77
共有 1 个文件被更改,包括 29 次插入3 次删除
  1. 29
    3
      EditorialModel/model.py

+ 29
- 3
EditorialModel/model.py 查看文件

@@ -100,19 +100,45 @@ class Model(object):
100 100
     ## Return a new uid
101 101
     # @return a new uid
102 102
     def new_uid(self):
103
-        used_uid = self._components.keys()
103
+        used_uid = [ int(uid) for uid in self._components['uids'].keys()]
104 104
         return sorted(used_uid)[-1] + 1 if len(used_uid) > 0 else 1
105 105
 
106 106
     ## Create a component from a component type and datas
107 107
     #
108
+    # @note if datas does not contains a rank the new component will be added last
109
+    # @note datas['rank'] can be an integer or two specials strings 'last' or 'first'
108 110
     # @param component_type str : a component type ( component_class, component_fieldgroup, component_field or component_type )
109 111
     # @param datas dict : the options needed by the component creation
112
+    # @throw ValueError if datas['rank'] is not valid (too big or too small, not an integer nor 'last' or 'first' )
110 113
     def create_component(self, component_type, datas):
111
-        datas['uid'] = self.new_uid
112
-        em_component = self.emclass_from_name(component_type)(datas, self)
114
+        
115
+        em_obj = self.emclass_from_name(component_type)
116
+
117
+        datas['uid'] = self.new_uid()
118
+        em_component = em_obj(datas, self)
113 119
 
114 120
         self._components['uids'][em_component.uid] = em_component
115 121
         self._components[self.name_from_emclass(em_component.__class__)].append(em_component)
122
+
123
+        em_component.rank = em_component.get_max_rank()
124
+
125
+        create_fails = None
126
+
127
+        if 'rank' not in datas:
128
+            if isinstance(datas['rank'], int) or datas['rank'] == 'last':
129
+                em_component.set_rank(datas['rank'])
130
+            elif datas['rank'] == 'first':
131
+                em_component.set_rank(1)
132
+            else:
133
+                create_fails = ValueError("Invalid rank : '"+str(datas['rank'])+"'")
134
+                self.delete(em_component.uid)
135
+
136
+        if not em_component.check():
137
+            create_fails = RuntimeError("After creation the component self check fails")
138
+        
139
+        if not create_fails is None:
140
+            raise create_fails
141
+
116 142
         return em_component
117 143
 
118 144
     ## Delete a component

正在加载...
取消
保存