|
@@ -320,6 +320,7 @@ class LeDataSourceSQL(DummyDatasource):
|
320
|
320
|
# @param letype LeType(class) : We want related LeObject of this LeType child class (not instance)
|
321
|
321
|
# @param get_sub bool : If True leo is the superior and we want subordinates, else its the opposite
|
322
|
322
|
# @return a list of dict { 'id_relation':.., 'rank':.., 'lesup':.., 'lesub'.., 'rel_attrs': dict() }
|
|
323
|
+ # TODO A conserver , utilisé par la nouvelle méthode update_rank
|
323
|
324
|
def get_related(self, leo, letype, get_sub=True):
|
324
|
325
|
if LeCrud.name2class('LeType') not in letype.__bases__:
|
325
|
326
|
raise ValueError("letype argument should be a LeType child class, but got %s" % type(letype))
|
|
@@ -397,6 +398,47 @@ class LeDataSourceSQL(DummyDatasource):
|
397
|
398
|
self._check_rank(rank)
|
398
|
399
|
self._set_relation_rank(id_relation, rank)
|
399
|
400
|
|
|
401
|
+ ## @brief Sets a new rank on a relation
|
|
402
|
+ # @param id_relation int: relation ID
|
|
403
|
+ # @param rank int|str|tuple : 'first', 'last', an integer value or a (operator, value) tuple (for the shifting)
|
|
404
|
+ # @throw leapi.leapi.LeObjectQueryError if id_relation doesn't exist
|
|
405
|
+ #
|
|
406
|
+ # TODO Conserver cette méthode dans le datasource du fait des requêtes SQL. Elle est appelée par le set_rank de LeRelation
|
|
407
|
+ def update_rank(self, id_relation, rank):
|
|
408
|
+ ret = self.get_relation(id_relation, no_attr=True)
|
|
409
|
+ if not ret:
|
|
410
|
+ raise leapi.leapi.LeObjectQueryError("No relation with id_relation = %d" % id_relation)
|
|
411
|
+ lesup = ret['lesup']
|
|
412
|
+ lesub = ret['lesub']
|
|
413
|
+ current_rank = ret['rank']
|
|
414
|
+
|
|
415
|
+ # In case we passed an (operator, value) tuple, we will recalculate the new rank
|
|
416
|
+ if isinstance(rank, tuple):
|
|
417
|
+ operator = rank[0]
|
|
418
|
+ step_value = rank[1]
|
|
419
|
+ rank = current_rank + step_value if operator == '+' else current_rank - step_value
|
|
420
|
+
|
|
421
|
+ rank = 1 if rank == 'first' or rank < 1 else rank
|
|
422
|
+ if current_rank == rank:
|
|
423
|
+ return True
|
|
424
|
+
|
|
425
|
+ relations = self.get_related(lesup, lesub.__class__, get_sub=True)
|
|
426
|
+
|
|
427
|
+ if rank == 'last' or rank > len(relations):
|
|
428
|
+ rank = len(relations)
|
|
429
|
+ if current_rank == rank:
|
|
430
|
+ return True
|
|
431
|
+
|
|
432
|
+ # insert the relation at the right position considering its new rank
|
|
433
|
+ our_relation = relations.pop(current_rank)
|
|
434
|
+ relations.insert(our_relation, rank)
|
|
435
|
+
|
|
436
|
+ # rebuild now the list of relations from the resorted list and recalculating the ranks
|
|
437
|
+ rdatas = [(attrs['relation_id'], new_rank+1) for new_rank, (sup, sub, attrs) in enumerate(relations)]
|
|
438
|
+
|
|
439
|
+ sql = insert(MySQL.relations_table_name, columns=(MySQL.relations_pkname, 'rank'), values=rdatas, on_duplicate_key_update={'rank', mosql.util.raw('VALUES(`rank`)')})
|
|
440
|
+
|
|
441
|
+
|
400
|
442
|
## @brief Set the rank of a relation identified by its ID
|
401
|
443
|
#
|
402
|
444
|
# @note this solution is not the more efficient solution but it
|
|
@@ -492,6 +534,7 @@ class LeDataSourceSQL(DummyDatasource):
|
492
|
534
|
# @return a dict{'id_relation':.., 'lesup':.., 'lesub':..,'rank':.., 'depth':.., #if not none#'nature':.., #if exists#'dict_attr':..>}
|
493
|
535
|
#
|
494
|
536
|
# @todo TESTS
|
|
537
|
+ # TODO conserver, appelé par la méthode update_rank
|
495
|
538
|
def get_relation(self, id_relation, no_attr=False):
|
496
|
539
|
relation = dict()
|
497
|
540
|
with self.connection as cur:
|