Browse Source

Merge branch 'component_db_instance_attr'

Conflicts:
	EditorialModel/classes.py
	EditorialModel/types.py
Yann Weber 9 years ago
parent
commit
9cec5af1fa

+ 7
- 11
Database/sqlutils.py View File

5
 import sqlalchemy as sqla
5
 import sqlalchemy as sqla
6
 from django.conf import settings
6
 from django.conf import settings
7
 
7
 
8
+import EditorialModel
8
 
9
 
9
 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Lodel.settings")
10
 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Lodel.settings")
10
 
11
 
76
 # @param cls : An EmComponent child class
77
 # @param cls : An EmComponent child class
77
 # @return An sqlalchemy table
78
 # @return An sqlalchemy table
78
 # @throw TypeError if em_instance is an EmComponent  or not an EmComponent child class (or an instance)
79
 # @throw TypeError if em_instance is an EmComponent  or not an EmComponent child class (or an instance)
79
-def get_table(cls):
80
-    from EditorialModel.components import EmComponent  # dirty circula inclusion hack
81
-    if not issubclass(cls, EmComponent) or cls.table is None:
82
-        raise TypeError("Excepting an EmComponent child class not an " + str(cls))
83
-    engine = cls.db_engine()
84
-    return sqla.Table(cls.table, meta(engine))
85
-
86
-
87
-def getTable(cls):
88
-    return get_table(cls)
89
-
80
+# @todo Move this function as an instance method ?
81
+def get_table(self):
82
+    if not issubclass(self.__class__, EditorialModel.components.EmComponent) or self.table is None:
83
+        raise TypeError("Excepting an EmComponent child class not an " + str(self.__class__))
84
+    engine = self.db_engine
85
+    return sqla.Table(self.table, meta(engine))
90
 
86
 
91
 ## This function is intended to execute ddl defined in sqlalter
87
 ## This function is intended to execute ddl defined in sqlalter
92
 # @warning There is a dirty workaround here, DDL should returns only one query, but DropColumn for sqlite has to return 4 queries (rename, create, insert, drop). There is a split on the compiled SQL to extract and execute one query at a time
88
 # @warning There is a dirty workaround here, DDL should returns only one query, but DropColumn for sqlite has to return 4 queries (rename, create, insert, drop). There is a split on the compiled SQL to extract and execute one query at a time

+ 6
- 6
EditorialModel/classes.py View File

44
         #Create a new entry in the em_class table
44
         #Create a new entry in the em_class table
45
         result = super(EmClass, cls).create(name=name, classtype=classtype, icon=icon, sortcolumn=sortcolumn, **em_component_args)
45
         result = super(EmClass, cls).create(name=name, classtype=classtype, icon=icon, sortcolumn=sortcolumn, **em_component_args)
46
 
46
 
47
-        dbe = result.db_engine()
47
+        dbe = result.db_engine
48
         conn = dbe.connect()
48
         conn = dbe.connect()
49
 
49
 
50
         #Create a new table storing LodelObjects of this EmClass
50
         #Create a new table storing LodelObjects of this EmClass
69
         if len(fieldgroups) > 0:
69
         if len(fieldgroups) > 0:
70
             return False
70
             return False
71
 
71
 
72
-        dbe = self.__class__.db_engine()
72
+        dbe = self.db_engine
73
         meta = sqlutils.meta(dbe)
73
         meta = sqlutils.meta(dbe)
74
         #Here we have to give a connection
74
         #Here we have to give a connection
75
         class_table = sql.Table(self.name, meta)
75
         class_table = sql.Table(self.name, meta)
88
     ## Isolate SQL for EmClass::fieldgroups
88
     ## Isolate SQL for EmClass::fieldgroups
89
     # @return An array of dict (sqlalchemy fetchall)
89
     # @return An array of dict (sqlalchemy fetchall)
90
     def _fieldgroups_db(self):
90
     def _fieldgroups_db(self):
91
-        dbe = self.__class__.db_engine()
91
+        dbe = self.db_engine
92
         emfg = sql.Table(EditorialModel.fieldgroups.EmFieldGroup.table, sqlutils.meta(dbe))
