Bläddra i källkod

EmModel : new method migrate_handler()

 use it to re-run ME creation in a new Migration Handler
ArnAud 9 år sedan
förälder
incheckning
e5d6caa6cf
2 ändrade filer med 56 tillägg och 3 borttagningar
  1. 17
    0
      EditorialModel/backend/dummy_backend.py
  2. 39
    3
      EditorialModel/model.py

+ 17
- 0
EditorialModel/backend/dummy_backend.py Visa fil

@@ -0,0 +1,17 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+## @package EditorialModel.backend.dummy_backend
4
+# @brief Gives an empty backend
5
+#
6
+# Allows an editorial model to use an empty backend
7
+# Mostly for migration and test purpose
8
+
9
+## Manages a Json file based backend structure
10
+class EmBackendDummy(object):
11
+
12
+    def load(self):
13
+        data = {}
14
+        return data
15
+
16
+    def save(self):
17
+        return True

+ 39
- 3
EditorialModel/model.py Visa fil

@@ -4,6 +4,7 @@
4 4
 # Manage instance of an editorial model
5 5
 
6 6
 from EditorialModel.migrationhandler.dummy import DummyMigrationHandler
7
+from EditorialModel.backend.dummy_backend import EmBackendDummy
7 8
 from EditorialModel.classes import EmClass
8 9
 from EditorialModel.fieldgroups import EmFieldGroup
9 10
 from EditorialModel.fields import EmField
@@ -15,7 +16,7 @@ import hashlib
15 16
 ## Manages the Editorial Model
16 17
 class Model(object):
17 18
 
18
-    components_class = [EmClass, EmField, EmFieldGroup, EmType]
19
+    components_class = [EmClass, EmType, EmFieldGroup, EmField]
19 20
 
20 21
     ## Constructor
21 22
     #
@@ -141,10 +142,10 @@ class Model(object):
141 142
         datas['uid'] = uid if uid else self.new_uid()
142 143
         em_component = em_obj(self, **datas)
143 144
 
144
-        em_component.rank = em_component.get_max_rank() + 1 #Inserting last by default
145
+        em_component.rank = em_component.get_max_rank() + 1 #  Inserting last by default
145 146
 
146 147
         self._components['uids'][em_component.uid] = em_component
147
-        self._components[self.name_from_emclass(em_component.__class__)].append(em_component)
148
+        self._components[component_type].append(em_component)
148 149
 
149 150
         if rank != 'last':
150 151
             em_component.set_rank(1 if rank == 'first' else rank)
@@ -193,3 +194,38 @@ class Model(object):
193 194
     ## Returns a list of all the EmClass objects of the model
194 195
     def classes(self):
195 196
         return list(self._components[self.name_from_emclass(EmClass)])
197
+
198
+    ## Use a new migration handler, re-apply all the ME to this handler
199
+    #
200
+    # @param new_mh MigrationHandler: A migration_handler object
201
+    # @warning : if a relational-attribute field (with 'rel_field_id') comes before it's relational field (with 'rel_to_type_id'), this will blow up
202
+    def migrate_handler(self, new_mh):
203
+        new_me = Model(EmBackendDummy(), new_mh)
204
+        relations = {'fields_list': [], 'subordinates_list': []}
205
+
206
+        # re-create component one by one, in components_class[] order
207
+        for cls in self.components_class:
208
+            for component in self.components(cls):
209
+                component_type = self.name_from_emclass(cls)
210
+                component_dump = component.attr_dump
211
+                del component_dump['model'], component_dump['_inited']
212
+                # Save relations between component to apply them later
213
+                for relation in relations.keys():
214
+                    if relation in component_dump and component_dump[relation]:
215
+                        relations[relation].append((component.uid, component_dump[relation]))
216
+                        del component_dump[relation]
217
+                new_me.create_component(component_type, component_dump, component.uid)
218
+
219
+        # apply selected field  to types
220
+        for fields_list in relations['fields_list']:
221
+            uid, fields = fields_list
222
+            for field_id in fields:
223
+                new_me.component(uid).select_field(new_me.component(field_id))
224
+        # add superiors to types
225
+        for subordinates_list in relations['subordinates_list']:
226
+            uid, sub_list = subordinates_list
227
+            for nature, subordinates in sub_list.items():
228
+                for subordinate_id in subordinates:
229
+                    new_me.component(subordinate_id).add_superior(new_me.component(uid), nature)
230
+
231
+        self.migration_handler = new_mh

Loading…
Avbryt
Spara