Browse Source

SQL isolation in fieldgroups and fields

Yann Weber 9 years ago
parent
commit
9d5f1ac260
2 changed files with 43 additions and 16 deletions
  1. 24
    8
      EditorialModel/fieldgroups.py
  2. 19
    8
      EditorialModel/fields.py

+ 24
- 8
EditorialModel/fieldgroups.py View File

@@ -1,7 +1,8 @@
1 1
 #-*- coding: utf-8 -*-
2 2
 
3 3
 from EditorialModel.components import EmComponent, EmComponentNotExistError
4
-from Database.sqlobject import SqlObject
4
+from Database import sqlutils
5
+import sqlalchemy as sql
5 6
 
6 7
 import EditorialModel
7 8
 
@@ -33,16 +34,31 @@ class EmFieldGroup(EmComponent):
33 34
         try:
34 35
             exists = EmFieldGroup(name)
35 36
         except EmComponentNotExistError:
36
-            uids = SqlObject('uids')
37
-            res = uids.wexec(uids.table.insert().values(table=EmFieldGroup.table))
38
-            uid = res.inserted_primary_key
39
-
40
-            emfieldgroup = SqlObject(EmFieldGroup.table)
41
-            res = emfieldgroup.wexec(emfieldgroup.table.insert().values(uid=uid, name=name, class_id=em_class.id))
42
-            return EmFieldGroup(name)
37
+            return EmFieldGroup._createDb(name, em_class)
43 38
 
44 39
         return exists
45 40
 
41
+    @classmethod
42
+    def _createDb(c,name, em_class):
43
+        """ Make the Db insertion for fieldgroup creation """
44
+        dbe = c.getDbE()
45
+        #Create a new uid
46
+        uids = sql.Table('uids', sqlutils.meta(dbe))
47
+        conn = dbe.connect()
48
+        req = uids.insert(values={'table': c.table})
49
+        res = conn.execute(req)
50
+        uid = res.inserted_primary_key
51
+
52
+        req = sql.Table(c.table, sqlutils.meta(dbe)).insert().values(uid=uid, name=name, class_id=em_class.id)
53
+        res = conn.execute(req)
54
+
55
+        conn.close()
56
+
57
+        return EmFieldGroup(name)
58
+        
59
+
60
+        
61
+
46 62
     """ Use dictionary (from database) to populate the object
47 63
     """
48 64
     def populate(self):

+ 19
- 8
EditorialModel/fields.py View File

@@ -40,12 +40,8 @@ class EmField(EmComponent):
40 40
         try:
41 41
             exists = EmField(name)
42 42
         except EmComponentNotExistError:
43
-            uids = SqlObject('uids')
44
-            res = uids.wexec(uids.table.insert().values(table=EmField.table))
45
-            uid = res.inserted_primary_key
46
-
47 43
             values = {
48
-                'uid' : uid,
44
+                'uid' : None,
49 45
                 'name' : name,
50 46
                 'fieldgroup_id' : em_fieldgroup.id,
51 47
                 'fieldtype_id' : em_fieldtype.id,
@@ -53,11 +49,26 @@ class EmField(EmComponent):
53 49
                 'internal' : 1 if internal else 0,
54 50
             }
55 51
 
56
-            emfield_req = SqlObject(EmField.table)
57
-            res = emfield_req.wexec(emfield_req.table.insert(values=values))
58
-            return EmField(name)
52
+            return EmField._createDb(values)
59 53
 
60 54
         return exists
55
+    
56
+    @classmethod
57
+    def _createDb(c, values):
58
+        dbe = c.getDbe()
59
+        #Create a new uid
60
+        uids = sql.Table('uids', sqlutils.meta(dbe))
61
+        conn = dbe.connect()
62
+        req = uids.insert(values={'table':c.table})
63
+        res = conn.execute(req)
64
+
65
+        values['uid'] = res.inserted_primary_key
66
+        req = sql.Table(c.table).insert(values=values)
67
+        res = conn.execute(req)
68
+
69
+        conn.close()
70
+
71
+        return Field(values['name'])
61 72
 
62 73
     """ Use dictionary (from database) to populate the object
63 74
     """

Loading…
Cancel
Save