Browse Source

EmType: implement add_superior() & del_superior()

ArnAud 9 years ago
parent
commit
49b78fe37d
2 changed files with 31 additions and 29 deletions
  1. 2
    0
      EditorialModel/model.py
  2. 29
    29
      EditorialModel/types.py

+ 2
- 0
EditorialModel/model.py View File

221
             uid, fields = fields_list
221
             uid, fields = fields_list
222
             for field_id in fields:
222
             for field_id in fields:
223
                 new_me.component(uid).select_field(new_me.component(field_id))
223
                 new_me.component(uid).select_field(new_me.component(field_id))
224
+
224
         # add superiors to types
225
         # add superiors to types
226
+        # TODO: debug, this add a superior to all types !
225
         for superiors_list in relations['superiors_list']:
227
         for superiors_list in relations['superiors_list']:
226
             uid, sup_list = superiors_list
228
             uid, sup_list = superiors_list
227
             for nature, superior_uid in sup_list.items():
229
             for nature, superior_uid in sup_list.items():

+ 29
- 29
EditorialModel/types.py View File

220
     # @throw ValueError when relation_nature isn't reconized or not allowed for this type
220
     # @throw ValueError when relation_nature isn't reconized or not allowed for this type
221
     # @throw ValueError when relation_nature don't allow to link this types together
221
     # @throw ValueError when relation_nature don't allow to link this types together
222
     def add_superior(self, em_type, relation_nature):
222
     def add_superior(self, em_type, relation_nature):
223
-        if not isinstance(em_type, EmType) or not isinstance(relation_nature, str):
224
-            raise TypeError("Excepted <class EmType> and <class str> as em_type argument. But got : " + str(type(em_type)) + " " + str(type(relation_nature)))
225
-        if relation_nature not in EmClassType.natures(self.classtype['name']):
226
-            raise ValueError("Invalid nature for add_superior : '" + relation_nature + "'. Allowed relations for this type are " + str(EmClassType.natures(self.classtype['name'])))
227
-
228
         #Checking that this relation is allowed by the nature of the relation
223
         #Checking that this relation is allowed by the nature of the relation
229
         att = self.classtype['hierarchy'][relation_nature]['attach']
224
         att = self.classtype['hierarchy'][relation_nature]['attach']
230
         if att == 'classtype':
225
         if att == 'classtype':
233
         elif self.name != em_type.name:
228
         elif self.name != em_type.name:
234
             raise ValueError("Not allowed to put a different em_type as superior in a relation of nature '" + relation_nature + "'")
229
             raise ValueError("Not allowed to put a different em_type as superior in a relation of nature '" + relation_nature + "'")
235
 
230
 
236
-        # TODO Réimplémenter
237
-        # conn = self.db_engine.connect()
238
-        # htable = self._table_hierarchy
239
-        # values = {'subordinate_id': self.uid, 'superior_id': em_type.uid, 'nature': relation_nature}
240
-        # req = htable.insert(values=values)
241
-        #
242
-        # try:
243
-        #     conn.execute(req)
244
-        # except sql.exc.IntegrityError:
245
-        #     ret = False
246
-        # else:
247
-        #     ret = True
248
-        # finally:
249
-        #     conn.close()
250
-        #
251
-        # return ret
231
+        self._change_superiors_list(em_type, relation_nature, True)
252
 
232
 
253
     ## Delete a superior in the type hierarchy
233
     ## Delete a superior in the type hierarchy
254
     # @param em_type EmType: An EmType instance
234
     # @param em_type EmType: An EmType instance
235
+    # @param relation_nature str: The name of the relation's nature
255
     # @throw TypeError when em_type isn't an EmType instance
236
     # @throw TypeError when em_type isn't an EmType instance
237
+    # @throw ValueError when relation_nature isn't reconized or not allowed for this type
256
     def del_superior(self, em_type, relation_nature):
238
     def del_superior(self, em_type, relation_nature):
257
-        if not isinstance(em_type, EmType):
258
-            raise TypeError("Excepted <class EmType> as argument. But got : " + str(type(em_type)))
239
+        self._change_superiors_list(em_type, relation_nature, False)
240
+
241
+    ## Apply changes to the superiors_list
242
+    # @param em_type EmType: An EmType instance
243
+    # @param relation_nature str: The name of the relation's nature
244
+    # @param add bool: Add or delete relation
245
+    def _change_superiors_list(self, em_type, relation_nature, add=True):
246
+        # check instance of parameters
247
+        if not isinstance(em_type, EmType) or not isinstance(relation_nature, str):
248
+            raise TypeError("Excepted <class EmType> and <class str> as em_type argument. But got : " + str(type(em_type)) + " " + str(type(relation_nature)))
249
+        # check if relation_nature is valid for this type
259
         if relation_nature not in EmClassType.natures(self.classtype['name']):
250
         if relation_nature not in EmClassType.natures(self.classtype['name']):
260
             raise ValueError("Invalid nature for add_superior : '" + relation_nature + "'. Allowed relations for this type are " + str(EmClassType.natures(self.classtype['name'])))
251
             raise ValueError("Invalid nature for add_superior : '" + relation_nature + "'. Allowed relations for this type are " + str(EmClassType.natures(self.classtype['name'])))
261
 
252
 
262
-        # TODO Réimplémenter
263
-        # conn = self.db_engine.connect()
264
-        # htable = self._table_hierarchy
265
-        # req = htable.delete(htable.c.superior_id == em_type.uid and htable.c.nature == relation_nature)
266
-        # conn.execute(req)
267
-        # conn.close()
253
+        try:
254
+            if add:
255
+                self.superiors_list[relation_nature] = em_type.uid
256
+                self.model.migration_handler.register_change(self.model, self.uid, None, {'superiors_list': {relation_nature: em_type.uid}})
257
+            else:
258
+                del self.superiors_list[relation_nature]
259
+                self.model.migration_handler.register_change(self.model, self.uid, {'superiors_list': {relation_nature: em_type.uid}}, None)
260
+        except MigrationHandlerChangeError as exception_object:
261
+            if add:
262
+                del self.superiors_list[relation_nature]
263
+            else:
264
+                self.superiors_list[relation_nature] = em_type.uid
265
+            raise exception_object
266
+
267
+        self.model.migration_handler.register_model_state(self.model, hash(self.model))
268
 
268
 
269
     ## Checks if the EmType is valid
269
     ## Checks if the EmType is valid
270
     # @throw EmComponentCheckError if check fails
270
     # @throw EmComponentCheckError if check fails

Loading…
Cancel
Save