|
@@ -122,54 +122,100 @@ class EmType(EmComponent):
|
122
|
122
|
|
123
|
123
|
## Return the EmClassType of the type
|
124
|
124
|
# @return EditorialModel.classtypes.*
|
125
|
|
- def _getClassType(self):
|
|
125
|
+ def classType(self):
|
126
|
126
|
return getattr(EmClassType, EmClass(self.class_id).class_type)
|
127
|
127
|
|
128
|
128
|
## @brief Get the list of subordinates EmType
|
129
|
129
|
# Get a list of EmType instance that have this EmType for superior
|
130
|
|
- #
|
131
|
130
|
# @return Return a dict with relation nature as keys and values as a list of subordinates
|
132
|
131
|
# EmType instance
|
133
|
|
- #
|
134
|
132
|
# @throw RuntimeError if a nature fetched from db is not valid
|
135
|
133
|
def subordinates(self):
|
|
134
|
+ return self._subOrSup(False)
|
136
|
135
|
|
|
136
|
+ ## @brief Get the list of subordinates EmType
|
|
137
|
+ # Get a list of EmType instance that have this EmType for superior
|
|
138
|
+ # @return Return a dict with relation nature as keys and values as a list of subordinates
|
|
139
|
+ # EmType instance
|
|
140
|
+ # @throw RuntimeError if a nature fetched from db is not valid
|
|
141
|
+ # @see EmType::_subOrSup()
|
|
142
|
+ def superiors(self):
|
|
143
|
+ return self._subOrSup(True)
|
|
144
|
+
|
|
145
|
+ ## @brief Return the list of subordinates or superiors for an EmType
|
|
146
|
+ # This is the logic function that implements EmType::subordinates() and EmType::superiors()
|
|
147
|
+ # @param sup bool: If True returns superiors, if False returns..... subordinates
|
|
148
|
+ # @return A dict with relation nature as keys and list of subordinates/superiors as values
|
|
149
|
+ # @throw RunTimeError if a nature fetched from db is not valid
|
|
150
|
+ # @see EmType::subordinates(), EmType::superiors()
|
|
151
|
+ def _subOrSup(self, sup = True):
|
137
|
152
|
conn = self.getDbE().connect()
|
138
|
153
|
htable = self.__class__._tableHierarchy()
|
139
|
|
- req = htable.select(htable.c.subordinate_id, htable.c.nature).where(htable.c.superior_id == self.uid)
|
|
154
|
+ req = htable.select(htable.c.subordinate_id, htable.c.nature)
|
|
155
|
+ if sup:
|
|
156
|
+ req = req.where(htable.c.subordinate_id == self.uid)
|
|
157
|
+ else:
|
|
158
|
+ req = req.where(htable.c.superior_id == self.uid)
|
140
|
159
|
res = conn.execute(req)
|
141
|
160
|
rows = res.fetchall()
|
142
|
161
|
conn.close()
|
143
|
162
|
|
144
|
163
|
result = dict()
|
145
|
|
- for nname in EmNature.__dict__():
|
146
|
|
- result[getattr(EmNature, nname)] = []
|
147
|
|
-
|
|
164
|
+ for nature in EmClasstype.natures(self.classType):
|
|
165
|
+ result[nname] = []
|
|
166
|
+
|
148
|
167
|
for row in rows:
|
149
|
168
|
if row['nature'] not in result:
|
150
|
169
|
#Maybe security issue ?
|
151
|
|
- logger.error("Unreconized nature in Database for EmType<"+str(self.uid)+"> subordinate <"+str(row['subordinate_id'])+"> : '"+row['nature']+"'")
|
|
170
|
+ logger.error("Unreconized or unauthorized nature in Database for EmType<"+str(self.uid)+"> subordinate <"+str(row['subordinate_id'])+"> : '"+row['nature']+"'")
|
152
|
171
|
raise RuntimeError("Unreconized nature from database : "+row['nature'])
|
153
|
172
|
|
154
|
173
|
result[row['nature']].append( EmType(row['subordinate_id']) )
|
155
|
|
-
|
156
|
174
|
return result
|
157
|
175
|
|
|
176
|
+
|
158
|
177
|
## Add a superior in the type hierarchy
|
159
|
178
|
# @param em_type EmType: An EmType instance
|
160
|
179
|
# @param relation_nature str: The name of the relation's nature
|
|
180
|
+ # @return False if no modification made, True if modifications success
|
|
181
|
+ #
|
161
|
182
|
# @throw TypeError when em_type not an EmType instance
|
162
|
183
|
# @throw ValueError when relation_nature isn't reconized or not allowed for this type
|
163
|
|
- # @todo define return value and raise condition
|
164
|
184
|
def add_superior(self, em_type, relation_nature):
|
165
|
|
- pass
|
|
185
|
+ if not isinstance(em_type, EmType) or not isinstance(relation_nature, str):
|
|
186
|
+ raise TypeError("Excepted <class EmType> and <class str> as arguments. But got : "+str(type(em_type))+" "+str(type(relation_nature)))
|
|
187
|
+ if relation_nature not in EmClassType.natures(self.classType):
|
|
188
|
+ raise ValueError("Invalid nature for add_superior : '"+relation_nature+"'. Allowed relations for this type are "+str(EmClassType.natures(self.classType)))
|
|
189
|
+
|
|
190
|
+ conn = self.getDbE().connect()
|
|
191
|
+ htable = self.__class__._tableHierarchy()
|
|
192
|
+ req = htable.insert(subordinate_id = self.uid, superior_id = em_type.uid, nature = relation_nature)
|
|
193
|
+
|
|
194
|
+ try:
|
|
195
|
+ res = conn.execute(req)
|
|
196
|
+ except sql.exc.IntegrityError:
|
|
197
|
+ ret = False
|
|
198
|
+ else:
|
|
199
|
+ ret = True
|
|
200
|
+ finally:
|
|
201
|
+ conn.close()
|
|
202
|
+
|
|
203
|
+ return ret
|
166
|
204
|
|
167
|
205
|
## Delete a superior in the type hierarchy
|
168
|
206
|
# @param em_type EmType: An EmType instance
|
169
|
207
|
# @throw TypeError when em_type isn't an EmType instance
|
170
|
|
- # @todo define return value and raise condition
|
171
|
|
- def del_superior(self, em_type):
|
172
|
|
- pass
|
|
208
|
+ def del_superior(self, em_type, relation_nature):
|
|
209
|
+ if not isinstance(em_type, EmType):
|
|
210
|
+ raise TypeError("Excepted <class EmType> as argument. But got : "+str(type(em_type)))
|
|
211
|
+ if relation_nature not in EmClassType.natures(self.classType):
|
|
212
|
+ raise ValueError("Invalid nature for add_superior : '"+relation_nature+"'. Allowed relations for this type are "+str(EmClassType.natures(self.classType)))
|
|
213
|
+
|
|
214
|
+ conn = self.getDbE().connect()
|
|
215
|
+ htable = self.__class__._tableHierarchy()
|
|
216
|
+ req = htable.delete().where(superior_id = em_type.uid, nature = relation_nature)
|
|
217
|
+ conn.execute(req)
|
|
218
|
+ conn.close()
|
173
|
219
|
|
174
|
220
|
## @brief Get the list of linked type
|
175
|
221
|
# Types are linked with special fields called relation_to_type fields
|