mirror of
https://github.com/yweber/lodel2.git
synced 2026-07-08 00:20:48 +02:00
LeCrud: is_letype() and is_leclass() helper functions + use it in LeDataSourceSQL
This commit is contained in:
parent
a5efcdbfd8
commit
142f2ff3f5
2 changed files with 30 additions and 17 deletions
|
|
@ -51,7 +51,7 @@ class LeDataSourceSQL(DummyDatasource):
|
|||
fields = [(main_table, common_fields)]
|
||||
|
||||
# it is a LeType or a LeClass, query on main table left join class table on lodel_id
|
||||
elif hasattr(target_cls, '_leclass') or hasattr(target_cls, '_class_id'):
|
||||
elif target_cls.is_letype() or target_cls.is_leclass():
|
||||
# find main table and main table datas
|
||||
main_table = self.datasource_utils.objects_table_name
|
||||
main_class = target_cls._leclass if hasattr(target_cls, '_leclass') else target_cls
|
||||
|
|
@ -170,22 +170,24 @@ class LeDataSourceSQL(DummyDatasource):
|
|||
# @todo should work with LeType, LeClass, and Relations
|
||||
def insert(self, target_cls, **datas):
|
||||
# it is a LeType
|
||||
if (hasattr(target_cls, '_leclass')):
|
||||
# find main table and main table datas
|
||||
main_table = self.datasource_utils.objects_table_name
|
||||
main_datas = {'class_id':target_cls._leclass._class_id, 'type_id':target_cls._type_id}
|
||||
for main_column_name in common_fields:
|
||||
if main_column_name in datas:
|
||||
main_datas[main_column_name] = datas[main_column_name]
|
||||
unset(datas[main_column_name])
|
||||
if not target_cls.is_letype():
|
||||
raise AttributeError("'%s' is not a LeType, it is not possible to insert it" % target_cls)
|
||||
|
||||
cur = self.datasource_utils.query(self.connection, insert(main_table, main_datas))
|
||||
lodel_id = cur.lastrowid
|
||||
# find main table and main table datas
|
||||
main_table = self.datasource_utils.objects_table_name
|
||||
main_datas = {'class_id':target_cls._leclass._class_id, 'type_id':target_cls._type_id}
|
||||
for main_column_name in common_fields:
|
||||
if main_column_name in datas:
|
||||
main_datas[main_column_name] = datas[main_column_name]
|
||||
unset(datas[main_column_name])
|
||||
|
||||
# insert in class_table
|
||||
datas[self.datasource_utils.field_lodel_id] = lodel_id
|
||||
class_table = self.datasource_utils.get_table_name_from_class(target_cls._leclass.__name__)
|
||||
self.datasource_utils.query(self.connection, insert(class_table, datas))
|
||||
cur = self.datasource_utils.query(self.connection, insert(main_table, main_datas))
|
||||
lodel_id = cur.lastrowid
|
||||
|
||||
# insert in class_table
|
||||
datas[self.datasource_utils.field_lodel_id] = lodel_id
|
||||
class_table = self.datasource_utils.get_table_name_from_class(target_cls._leclass.__name__)
|
||||
self.datasource_utils.query(self.connection, insert(class_table, datas))
|
||||
|
||||
return lodel_id
|
||||
|
||||
|
|
|
|||
|
|
@ -87,8 +87,19 @@ class _LeCrud(object):
|
|||
if len(cls._uid_fieldtype) == 0:
|
||||
raise NotImplementedError("Abstract method uid_name for %s!"%cls.__name__)
|
||||
return list(cls._uid_fieldtype.keys())[0]
|
||||
|
||||
|
||||
|
||||
## @return maybe Bool: True if cls is a LeType or an instance of LeType
|
||||
# @param cls Class: a Class or instanciated object
|
||||
@classmethod
|
||||
def is_letype(cls):
|
||||
return hasattr(cls, '_leclass')
|
||||
|
||||
## @return maybe Bool: True if cls is a LeClass or an instance of LeClass
|
||||
# @param cls Class: a Class or instanciated object
|
||||
@classmethod
|
||||
def is_leclass(cls):
|
||||
return hasattr(cls, '_class_id') and not cls.is_letype()
|
||||
|
||||
## @brief Returns object datas
|
||||
# @param
|
||||
# @return a dict of fieldname : value
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue