Bläddra i källkod

Update relations rank modifications

Yann Weber 9 år sedan
förälder
incheckning
b8266f3303
3 ändrade filer med 44 tillägg och 13 borttagningar
  1. 0
    3
      DataSource/MySQL/test/test_datasource.py
  2. 7
    1
      DataSource/dummy/leapidatasource.py
  3. 37
    9
      leapi/lerelation.py

+ 0
- 3
DataSource/MySQL/test/test_datasource.py Visa fil

@@ -89,9 +89,6 @@ class DataSourceTestCase(TestCase):
89 89
 
90 90
             with patch.object(mosql_Query, '__call__', return_value=sql_query) as mock_insert:
91 91
                 with patch.object(db_utils, 'query', return_value=cursor_mock) as mock_utils_query:
92
-                    #mock_utils_query = Mock()
93
-                    #db_utils.query = mock_utils_query
94
-
95 92
                     # call the insert() method
96 93
                     datasource.insert(letype, **insert_datas)
97 94
 

+ 7
- 1
DataSource/dummy/leapidatasource.py Visa fil

@@ -49,4 +49,10 @@ class DummyDatasource(object):
49 49
     # @return int the number of inserted component
50 50
     def insert_multi(self, target_cls, datas_list):
51 51
         pass
52
-
52
+    
53
+    ## @brief Update a rank for a relation (and shift properly every concerned relations)
54
+    # @param le_relation LeRelation : A LeRelation instance
55
+    # @param new_rank int : An integer representing the absolute new rank
56
+    # @return ???
57
+    def update_rank(self, le_relation, new_rank):
58
+        pass

+ 37
- 9
leapi/lerelation.py Visa fil

@@ -94,18 +94,46 @@ class _LeRelation(lecrud._LeCrud):
94 94
         return self.set_rank('last')
95 95
 
96 96
     ## @brief move to the given rank defined by a shift step
97
-    # @param step str|int Step of the rank shift. Can be a string containing an operator and the value (i.e. "+6"
98
-    #                                               or "-6"), or can be an integer (the operator will then be "+")
97
+    # @param step int : The step
99 98
     # @return True in case of success, False in case of failure
99
+    # @throw ValueError if step is not castable into an integer
100 100
     def shift_rank(self, step):
101
-        return self.set_rank(step)
102
-
103
-    ## @brief sets a new rank
101
+        step = int(step)
102
+        return self.set_rank(self.rank + step)
103
+    
104
+    ## @brief modify a relation rank
105
+    # @param new_rank int|str : The new rank can be an integer > 1 or strings 'first' or 'last'
104 106
     # @return True in case of success, False in case of failure
105
-    def set_rank(self, rank):
106
-        parsed_rank = self.__class__._parse_rank(rank)
107
-        return self._datasource.update_rank(self.id_relation, rank)
108
-
107
+    # @throw ValueError if step is not castable into an integer
108
+    def set_rank(self, new_rank):
109
+        max_rank = self.get_max_rank()
110
+        try:
111
+            new_rank = int(new_rank)
112
+        except ValueError:
113
+            if new_rank == 'first':
114
+                new_rank = 1
115
+            elif new_rank == 'last':
116
+                new_rank = max_rank
117
+            else:
118
+                raise ValueError("The new rank can be an integer > 1 or strings 'first' or 'last', but %s given"%new_rank)
119
+
120
+        if self.rank == new_rank:
121
+            return True
122
+
123
+        if new_rank < 1:
124
+            if strict:
125
+                raise ValueError("Rank must be >= 1, but %d given"%rank)
126
+            new_rank = 1
127
+        elif new_rank > max_rank:
128
+            if strict:
129
+                raise ValueError("Rank is too big (max_rank = %d), but %d given"%(max_rank,rank))
130
+            new_rank = max_rank
131
+        self._datasource.update_rank(self, new_rank)
132
+    
133
+    ## @returns The maximum assignable rank for this relation
134
+    # @todo implementation
135
+    def get_max_rank(self):
136
+        pass
109 137
 
110 138
     @classmethod
111 139
     ## @brief checks if a rank value is valid

Loading…
Avbryt
Spara