|
@@ -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
|