Browse Source

Merge branch 'master' of git.labocleo.org:lodel2

ArnAud 9 years ago
parent
commit
b5209a9c64

+ 3
- 2
Database/sqlsetup.py View File

@@ -1,6 +1,7 @@
1 1
 # -*- coding: utf-8 -*-
2 2
 
3 3
 from Database.sqlwrapper import SqlWrapper
4
+import sqlalchemy as sql
4 5
 
5 6
 class SQLSetup(object): 
6 7
 
@@ -74,8 +75,8 @@ class SQLSetup(object):
74 75
         em_field['columns'] = default_columns + [
75 76
             {"name":"fieldtype",   "type":"VARCHAR(50)", "extra":{"nullable":False}},
76 77
             {"name":"fieldgroup_id",  "type":"INTEGER", "extra":{"foreignkey":"em_fieldgroup.uid", "nullable":False}},
77
-            {"name":"rel_to_type_id", "type":"INTEGER", "extra":{"foreignkey":"em_type.uid", "nullable":True, "default": None}}, # if relational: type this field refer to
78
-            {"name":"rel_field_id",   "type":"INTEGER", "extra":{"foreignkey":"em_type.uid", "nullable":True, "default": None}}, # if relational: field that specify the rel_to_type_id
78
+            {"name":"rel_to_type_id", "type":"INTEGER", "extra":{"foreignkey":"em_type.uid", "nullable":True, "server_default": sql.text('NULL')}}, # if relational: type this field refer to
79
+            {"name":"rel_field_id",   "type":"INTEGER", "extra":{"foreignkey":"em_type.uid", "nullable":True, "server_default": sql.text('NULL')}}, # if relational: field that specify the rel_to_type_id
79 80
             {"name":"optional",       "type":"BOOLEAN"},
80 81
             {"name":"internal",       "type":"BOOLEAN"},
81 82
             {"name":"icon",           "type":"INTEGER"},

+ 5
- 2
Database/sqlwrapper.py View File

@@ -1,7 +1,7 @@
1 1
 # -*- coding: utf-8 -*-
2 2
 import os
3 3
 import re
4
-import logging as logger
4
+import logging
5 5
 
6 6
 import sqlalchemy as sqla
7 7
 from sqlalchemy.ext.compiler import compiles
@@ -10,7 +10,10 @@ from django.conf import settings
10 10
 from Database.sqlalter import *
11 11
 
12 12
 #Logger config
13
-logger.getLogger().setLevel('DEBUG')
13
+#logger.getLogger().setLevel('WARNING')
14
+logger = logging.getLogger('lodel2.Database.sqlwrapper')
15
+logger.setLevel('WARNING')
16
+logger.propagate = False
14 17
 #To be able to use dango confs
15 18
 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Lodel.settings")
16 19
 

+ 6
- 10
EditorialModel/classes.py View File

@@ -29,25 +29,21 @@ class EmClass(EmComponent):
29 29
     @classmethod
30 30
     def create(c, name, class_type):
31 31
         try:
32
-            exists = EmClass(name)
32
+            res = EmClass(name)
33 33
             logger.info("Trying to create an EmClass that allready exists")
34 34
         except EmComponentNotExistError:
35
-            return c._createDb(name, class_type)
35
+            res = c._createDb(name, class_type)
36
+            logger.debug("EmClass successfully created")
36 37
 
37
-        return exists
38
+        return res
38 39
 
39 40
     @classmethod
40 41
     def _createDb(c, name, class_type):
41 42
         """ Do the db querys for EmClass::create() """
43
+        uid = c.newUid()
44
+
42 45
         dbe = c.getDbE()
43
-        #Create a new uid
44
-        uids = sql.Table('uids', sqlutils.meta(dbe))
45 46
         conn = dbe.connect()
46
-        req = uids.insert(values={'table':c.table})
47
-        res = conn.execute(req)
48
-
49
-        uid = res.inserted_primary_key[0]
50
-
51 47
         #Create a new entry in the em_class table
52 48
         dbclass = sql.Table(c.table, sqlutils.meta(dbe))
53 49
         req = dbclass.insert().values(uid = uid, name=name, classtype=class_type['name'])

+ 21
- 1
EditorialModel/components.py View File

@@ -15,7 +15,8 @@ logger = logging.getLogger('Lodel2.EditorialModel')
15 15
 class EmComponent(object):
16 16
 
17 17
     dbconf = 'default' #the name of the engine configuration
18
-
18
+    table = None
19
+    
19 20
     """ instaciate an EmComponent
20 21
         @param id_or_name int|str: name or id of the object
21 22
         @exception TypeError
@@ -133,6 +134,25 @@ class EmComponent(object):
133 134
         else:
134 135
             return "<%s #%s, '%s'>" % (type(self).__name__, self.id, self.name)
135 136
 
137
+    @classmethod
138
+    def newUid(c):
139
+        """ This function register a new component in uids table
140
+            @return The new uid
141
+        """
142
+        dbe = c.getDbE()
143
+
144
+        uidtable = sql.Table('uids', sqlutils.meta(dbe))
145
+        conn = dbe.connect()
146
+        req = uidtable.insert(values={'table':c.table})
147
+        res = conn.execute(req)
148
+
149
+        uid = res.inserted_primary_key[0]
150
+        logger.debug("Registering a new UID '"+str(uid)+"' for '"+c.table+"' component")
151
+
152
+        conn.close()
153
+
154
+        return uid
155
+
136 156
 
137 157
 class EmComponentNotExistError(Exception):
138 158
     pass

+ 2
- 5
EditorialModel/fieldgroups.py View File

@@ -41,13 +41,10 @@ class EmFieldGroup(EmComponent):
41 41
     @classmethod
42 42
     def _createDb(c,name, em_class):
43 43
         """ Make the Db insertion for fieldgroup creation """
44
+        uid = c.newUid()
45
+
44 46
         dbe = c.getDbE()
45
-        #Create a new uid
46
-        uids = sql.Table('uids', sqlutils.meta(dbe))
47 47
         conn = dbe.connect()
48
-        req = uids.insert(values={'table': c.table})
49
-        res = conn.execute(req)
50
-        uid = res.inserted_primary_key
51 48
 
52 49
         req = sql.Table(c.table, sqlutils.meta(dbe)).insert().values(uid=uid, name=name, class_id=em_class.id)
53 50
         res = conn.execute(req)

+ 2
- 6
EditorialModel/fields.py View File

@@ -57,14 +57,10 @@ class EmField(EmComponent):
57 57
     
58 58
     @classmethod
59 59
     def _createDb(c, values):
60
+        values['uid'] = c.newUid()
61
+
60 62
         dbe = c.getDbE()
61
-        #Create a new uid
62
-        uids = sql.Table('uids', sqlutils.meta(dbe))
63 63
         conn = dbe.connect()
64
-        req = uids.insert(values={'table':c.table})
65
-        res = conn.execute(req)
66
-
67
-        values['uid'] = res.inserted_primary_key
68 64
         req = sql.Table(c.table, sqlutils.meta(dbe)).insert(values=values)
69 65
         res = conn.execute(req)
70 66
 

+ 2
- 6
EditorialModel/types.py View File

@@ -46,14 +46,10 @@ class EmType(EmComponent):
46 46
 
47 47
     @classmethod
48 48
     def _createDb(c, name, em_class):
49
+        uid = c.newUid()
50
+
49 51
         dbe = c.getDbE()
50
-        #Create a new uid
51
-        uids = sql.Table('uids', sqlutils.meta(dbe))
52 52
         conn = dbe.connect()
53
-        req = uids.insert(values={'table':c.table})
54
-        res = conn.execute(req)
55
-
56
-        uid = res.inserted_primary_key
57 53
 
58 54
         #Insert type in db
59 55
         dbtype = sql.Table(c.table, sqlutils.meta(dbe))

Loading…
Cancel
Save