|
@@ -116,52 +116,18 @@ class EmComponent(object):
|
116
|
116
|
|
117
|
117
|
super(EmComponent, self).__setattr__('deleted', False)
|
118
|
118
|
|
119
|
|
- ## Insert a new component
|
120
|
|
- #
|
121
|
|
- # This function create and assign a new UID and handle the date_create and date_update values
|
122
|
|
- # @warning There is a mandatory argument dbconf that indicate wich database configuration to use
|
123
|
|
- # @param **kwargs : Names arguments representing object properties
|
124
|
|
- # @return An instance of the created component
|
125
|
|
- # @throw TypeError if an element of kwargs isn't a valid object propertie or if a mandatory argument is missing
|
126
|
|
- # @throw RuntimeError if the creation fails at database level
|
127
|
|
- # @todo Check that every mandatory _fields are given in args
|
128
|
|
- # @todo Stop using datetime.datetime.utcnow() for date_update and date_create init
|
129
|
|
- @classmethod
|
130
|
|
- def create(cls, **kwargs):
|
131
|
|
- #Checking for invalid arguments
|
132
|
|
- valid_args = [ name for name,_ in (cls._fields + EmComponent._fields)]
|
133
|
|
-
|
134
|
|
- for argname in kwargs:
|
135
|
|
- if argname in ['date_update', 'date_create', 'rank', 'uid']: # Automatic properties
|
136
|
|
- raise TypeError("Invalid argument : " + argname)
|
137
|
|
- elif argname not in valid_args:
|
138
|
|
- raise TypeError("Unexcepted keyword argument '" + argname + "' for " + cls.__name__ + " creation")
|
139
|
|
-
|
140
|
|
- #Check uniq names constraint
|
141
|
|
- try:
|
142
|
|
- name = kwargs['name']
|
143
|
|
- exist = cls(name)
|
144
|
|
- for kname in kwargs:
|
145
|
|
- if not (getattr(exist, kname) == kwargs[kname]):
|
146
|
|
- raise EmComponentExistError("An " + cls.__name__ + " named " + name + " allready exists with a different " + kname)
|
147
|
|
- logger.info("Trying to create an " + cls.__name__ + " that allready exist with same attribute. Returning the existing one")
|
148
|
|
- return exist
|
149
|
|
- except EmComponentNotExistError:
|
150
|
|
- pass
|
151
|
|
-
|
152
|
|
- kwargs['uid'] = cls.new_uid()
|
153
|
|
- kwargs['date_update'] = kwargs['date_create'] = datetime.datetime.utcnow()
|
154
|
|
-
|
155
|
|
- kwargs['rank'] = cls._get_max_rank( kwargs[cls.ranked_in], dbe )+1
|
156
|
|
-
|
157
|
|
- return cls(kwargs['name'], dbconf)
|
158
|
|
-
|
159
|
|
- ## @brief Get the maximum rank given an EmComponent child class and a ranked_in filter
|
160
|
|
- # @param ranked_in_value mixed: The rank "family"
|
161
|
|
- # @return -1 if no EmComponent found else return an integer >= 0
|
162
|
|
- @classmethod
|
163
|
|
- def _get_max_rank(cls, ranked_in_value):
|
164
|
|
- pass
|
|
119
|
+ ## Check if the EmComponent is valid
|
|
120
|
+ # This function has to check that rank are correct and continuous other checks are made in childs classes
|
|
121
|
+ # @warning Hardcoded minimum rank
|
|
122
|
+ # @warning Rank modified by _fields['rank'].value
|
|
123
|
+ # @return True
|
|
124
|
+ def check(self):
|
|
125
|
+ if self.get_max_rank() > len(self.same_rank_group()):
|
|
126
|
+ #Non continuous ranks
|
|
127
|
+ for i, component in enumerate(self.same_rank_group()):
|
|
128
|
+ component._fields['rank'].value = i + 1
|
|
129
|
+ # No need to sort again here
|
|
130
|
+ return True
|
165
|
131
|
|
166
|
132
|
## @brief Get the maximum rank given an EmComponent child class and a ranked_in filter
|
167
|
133
|
# @return A positive integer or -1 if no components
|
|
@@ -178,7 +144,12 @@ class EmComponent(object):
|
178
|
144
|
|
179
|
145
|
## Set a new rank for this component
|
180
|
146
|
# @note This function assume that ranks are properly set from 1 to x with no gap
|
|
147
|
+ #
|
|
148
|
+ # @warning Hardcoded minimum rank
|
|
149
|
+ # @warning Rank modified by _fields['rank'].value
|
|
150
|
+ #
|
181
|
151
|
# @param new_rank int: The new rank
|
|
152
|
+ #
|
182
|
153
|
# @throw TypeError If bad argument type
|
183
|
154
|
# @throw ValueError if out of bound value
|
184
|
155
|
def set_rank(self, new_rank):
|