|
@@ -4,10 +4,10 @@ from Database import sqlutils
|
4
|
4
|
import sqlalchemy as sql
|
5
|
5
|
|
6
|
6
|
import EditorialModel
|
7
|
|
-from EditorialModel.components import EmComponent, EmComponentNotExistError
|
|
7
|
+from EditorialModel.components import EmComponent
|
8
|
8
|
from EditorialModel.fieldgroups import EmFieldGroup
|
9
|
9
|
from EditorialModel.fields import EmField
|
10
|
|
-from EditorialModel.classtypes import EmNature, EmClassType
|
|
10
|
+from EditorialModel.classtypes import EmClassType
|
11
|
11
|
import EditorialModel.fieldtypes as ftypes
|
12
|
12
|
import EditorialModel.classes
|
13
|
13
|
|
|
@@ -39,10 +39,10 @@ class EmType(EmComponent):
|
39
|
39
|
# @return An EmType instance
|
40
|
40
|
# @throw EmComponentExistError if an EmType with this name but different attributes exists
|
41
|
41
|
# @see EmComponent::__init__()
|
42
|
|
- #
|
|
42
|
+ #
|
43
|
43
|
# @todo check that em_class is an EmClass object (fieldtypes can handle it)
|
44
|
|
- def create(c, name, em_class, sortcolumn='rank', icon=None, **em_component_args):
|
45
|
|
- return super(EmType, c).create(name=name, class_id=em_class.uid, sortcolumn=sortcolumn, **em_component_args)
|
|
44
|
+ def create(cls, name, em_class, sortcolumn='rank', **em_component_args):
|
|
45
|
+ return super(EmType, cls).create(name=name, class_id=em_class.uid, sortcolumn=sortcolumn, **em_component_args)
|
46
|
46
|
|
47
|
47
|
@property
|
48
|
48
|
## Return an sqlalchemy table for type hierarchy
|
|
@@ -68,11 +68,10 @@ class EmType(EmComponent):
|
68
|
68
|
if sum([len(subs[subnat]) for subnat in subs]) > 0:
|
69
|
69
|
return False
|
70
|
70
|
#Delete all relation with superiors
|
71
|
|
- for nature,sups in self.superiors().items():
|
|
71
|
+ for nature, sups in self.superiors().items():
|
72
|
72
|
for sup in sups:
|
73
|
73
|
self.del_superior(sup, nature)
|
74
|
74
|
return super(EmType, self).delete()
|
75
|
|
-
|
76
|
75
|
|
77
|
76
|
## Get the list of associated fieldgroups
|
78
|
77
|
# @return A list of EmFieldGroup instance
|
|
@@ -84,7 +83,7 @@ class EmType(EmComponent):
|
84
|
83
|
rows = res.fetchall()
|
85
|
84
|
conn.close()
|
86
|
85
|
|
87
|
|
- return [ EmFieldGroup(row['uid']) for row in rows ]
|
|
86
|
+ return [EmFieldGroup(row['uid']) for row in rows]
|
88
|
87
|
|
89
|
88
|
## Get the list of all Emfield possibly associated with this type
|
90
|
89
|
# @return A list of EmField instance
|
|
@@ -104,8 +103,8 @@ class EmType(EmComponent):
|
104
|
103
|
table = sql.Table('em_field_type', meta)
|
105
|
104
|
res = conn.execute(table.select().where(table.c.type_id == self.uid))
|
106
|
105
|
|
107
|
|
- return [ EditorialModel.fields.EmField(row['field_id']) for row in res.fetchall()]
|
108
|
|
-
|
|
106
|
+ return [EditorialModel.fields.EmField(row['field_id']) for row in res.fetchall()]
|
|
107
|
+
|
109
|
108
|
## Return the list of associated fields
|
110
|
109
|
# @return A list of EmField instance
|
111
|
110
|
def fields(self):
|
|
@@ -113,7 +112,7 @@ class EmType(EmComponent):
|
113
|
112
|
for field in self.all_fields():
|
114
|
113
|
if not field.optional:
|
115
|
114
|
result.append(field)
|
116
|
|
- return result+selected_fields
|
|
115
|
+ return result + selected_fields
|
117
|
116
|
|
118
|
117
|
## Select_field (Function)
|
119
|
118
|
#
|
|
@@ -127,7 +126,7 @@ class EmType(EmComponent):
|
127
|
126
|
# @see EmType::_opt_field_act()
|
128
|
127
|
def select_field(self, field):
|
129
|
128
|
return self._opt_field_act(field, True)
|
130
|
|
-
|
|
129
|
+
|
131
|
130
|
## Unselect_field (Function)
|
132
|
131
|
#
|
133
|
132
|
# Indicates that an optional field will not be used
|
|
@@ -141,7 +140,6 @@ class EmType(EmComponent):
|
141
|
140
|
def unselect_field(self, field):
|
142
|
141
|
return self._opt_field_act(field, False)
|
143
|
142
|
|
144
|
|
-
|
145
|
143
|
## @brief Select or unselect an optional field
|
146
|
144
|
# @param field EmField: The EmField to select or unselect
|
147
|
145
|
# @param select bool: If True select field, else unselect it
|
|
@@ -151,7 +149,7 @@ class EmType(EmComponent):
|
151
|
149
|
# @throw ValueError if field is not optional or is not associated with this type
|
152
|
150
|
def _opt_field_act(self, field, select=True):
|
153
|
151
|
if not isinstance(field, EmField):
|
154
|
|
- raise TypeError("Excepted <class EmField> as field argument. But got "+str(type(field)))
|
|
152
|
+ raise TypeError("Excepted <class EmField> as field argument. But got " + str(type(field)))
|
155
|
153
|
if not field in self.all_fields():
|
156
|
154
|
raise ValueError("This field is not part of this type")
|
157
|
155
|
if not field.optional:
|
|
@@ -191,26 +189,25 @@ class EmType(EmComponent):
|
191
|
189
|
# @note Not conceptualized yet
|
192
|
190
|
# @todo Conception
|
193
|
191
|
# @todo Maybe we don't need a EmHook instance but just a hook identifier
|
194
|
|
- def del_hook(self,hook):
|
|
192
|
+ def del_hook(self, hook):
|
195
|
193
|
raise NotImplementedError()
|
196
|
194
|
|
197
|
|
-
|
198
|
195
|
## @brief Get the list of subordinates EmType
|
199
|
196
|
# Get a list of EmType instance that have this EmType for superior
|
200
|
197
|
# @return Return a dict with relation nature as keys and values as a list of subordinates
|
201
|
198
|
# EmType instance
|
202
|
199
|
# @throw RuntimeError if a nature fetched from db is not valid
|
203
|
200
|
def subordinates(self):
|
204
|
|
- return self._subOrSup(False)
|
|
201
|
+ return self._sub_or_sup(False)
|
205
|
202
|
|
206
|
203
|
## @brief Get the list of subordinates EmType
|
207
|
204
|
# Get a list of EmType instance that have this EmType for superior
|
208
|
205
|
# @return Return a dict with relation nature as keys and values as a list of subordinates
|
209
|
206
|
# EmType instance
|
210
|
207
|
# @throw RuntimeError if a nature fetched from db is not valid
|
211
|
|
- # @see EmType::_subOrSup()
|
|
208
|
+ # @see EmType::_sub_or_sup()
|
212
|
209
|
def superiors(self):
|
213
|
|
- return self._subOrSup(True)
|
|
210
|
+ return self._sub_or_sup(True)
|
214
|
211
|
|
215
|
212
|
## @brief Return the list of subordinates or superiors for an EmType
|
216
|
213
|
# This is the logic function that implements EmType::subordinates() and EmType::superiors()
|
|
@@ -218,10 +215,9 @@ class EmType(EmComponent):
|
218
|
215
|
# @return A dict with relation nature as keys and list of subordinates/superiors as values
|
219
|
216
|
# @throw RunTimeError if a nature fetched from db is not valid
|
220
|
217
|
# @see EmType::subordinates(), EmType::superiors()
|
221
|
|
- def _subOrSup(self, sup = True):
|
|
218
|
+ def _sub_or_sup(self, sup=True):
|
222
|
219
|
conn = self.db_engine().connect()
|
223
|
220
|
htable = self._table_hierarchy
|
224
|
|
- type_table = sqlutils.get_table(self.__class__)
|
225
|
221
|
|
226
|
222
|
req = htable.select()
|
227
|
223
|
if sup:
|
|
@@ -241,14 +237,12 @@ class EmType(EmComponent):
|
241
|
237
|
for row in rows:
|
242
|
238
|
if row['nature'] not in result:
|
243
|
239
|
#Maybe security issue ?
|
244
|
|
- logger.error("Unreconized or unauthorized nature in Database for EmType<"+str(self.uid)+"> subordinate <"+str(row['subordinate_id'])+"> : '"+row['nature']+"'")
|
245
|
240
|
raise RuntimeError("Unreconized nature from database : "+row['nature'])
|
246
|
|
-
|
|
241
|
+
|
247
|
242
|
to_fetch = 'superior_id' if sup else 'subordinate_id'
|
248
|
|
- result[row['nature']].append( EmType(row[to_fetch]) )
|
|
243
|
+ result[row['nature']].append(EmType(row[to_fetch]))
|
249
|
244
|
return result
|
250
|
245
|
|
251
|
|
-
|
252
|
246
|
## Add a superior in the type hierarchy
|
253
|
247
|
# @param em_type EmType: An EmType instance
|
254
|
248
|
# @param relation_nature str: The name of the relation's nature
|
|
@@ -259,9 +253,9 @@ class EmType(EmComponent):
|
259
|
253
|
# @throw ValueError when relation_nature don't allow to link this types together
|
260
|
254
|
def add_superior(self, em_type, relation_nature):
|
261
|
255
|
if not isinstance(em_type, EmType) or not isinstance(relation_nature, str):
|
262
|
|
- raise TypeError("Excepted <class EmType> and <class str> as em_type argument. But got : "+str(type(em_type))+" "+str(type(relation_nature)))
|
|
256
|
+ raise TypeError("Excepted <class EmType> and <class str> as em_type argument. But got : " + str(type(em_type)) + " " + str(type(relation_nature)))
|
263
|
257
|
if relation_nature not in EmClassType.natures(self.classtype['name']):
|
264
|
|
- raise ValueError("Invalid nature for add_superior : '"+relation_nature+"'. Allowed relations for this type are "+str(EmClassType.natures(self.classtype['name'])))
|
|
258
|
+ raise ValueError("Invalid nature for add_superior : '" + relation_nature + "'. Allowed relations for this type are " + str(EmClassType.natures(self.classtype['name'])))
|
265
|
259
|
|
266
|
260
|
#Checking that this relation is allowed by the nature of the relation
|
267
|
261
|
att = self.classtype['hierarchy'][relation_nature]['attach']
|
|
@@ -269,15 +263,15 @@ class EmType(EmComponent):
|
269
|
263
|
if self.classtype['name'] != em_type.classtype['name']:
|
270
|
264
|
raise ValueError("Not allowed to put an em_type with a different classtype as superior")
|
271
|
265
|
elif self.name != em_type.name:
|
272
|
|
- raise ValueError("Not allowed to put a different em_type as superior in a relation of nature '"+relation_nature+"'")
|
|
266
|
+ raise ValueError("Not allowed to put a different em_type as superior in a relation of nature '" + relation_nature + "'")
|
273
|
267
|
|
274
|
268
|
conn = self.db_engine().connect()
|
275
|
269
|
htable = self._table_hierarchy
|
276
|
|
- values = { 'subordinate_id': self.uid, 'superior_id': em_type.uid, 'nature': relation_nature }
|
|
270
|
+ values = {'subordinate_id': self.uid, 'superior_id': em_type.uid, 'nature': relation_nature}
|
277
|
271
|
req = htable.insert(values=values)
|
278
|
272
|
|
279
|
273
|
try:
|
280
|
|
- res = conn.execute(req)
|
|
274
|
+ conn.execute(req)
|
281
|
275
|
except sql.exc.IntegrityError:
|
282
|
276
|
ret = False
|
283
|
277
|
else:
|
|
@@ -292,9 +286,9 @@ class EmType(EmComponent):
|
292
|
286
|
# @throw TypeError when em_type isn't an EmType instance
|
293
|
287
|
def del_superior(self, em_type, relation_nature):
|
294
|
288
|
if not isinstance(em_type, EmType):
|
295
|
|
- raise TypeError("Excepted <class EmType> as argument. But got : "+str(type(em_type)))
|
|
289
|
+ raise TypeError("Excepted <class EmType> as argument. But got : " + str(type(em_type)))
|
296
|
290
|
if relation_nature not in EmClassType.natures(self.classtype['name']):
|
297
|
|
- raise ValueError("Invalid nature for add_superior : '"+relation_nature+"'. Allowed relations for this type are "+str(EmClassType.natures(self.classtype['name'])))
|
|
291
|
+ raise ValueError("Invalid nature for add_superior : '" + relation_nature + "'. Allowed relations for this type are " + str(EmClassType.natures(self.classtype['name'])))
|
298
|
292
|
|
299
|
293
|
conn = self.db_engine().connect()
|
300
|
294
|
htable = self._table_hierarchy
|
|
@@ -307,11 +301,11 @@ class EmType(EmComponent):
|
307
|
301
|
# @return a list of EmType
|
308
|
302
|
# @see EmFields
|
309
|
303
|
def linked_types(self):
|
310
|
|
- return self._linked_types_Db()
|
|
304
|
+ return self._linked_types_db()
|
311
|
305
|
|
312
|
306
|
## @brief Return the list of all the types linked to this type, should they be superiors or subordinates
|
313
|
307
|
# @return A list of EmType objects
|
314
|
|
- def _linked_types_Db(self):
|
|
308
|
+ def _linked_types_db(self):
|
315
|
309
|
conn = self.db_engine().connect()
|
316
|
310
|
htable = self._table_hierarchy
|
317
|
311
|
req = htable.select(htable.c.superior_id, htable.c.subordinate_id)
|
|
@@ -324,6 +318,6 @@ class EmType(EmComponent):
|
324
|
318
|
rows = dict(zip(rows.keys(), rows))
|
325
|
319
|
result = []
|
326
|
320
|
for row in rows:
|
327
|
|
- result.append(EmType(row['subordinate_id'] if row['superior_id']==self.uid else row['superior_id']))
|
|
321
|
+ result.append(EmType(row['subordinate_id'] if row['superior_id'] == self.uid else row['superior_id']))
|
328
|
322
|
|
329
|
323
|
return result
|