Browse Source

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 years ago
parent
commit
c9f2f16c77
1 changed files with 29 additions and 3 deletions
  1. 29
    3
      EditorialModel/model.py

+ 29
- 3
EditorialModel/model.py View File

100
     ## Return a new uid
100
     ## Return a new uid
101
     # @return a new uid
101
     # @return a new uid
102
     def new_uid(self):
102
     def new_uid(self):
103
-        used_uid = self._components.keys()
103
+        used_uid = [ int(uid) for uid in self._components['uids'].keys()]
104
         return sorted(used_uid)[-1] + 1 if len(used_uid) > 0 else 1
104
         return sorted(used_uid)[-1] + 1 if len(used_uid) > 0 else 1
105
 
105
 
106
     ## Create a component from a component type and datas
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
     # @param component_type str : a component type ( component_class, component_fieldgroup, component_field or component_type )
110
     # @param component_type str : a component type ( component_class, component_fieldgroup, component_field or component_type )
109
     # @param datas dict : the options needed by the component creation
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
     def create_component(self, component_type, datas):
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
         self._components['uids'][em_component.uid] = em_component
120
         self._components['uids'][em_component.uid] = em_component
115
         self._components[self.name_from_emclass(em_component.__class__)].append(em_component)
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
         return em_component
142
         return em_component
117
 
143
 
118
     ## Delete a component
144
     ## Delete a component

Loading…
Cancel
Save