92
         emfg = sql.Table(EditorialModel.fieldgroups.EmFieldGroup.table, sqlutils.meta(dbe))
93
         req = emfg.select().where(emfg.c.class_id == self.uid)
93
         req = emfg.select().where(emfg.c.class_id == self.uid)
94
 
94
 
116
     ## Isolate SQL for EmCLass::types
116
     ## Isolate SQL for EmCLass::types
117
     # @return An array of dict (sqlalchemy fetchall)
117
     # @return An array of dict (sqlalchemy fetchall)
118
     def _types_db(self):
118
     def _types_db(self):
119
-        dbe = self.__class__.db_engine()
119
+        dbe = self.db_engine
120
         emtype = sql.Table(EditorialModel.types.EmType.table, sqlutils.meta(dbe))
120
         emtype = sql.Table(EditorialModel.types.EmType.table, sqlutils.meta(dbe))
121
         req = emtype.select().where(emtype.c.class_id == self.uid)
121
         req = emtype.select().where(emtype.c.class_id == self.uid)
122
         conn = dbe.connect()
122
         conn = dbe.connect()
134
 
134
 
135
     def _link_type_db(self, table_name):
135
     def _link_type_db(self, table_name):
136
         #  Create a new table storing additionnal fields for the relation between the linked type and this EmClass
136
         #  Create a new table storing additionnal fields for the relation between the linked type and this EmClass
137
-        conn = self.__class__.db_engine().connect()
137
+        conn = self.db_engine.connect()
138
         meta = sql.MetaData()
138
         meta = sql.MetaData()
139
         emlinketable = sql.Table(table_name, meta, sql.Column('uid', sql.VARCHAR(50), primary_key=True))
139
         emlinketable = sql.Table(table_name, meta, sql.Column('uid', sql.VARCHAR(50), primary_key=True))
140
         emlinketable.create(conn)
140
         emlinketable.create(conn)
146
         return self._linked_types_db()
146
         return self._linked_types_db()
147
 
147
 
148
     def _linked_types_db(self):
148
     def _linked_types_db(self):
149
-        dbe = self.__class__.db_engine()
149
+        dbe = self.db_engine
150
         meta = sql.MetaData()
150
         meta = sql.MetaData()
151
         meta.reflect(dbe)
151
         meta.reflect(dbe)
152
 
152
 

+ 38
- 21
EditorialModel/components.py View File

53
     # @param id_or_name int|str: name or id of the object
53
     # @param id_or_name int|str: name or id of the object
54
     # @throw TypeError if id_or_name is not an integer nor a string
54
     # @throw TypeError if id_or_name is not an integer nor a string
55
     # @throw NotImplementedError if called with EmComponent
55
     # @throw NotImplementedError if called with EmComponent
56
-    def __init__(self, id_or_name):
56
+    def __init__(self, id_or_name, dbconf = 'default'):
57
+        
58
+        self.dbconf = dbconf
59
+        if self.dbconf:
60
+            self.db_engine = sqlutils.get_engine(dbconf)
61
+        else:
62
+            self.db_engine = False
63
+
57
         if type(self) == EmComponent:
64
         if type(self) == EmComponent:
58
             raise NotImplementedError('Abstract class')
65
             raise NotImplementedError('Abstract class')
59
 
66
 
129
 
136
 
130
         super(EmComponent, self).__setattr__('deleted', False)
137
         super(EmComponent, self).__setattr__('deleted', False)
131
 
138
 
132
-    @classmethod
139
+    #@classmethod
133
     ## Shortcut that return the sqlAlchemy engine
140
     ## Shortcut that return the sqlAlchemy engine
134
-    def db_engine(cls):
135
-        return sqlutils.get_engine(cls.dbconf)
141
+    #def db_engine(cls):
142
+    #    return sqlutils.get_engine(cls.dbconf)
136
 
143
 
137
     ## Do the query on the database for EmComponent::populate()
144
     ## Do the query on the database for EmComponent::populate()
138
     # @throw EmComponentNotExistError if the instance is not anymore stored in database
145
     # @throw EmComponentNotExistError if the instance is not anymore stored in database
139
     def _populate_db(self):
146
     def _populate_db(self):
140
-        dbe = self.__class__.db_engine()
147
+        dbe = self.db_engine
141
         component = sql.Table(self.table, sqlutils.meta(dbe))
148
         component = sql.Table(self.table, sqlutils.meta(dbe))
142
         req = sql.sql.select([component])
149
         req = sql.sql.select([component])
143
 
150
 
159
     ## Insert a new component in the database
166
     ## Insert a new component in the database
160
     #
167
     #
161
     # This function create and assign a new UID and handle the date_create and date_update values
168
     # This function create and assign a new UID and handle the date_create and date_update values
162
-    #
169
+    # @warning There is a mandatory argument dbconf that indicate wich database configuration to use
163
     # @param **kwargs : Names arguments representing object properties
170
     # @param **kwargs : Names arguments representing object properties
164
     # @return An instance of the created component
171
     # @return An instance of the created component
165
     # @throw TypeError if an element of kwargs isn't a valid object propertie or if a mandatory argument is missing
172
     # @throw TypeError if an element of kwargs isn't a valid object propertie or if a mandatory argument is missing
194
         #    if cls._fields[name].notNull and cls._fields[name].default == None:
201
         #    if cls._fields[name].notNull and cls._fields[name].default == None:
195
         #        raise TypeError("Missing argument : "+name)
202
         #        raise TypeError("Missing argument : "+name)
196
 
203
 
204
+        if 'dbconf' in kwargs:
205
+            if not kwargs['db_engine']:
206
+                raise NotImplementedError("Its a nonsense to call create with no database")
207
+            dbconf = kwargs['dbconf']
208
+        else:
209
+            dbconf = 'default'
210
+        dbe = sqlutils.get_engine(dbconf)
197
 
211
 
198
-        kwargs['uid'] = cls.new_uid()
212
+        kwargs['uid'] = cls.new_uid(dbe)
199
         kwargs['date_update'] = kwargs['date_create'] = datetime.datetime.utcnow()
213
         kwargs['date_update'] = kwargs['date_create'] = datetime.datetime.utcnow()
200
 
214
 
201
-        dbe = cls.db_engine()
202
         conn = dbe.connect()
215
         conn = dbe.connect()
203
 
216
 
204
-        kwargs['rank'] = cls.get_max_rank( kwargs[cls.ranked_in] )+1
217
+        kwargs['rank'] = cls._get_max_rank( kwargs[cls.ranked_in], dbe )+1
205
 
218
 
206
         table = sql.Table(cls.table, sqlutils.meta(dbe))
219
         table = sql.Table(cls.table, sqlutils.meta(dbe))
207
         req = table.insert(kwargs)
220
         req = table.insert(kwargs)
208
         if not conn.execute(req):
221
         if not conn.execute(req):
209
             raise RuntimeError("Unable to create the "+cls.__class__.__name__+" EmComponent ")
222
             raise RuntimeError("Unable to create the "+cls.__class__.__name__+" EmComponent ")
210
         conn.close()
223
         conn.close()
211
-        return cls(kwargs['name'])
224
+        return cls(kwargs['name'], dbconf)
212
 
225
 
213
     ## Write the representation of the component in the database
226
     ## Write the representation of the component in the database
214
     # @return bool
227
     # @return bool
232
     # @throw RunTimeError if it was unable to do the Db update
245
     # @throw RunTimeError if it was unable to do the Db update
233
     def _save_db(self, values):
246
     def _save_db(self, values):
234
         """ Do the query on the db """
247
         """ Do the query on the db """
235
-        dbe = self.__class__.db_engine()
248
+        dbe = self.db_engine
236
         component = sql.Table(self.table, sqlutils.meta(dbe))
249
         component = sql.Table(self.table, sqlutils.meta(dbe))
237
         req = sql.update(component, values=values).where(component.c.uid == self.uid)
250
         req = sql.update(component, values=values).where(component.c.uid == self.uid)
238
 
251
 
247
     # @throw RunTimeError if it was unable to do the deletion
260
     # @throw RunTimeError if it was unable to do the deletion
248
     def delete(self):
261
     def delete(self):
249
         #<SQL>
262
         #<SQL>
250
-        dbe = self.__class__.db_engine()
263
+        dbe = self.db_engine
251
         component = sql.Table(self.table, sqlutils.meta(dbe))
264
         component = sql.Table(self.table, sqlutils.meta(dbe))
252
         req = component.delete().where(component.c.uid == self.uid)
265
         req = component.delete().where(component.c.uid == self.uid)
253
         conn = dbe.connect()
266
         conn = dbe.connect()
260
         super(EmComponent, self).__setattr__('deleted', True)
273
         super(EmComponent, self).__setattr__('deleted', True)
261
         return True
274
         return True
262
 
275
 
263
-    ## get_max_rank
264
-    # Retourne le rank le plus élevé pour le groupe de component au quel apartient l'objet actuelle
276
+    ## @brief Get the maximum rank given an EmComponent child class and a ranked_in filter
265
     # @param ranked_in_value mixed: The rank "family"
277
     # @param ranked_in_value mixed: The rank "family"
266
-    # @param return -1 if no EmComponent found else return an integer >= 0
278
+    # @return -1 if no EmComponent found else return an integer >= 0
267
     @classmethod
279
     @classmethod
268
-    def get_max_rank(cls, ranked_in_value):
269
-        dbe = cls.db_engine()
280
+    def _get_max_rank(cls, ranked_in_value, dbe):
270
         component = sql.Table(cls.table, sqlutils.meta(dbe))
281
         component = sql.Table(cls.table, sqlutils.meta(dbe))
271
         req = sql.sql.select([component.c.rank]).where(getattr(component.c, cls.ranked_in) == ranked_in_value).order_by(component.c.rank.desc())
282
         req = sql.sql.select([component.c.rank]).where(getattr(component.c, cls.ranked_in) == ranked_in_value).order_by(component.c.rank.desc())
272
         c = dbe.connect()
283
         c = dbe.connect()
278
         else:
289
         else:
279
             return -1
290
             return -1
280
 
291
 
292
+    ## Only make a call to the class method
293
+    # @return A positive integer or -1 if no components
294
+    # @see EmComponent::_get_max_rank()
295
+    def get_max_rank(self, ranked_in_value):
296
+        return self.__class__._get_max_rank(ranked_in_value, self.db_engine)
297
+
281
     ## Set a new rank for this component
298
     ## Set a new rank for this component
282
     # @note This function assume that ranks are properly set from 1 to x with no gap
299
     # @note This function assume that ranks are properly set from 1 to x with no gap
283
     # @param new_rank int: The new rank
300
     # @param new_rank int: The new rank
298
         limits = [ self.rank + ( 1 if mod > 0 else -1), new_rank ] #The range of modified ranks
315
         limits = [ self.rank + ( 1 if mod > 0 else -1), new_rank ] #The range of modified ranks
299
         limits.sort()
316
         limits.sort()
300
 
317
 
301
-        dbe = self.db_engine()
318
+        dbe = self.db_engine
302
         conn = dbe.connect()
319
         conn = dbe.connect()
303
-        table = sqlutils.get_table(self.__class__)
320
+        table = sqlutils.get_table(self)
304
 
321
 
305
         #Selecting the components that will be modified
322
         #Selecting the components that will be modified
306
         req = table.select().where( getattr(table.c, self.ranked_in) == getattr(self, self.ranked_in)).where(table.c.rank >= limits[0]).where(table.c.rank <= limits[1])
323
         req = table.select().where( getattr(table.c, self.ranked_in) == getattr(self, self.ranked_in)).where(table.c.rank >= limits[0]).where(table.c.rank <= limits[1])
358
     #
375
     #
359
     # Use the class property table
376
     # Use the class property table
360
     # @return A new uid (an integer)
377
     # @return A new uid (an integer)
361
-    def new_uid(cls):
378
+    def new_uid(cls, db_engine):
362
         if cls.table is None:
379
         if cls.table is None:
363
             raise NotImplementedError("Abstract method")
380
             raise NotImplementedError("Abstract method")
364
 
381
 
365
-        dbe = cls.db_engine()
382
+        dbe = db_engine
366
 
383
 
367
         uidtable = sql.Table('uids', sqlutils.meta(dbe))
384
         uidtable = sql.Table('uids', sqlutils.meta(dbe))
368
         conn = dbe.connect()
385
         conn = dbe.connect()

+ 4
- 2
EditorialModel/fieldgroups.py View File

5
 import EditorialModel.fieldtypes as ftypes
5
 import EditorialModel.fieldtypes as ftypes
6
 
6
 
7
 from Database import sqlutils
7
 from Database import sqlutils
8
+import sqlalchemy as sql
8
 
9
 
9
 import EditorialModel
10
 import EditorialModel
10
 
11
 
43
     ## Get the list of associated fields
44
     ## Get the list of associated fields
44
     # @return A list of EmField instance
45
     # @return A list of EmField instance
45
     def fields(self):
46
     def fields(self):
46
-        field_table = sqlutils.getTable(EditorialModel.fields.EmField)
47
+        meta = sqlutils.meta(self.db_engine)
48
+        field_table = sql.Table(EditorialModel.fields.EmField.table, meta)
47
         req = field_table.select(field_table.c.uid).where(field_table.c.fieldgroup_id == self.uid)
49
         req = field_table.select(field_table.c.uid).where(field_table.c.fieldgroup_id == self.uid)
48
-        conn = self.__class__.db_engine().connect()
50
+        conn = self.db_engine.connect()
49
         res = conn.execute(req)
51
         res = conn.execute(req)
50
         rows = res.fetchall()
52
         rows = res.fetchall()
51
         conn.close()
53
         conn.close()

+ 6
- 5
EditorialModel/fields.py View File

71
     # @return bool : True if deleted False if deletion aborded
71
     # @return bool : True if deleted False if deletion aborded
72
     # @todo Check if unconditionnal deletion is correct
72
     # @todo Check if unconditionnal deletion is correct
73
     def delete(self):
73
     def delete(self):
74
-        dbe = self.__class__.db_engine()
74
+        dbe = self.db_engine
75
         class_table = sql.Table(self.get_class_table(), sqlutils.meta(dbe))
75
         class_table = sql.Table(self.get_class_table(), sqlutils.meta(dbe))
76
         field_col = sql.Column(self.name)
76
         field_col = sql.Column(self.name)
77
         ddl = DropColumn(class_table, field_col)
77
         ddl = DropColumn(class_table, field_col)
78
-        sqlutils.ddl_execute(ddl, self.__class__.db_engine())
78
+        sqlutils.ddl_execute(ddl, self.db_engine)
79
         return super(EmField, self).delete()
79
         return super(EmField, self).delete()
80
 
80
 
81
     ## add_field_column_to_class_table (Function)
81
     ## add_field_column_to_class_table (Function)
84
     #
84
     #
85
     # @return True in case of success, False if not
85
     # @return True in case of success, False if not
86
     def add_field_column_to_class_table(self):
86
     def add_field_column_to_class_table(self):
87
-        dbe = self.db_engine()
87
+        dbe = self.db_engine
88
         fieldtype = get_field_type(self.fieldtype)
88
         fieldtype = get_field_type(self.fieldtype)
89
         new_column = sql.Column(name=self.name, **(fieldtype.sqlalchemy_args()))
89
         new_column = sql.Column(name=self.name, **(fieldtype.sqlalchemy_args()))
90
         class_table = sql.Table(self.get_class_table(), sqlutils.meta(dbe))
90
         class_table = sql.Table(self.get_class_table(), sqlutils.meta(dbe))
103
     # @return An EmClass instance
103
     # @return An EmClass instance
104
     def get_class(self):
104
     def get_class(self):
105
         #<SQL>
105
         #<SQL>
106
-        dbe = self.db_engine()
106
+        dbe = self.db_engine
107
+        meta = sqlutils.meta(dbe)
107
         conn = dbe.connect()
108
         conn = dbe.connect()
108
-        fieldgroup_table = sqlutils.getTable(EmFieldGroup)
109
+        fieldgroup_table = sql.Table(EmFieldGroup.table, meta)
109
         req = fieldgroup_table.select().where(fieldgroup_table.c.uid == self.fieldgroup_id)
110
         req = fieldgroup_table.select().where(fieldgroup_table.c.uid == self.fieldgroup_id)
110
         res = conn.execute(req)
111
         res = conn.execute(req)
111
         row = res.fetchone()
112
         row = res.fetchone()

+ 5
- 5
EditorialModel/test/test_component.py View File

240
     def test_newuid(self):
240
     def test_newuid(self):
241
         """ Test valid calls for new_uid method """
241
         """ Test valid calls for new_uid method """
242
         for _ in range(10):
242
         for _ in range(10):
243
-            nuid = EmTestComp.new_uid()
243
+            nuid = EmTestComp.new_uid(self.dber)
244
         
244
         
245
             conn = self.dber.connect()
245
             conn = self.dber.connect()
246
             tuid = sqla.Table('uids', sqlutils.meta(self.dber))
246
             tuid = sqla.Table('uids', sqlutils.meta(self.dber))
257
     def test_newuid_abstract(self):
257
     def test_newuid_abstract(self):
258
         """ Test not valit call for new_uid method """
258
         """ Test not valit call for new_uid method """
259
         with self.assertRaises(NotImplementedError):
259
         with self.assertRaises(NotImplementedError):
260
-            EmComponent.new_uid()
260
+            EmComponent.new_uid(self.dber)
261
         pass
261
         pass
262
         
262
         
263
 #=======================#
263
 #=======================#
432
         pass
432
         pass
433
 
433
 
434
     def testGetMaxRank(self):
434
     def testGetMaxRank(self):
435
-        old = EmTestComp.get_max_rank('f')
435
+        old = EmTestComp._get_max_rank('f', self.dber)
436
         EmTestComp.create(name="foobartest", rank_fam = 'f')
436
         EmTestComp.create(name="foobartest", rank_fam = 'f')
437
-        n = EmTestComp.get_max_rank('f')
437
+        n = EmTestComp._get_max_rank('f', self.dber)
438
         self.assertEqual(old+1, n, "Excepted value was "+str(old+1)+" but got "+str(n))
438
         self.assertEqual(old+1, n, "Excepted value was "+str(old+1)+" but got "+str(n))
439
-        self.assertEqual(EmTestComp.get_max_rank('z'), -1)
439
+        self.assertEqual(EmTestComp._get_max_rank('z', self.dber), -1)
440
         pass
440
         pass
441
 
441
 
442
 #====================#
442
 #====================#

+ 3
- 2
EditorialModel/test/test_field.py View File

79
     # @param field EmField: EmField object
79
     # @param field EmField: EmField object
80
     # @return Number of found records
80
     # @return Number of found records
81
     def _get_field_records_Db(self,field):
81
     def _get_field_records_Db(self,field):
82
-        dbe = EmComponent.db_engine()
82
+        dbe = field.db_engine
83
         fieldtable = sqla.Table(EmField.table, sqlutils.meta(dbe))
83
         fieldtable = sqla.Table(EmField.table, sqlutils.meta(dbe))
84
         conn = dbe.connect()
84
         conn = dbe.connect()
85
         req = fieldtable.select().where(fieldtable.c.uid==field.uid).where(fieldtable.c.name==field.name)
85
         req = fieldtable.select().where(fieldtable.c.uid==field.uid).where(fieldtable.c.name==field.name)
103
     # @param table_name str: Name of the table
103
     # @param table_name str: Name of the table
104
     # @return list of columns
104
     # @return list of columns
105
     def _get_table_columns_Db(self, table_name):
105
     def _get_table_columns_Db(self, table_name):
106
-        table = sqla.Table(table_name, sqlutils.meta(EmComponent.db_engine()))
106
+        dbe = self.testClass.db_engine
107
+        table = sqla.Table(table_name, sqlutils.meta(dbe))
107
         return table.c
108
         return table.c
108
 
109
 
109
 ## TestField (Class)
110
 ## TestField (Class)

+ 4
- 3
EditorialModel/test/test_fieldgroups.py View File

67
 
67
 
68
     def setUp(self):
68
     def setUp(self):
69
         super(TestInit, self).setUp()
69
         super(TestInit, self).setUp()
70
+        dbe = sqlutils.get_engine()
70
         conn = sqlutils.get_engine().connect()
71
         conn = sqlutils.get_engine().connect()
71
 
72
 
72
         ent1 = EmClass('entity1')
73
         ent1 = EmClass('entity1')
76
         self.creadate = datetime.datetime.utcnow()
77
         self.creadate = datetime.datetime.utcnow()
77
         #Test fieldgroup
78
         #Test fieldgroup
78
         self.tfg = [
79
         self.tfg = [
79
-            { 'uid': EmFieldGroup.new_uid(), 'name': 'fg1', 'string': '{"fr":"Super Fieldgroup"}', 'help': '{"en":"help"}', 'rank': 0 , 'class_id': ent1.uid, 'date_create' : self.creadate, 'date_update': self.creadate},
80
-            { 'uid': EmFieldGroup.new_uid(), 'name': 'fg2', 'string': '{"fr":"Super Fieldgroup"}', 'help': '{"en":"help"}', 'rank': 1 , 'class_id': ent1.uid, 'date_create': self.creadate, 'date_update': self.creadate},
81
-            { 'uid': EmFieldGroup.new_uid(), 'name': 'fg3', 'string': '{"fr":"Super Fieldgroup"}', 'help': '{"en":"help"}', 'rank': 2 , 'class_id': idx1.uid, 'date_create': self.creadate, 'date_update': self.creadate},
80
+            { 'uid': EmFieldGroup.new_uid(dbe), 'name': 'fg1', 'string': '{"fr":"Super Fieldgroup"}', 'help': '{"en":"help"}', 'rank': 0 , 'class_id': ent1.uid, 'date_create' : self.creadate, 'date_update': self.creadate},
81
+            { 'uid': EmFieldGroup.new_uid(dbe), 'name': 'fg2', 'string': '{"fr":"Super Fieldgroup"}', 'help': '{"en":"help"}', 'rank': 1 , 'class_id': ent1.uid, 'date_create': self.creadate, 'date_update': self.creadate},
82
+            { 'uid': EmFieldGroup.new_uid(dbe), 'name': 'fg3', 'string': '{"fr":"Super Fieldgroup"}', 'help': '{"en":"help"}', 'rank': 2 , 'class_id': idx1.uid, 'date_create': self.creadate, 'date_update': self.creadate},
82
         ]
83
         ]
83
 
84
 
84
         req = sqla.Table('em_fieldgroup', sqlutils.meta(sqlutils.get_engine())).insert(self.tfg)
85
         req = sqla.Table('em_fieldgroup', sqlutils.meta(sqlutils.get_engine())).insert(self.tfg)

+ 11
- 9
EditorialModel/types.py View File

49
     # @return sqlalchemy em_type_hierarchy table object
49
     # @return sqlalchemy em_type_hierarchy table object
50
     # @todo Don't hardcode table name
50
     # @todo Don't hardcode table name
51
     def _table_hierarchy(self):
51
     def _table_hierarchy(self):
52
-        return sql.Table(self.__class__.table_hierarchy, sqlutils.meta(self.db_engine()))
52
+        return sql.Table(self.__class__.table_hierarchy, sqlutils.meta(self.db_engine))
53
 
53
 
54
     @property
54
     @property
55
     ## Return the EmClassType of the type
55
     ## Return the EmClassType of the type
76
     ## Get the list of associated fieldgroups
76
     ## Get the list of associated fieldgroups
77
     # @return A list of EmFieldGroup instance
77
     # @return A list of EmFieldGroup instance
78
     def field_groups(self):
78
     def field_groups(self):
79
-        fg_table = sqlutils.getTable(EmFieldGroup)
79
+        meta = sqlutils.meta(self.db_engine)
80
+        fg_table = sql.Table(EmFieldGroup.table, meta)
80
         req = fg_table.select(fg_table.c.uid).where(fg_table.c.class_id == self.class_id)
81
         req = fg_table.select(fg_table.c.uid).where(fg_table.c.class_id == self.class_id)
81
-        conn = self.__class__.db_engine().connect()
82
+        conn = self.db_engine.connect()
82
         res = conn.execute(req)
83
         res = conn.execute(req)
83
         rows = res.fetchall()
84
         rows = res.fetchall()
84
         conn.close()
85
         conn.close()
96
     ## Return selected optional field
97
     ## Return selected optional field
97
     # @return A list of EmField instance
98
     # @return A list of EmField instance
98
     def selected_fields(self):
99
     def selected_fields(self):
99
-        dbe = self.db_engine()
100
+        dbe = self.db_engine
100
         meta = sqlutils.meta(dbe)
101
         meta = sqlutils.meta(dbe)
101
         conn = dbe.connect()
102
         conn = dbe.connect()
102
 
103
 
155
         if not field.optional:
156
         if not field.optional:
156
             raise ValueError("This field is not optional")
157
             raise ValueError("This field is not optional")
157
 
158
 
158
-        dbe = self.db_engine()
159
+        dbe = self.db_engine
159
         meta = sqlutils.meta(dbe)
160
         meta = sqlutils.meta(dbe)
160
         conn = dbe.connect()
161
         conn = dbe.connect()
161
 
162
 
216
     # @throw RunTimeError if a nature fetched from db is not valid
217
     # @throw RunTimeError if a nature fetched from db is not valid
217
     # @see EmType::subordinates(), EmType::superiors()
218
     # @see EmType::subordinates(), EmType::superiors()
218
     def _sub_or_sup(self, sup=True):
219
     def _sub_or_sup(self, sup=True):
219
-        conn = self.db_engine().connect()
220
+        conn = self.db_engine.connect()
220
         htable = self._table_hierarchy
221
         htable = self._table_hierarchy
222
+        type_table = sqlutils.get_table(self)
221
 
223
 
222
         req = htable.select()
224
         req = htable.select()
223
         if sup:
225
         if sup:
265
         elif self.name != em_type.name:
267
         elif self.name != em_type.name:
266
             raise ValueError("Not allowed to put a different em_type as superior in a relation of nature '" + relation_nature + "'")
268
             raise ValueError("Not allowed to put a different em_type as superior in a relation of nature '" + relation_nature + "'")
267
 
269
 
268
-        conn = self.db_engine().connect()
270
+        conn = self.db_engine.connect()
269
         htable = self._table_hierarchy
271
         htable = self._table_hierarchy
270
         values = {'subordinate_id': self.uid, 'superior_id': em_type.uid, 'nature': relation_nature}
272
         values = {'subordinate_id': self.uid, 'superior_id': em_type.uid, 'nature': relation_nature}
271
         req = htable.insert(values=values)
273
         req = htable.insert(values=values)
290
         if relation_nature not in EmClassType.natures(self.classtype['name']):
292
         if relation_nature not in 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'])))
293
             raise ValueError("Invalid nature for add_superior : '" + relation_nature + "'. Allowed relations for this type are " + str(EmClassType.natures(self.classtype['name'])))
292
 
294
 
293
-        conn = self.db_engine().connect()
295
+        conn = self.db_engine.connect()
294
         htable = self._table_hierarchy
296
         htable = self._table_hierarchy
295
         req = htable.delete(htable.c.superior_id == em_type.uid and htable.c.nature == relation_nature)
297
         req = htable.delete(htable.c.superior_id == em_type.uid and htable.c.nature == relation_nature)
296
         conn.execute(req)
298
         conn.execute(req)
306
     ## @brief Return the list of all the types linked to this type, should they be superiors or subordinates
308
     ## @brief Return the list of all the types linked to this type, should they be superiors or subordinates
307
     # @return A list of EmType objects
309
     # @return A list of EmType objects
308
     def _linked_types_db(self):
310
     def _linked_types_db(self):
309
-        conn = self.db_engine().connect()
311
+        conn = self.db_engine.connect()
310
         htable = self._table_hierarchy
312
         htable = self._table_hierarchy
311
         req = htable.select(htable.c.superior_id, htable.c.subordinate_id)
313
         req = htable.select(htable.c.superior_id, htable.c.subordinate_id)
312
         req = req.where(sql.or_(htable.c.subordinate_id == self.uid, htable.c.superior_id == self.uid))
314
         req = req.where(sql.or_(htable.c.subordinate_id == self.uid, htable.c.superior_id == self.uid))

Loading…
Cancel
Save