mirror of
https://github.com/yweber/lodel2.git
synced 2026-06-14 06:20:48 +02:00
Code factorisation for new UID creation
Added a classmethod EmComponent::newUid() that determines the caller's class and return a newly registrated UID
This commit is contained in:
parent
c4e1afd192
commit
f958efbff5
6 changed files with 41 additions and 33 deletions
|
|
@ -1,7 +1,7 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import os
|
||||
import re
|
||||
import logging as logger
|
||||
import logging
|
||||
|
||||
import sqlalchemy as sqla
|
||||
from sqlalchemy.ext.compiler import compiles
|
||||
|
|
@ -10,7 +10,10 @@ from django.conf import settings
|
|||
from Database.sqlalter import *
|
||||
|
||||
#Logger config
|
||||
logger.getLogger().setLevel('DEBUG')
|
||||
#logger.getLogger().setLevel('WARNING')
|
||||
logger = logging.getLogger('lodel2.Database.sqlwrapper')
|
||||
logger.setLevel('WARNING')
|
||||
logger.propagate = False
|
||||
#To be able to use dango confs
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Lodel.settings")
|
||||
|
||||
|
|
|
|||
|
|
@ -29,25 +29,21 @@ class EmClass(EmComponent):
|
|||
@classmethod
|
||||
def create(c, name, class_type):
|
||||
try:
|
||||
exists = EmClass(name)
|
||||
res = EmClass(name)
|
||||
logger.info("Trying to create an EmClass that allready exists")
|
||||
except EmComponentNotExistError:
|
||||
return c._createDb(name, class_type)
|
||||
res = c._createDb(name, class_type)
|
||||
logger.debug("EmClass successfully created")
|
||||
|
||||
return exists
|
||||
return res
|
||||
|
||||
@classmethod
|
||||
def _createDb(c, name, class_type):
|
||||
""" Do the db querys for EmClass::create() """
|
||||
dbe = c.getDbE()
|
||||
#Create a new uid
|
||||
uids = sql.Table('uids', sqlutils.meta(dbe))
|
||||
conn = dbe.connect()
|
||||
req = uids.insert(values={'table':c.table})
|
||||
res = conn.execute(req)
|
||||
|
||||
uid = res.inserted_primary_key[0]
|
||||
uid = c.newUid()
|
||||
|
||||
dbe = c.getDbE()
|
||||
conn = dbe.connect()
|
||||
#Create a new entry in the em_class table
|
||||
dbclass = sql.Table(c.table, sqlutils.meta(dbe))
|
||||
req = dbclass.insert().values(uid = uid, name=name, classtype=class_type['name'])
|
||||
|
|
|
|||
|
|
@ -15,7 +15,8 @@ logger = logging.getLogger('Lodel2.EditorialModel')
|
|||
class EmComponent(object):
|
||||
|
||||
dbconf = 'default' #the name of the engine configuration
|
||||
|
||||
table = None
|
||||
|
||||
""" instaciate an EmComponent
|
||||
@param id_or_name int|str: name or id of the object
|
||||
@exception TypeError
|
||||
|
|
@ -133,6 +134,25 @@ class EmComponent(object):
|
|||
else:
|
||||
return "<%s #%s, '%s'>" % (type(self).__name__, self.id, self.name)
|
||||
|
||||
@classmethod
|
||||
def newUid(c):
|
||||
""" This function register a new component in uids table
|
||||
@return The new uid
|
||||
"""
|
||||
dbe = c.getDbE()
|
||||
|
||||
uidtable = sql.Table('uids', sqlutils.meta(dbe))
|
||||
conn = dbe.connect()
|
||||
req = uidtable.insert(values={'table':c.table})
|
||||
res = conn.execute(req)
|
||||
|
||||
uid = res.inserted_primary_key[0]
|
||||
logger.debug("Registering a new UID '"+str(uid)+"' for '"+c.table+"' component")
|
||||
|
||||
conn.close()
|
||||
|
||||
return uid
|
||||
|
||||
|
||||
class EmComponentNotExistError(Exception):
|
||||
pass
|
||||
|
|
|
|||
|
|
@ -41,13 +41,10 @@ class EmFieldGroup(EmComponent):
|
|||
@classmethod
|
||||
def _createDb(c,name, em_class):
|
||||
""" Make the Db insertion for fieldgroup creation """
|
||||
uid = c.newUid()
|
||||
|
||||
dbe = c.getDbE()
|
||||
#Create a new uid
|
||||
uids = sql.Table('uids', sqlutils.meta(dbe))
|
||||
conn = dbe.connect()
|
||||
req = uids.insert(values={'table': c.table})
|
||||
res = conn.execute(req)
|
||||
uid = res.inserted_primary_key
|
||||
|
||||
req = sql.Table(c.table, sqlutils.meta(dbe)).insert().values(uid=uid, name=name, class_id=em_class.id)
|
||||
res = conn.execute(req)
|
||||
|
|
|
|||
|
|
@ -57,14 +57,10 @@ class EmField(EmComponent):
|
|||
|
||||
@classmethod
|
||||
def _createDb(c, values):
|
||||
dbe = c.getDbE()
|
||||
#Create a new uid
|
||||
uids = sql.Table('uids', sqlutils.meta(dbe))
|
||||
conn = dbe.connect()
|
||||
req = uids.insert(values={'table':c.table})
|
||||
res = conn.execute(req)
|
||||
values['uid'] = c.newUid()
|
||||
|
||||
values['uid'] = res.inserted_primary_key
|
||||
dbe = c.getDbE()
|
||||
conn = dbe.connect()
|
||||
req = sql.Table(c.table, sqlutils.meta(dbe)).insert(values=values)
|
||||
res = conn.execute(req)
|
||||
|
||||
|
|
|
|||
|
|
@ -46,14 +46,10 @@ class EmType(EmComponent):
|
|||
|
||||
@classmethod
|
||||
def _createDb(c, name, em_class):
|
||||
dbe = c.getDbE()
|
||||
#Create a new uid
|
||||
uids = sql.Table('uids', sqlutils.meta(dbe))
|
||||
conn = dbe.connect()
|
||||
req = uids.insert(values={'table':c.table})
|
||||
res = conn.execute(req)
|
||||
uid = c.newUid()
|
||||
|
||||
uid = res.inserted_primary_key
|
||||
dbe = c.getDbE()
|
||||
conn = dbe.connect()
|
||||
|
||||
#Insert type in db
|
||||
dbtype = sql.Table(c.table, sqlutils.meta(dbe))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue