mirror of
https://github.com/yweber/lodel2.git
synced 2026-07-10 01:10:49 +02:00
Added some class property to LeFactory generated classes to avoid Model calls
This commit is contained in:
parent
482378fb18
commit
0c51d6a483
5 changed files with 47 additions and 11 deletions
|
|
@ -56,11 +56,7 @@ class EmField(EmComponent):
|
|||
|
||||
self.fieldtype = fieldtype
|
||||
self._fieldtype_args = kwargs
|
||||
self._fieldtype_args.update({'nullable' : nullable, 'uniq' : uniq})
|
||||
#If internal is set, give it to the fieldtype
|
||||
if self.internal:
|
||||
self._fieldtype_args['internal'] = self.internal
|
||||
|
||||
self._fieldtype_args.update({'nullable' : nullable, 'uniq' : uniq, 'internal': self.internal})
|
||||
try:
|
||||
fieldtype_instance = self._fieldtype_cls(**self._fieldtype_args)
|
||||
except AttributeError as e:
|
||||
|
|
@ -86,7 +82,7 @@ class EmField(EmComponent):
|
|||
## @brief Get the fieldtype instance
|
||||
# @return a fieldtype instance
|
||||
def fieldtype_instance(self):
|
||||
return self._fieldtype_cls(self._fieldtype_args)
|
||||
return self._fieldtype_cls(**self._fieldtype_args)
|
||||
|
||||
|
||||
## @brief Return the list of relation fields for a rel_to_type
|
||||
|
|
|
|||
|
|
@ -47,6 +47,11 @@ class GenericFieldType(object):
|
|||
for argname,argvalue in kwargs.items():
|
||||
setattr(self, argname, argvalue)
|
||||
|
||||
## @return A fieldtype name from an instance
|
||||
@property
|
||||
def name(self):
|
||||
return self.__module__.split('.')[-1]
|
||||
|
||||
## @brief Check if a value is correct
|
||||
# @param value * : The value
|
||||
# @throw TypeError if not valid
|
||||
|
|
@ -68,8 +73,6 @@ class GenericFieldType(object):
|
|||
@staticmethod
|
||||
def module_name(fieldtype_name):
|
||||
return 'EditorialModel.fieldtypes.%s'%(fieldtype_name)
|
||||
|
||||
|
||||
|
||||
## @brief Transform a value into a valid python representation according to the fieldtype
|
||||
# @param value ? : The value to cast
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@ class LeClass(object):
|
|||
|
||||
## @brief Stores fieldtypes by field name
|
||||
_fieldtypes = dict()
|
||||
## @brief Stores relation with some LeType using rel2type fields. Key = rel2type fieldname value = LeType class
|
||||
_linked_types = dict()
|
||||
## @brief Stores fieldgroups and the fields they contains
|
||||
_fieldgroups = dict()
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,14 @@ from EditorialModel.fieldtypes.generic import GenericFieldType
|
|||
# The name is not good but i've no other ideas for the moment
|
||||
class LeFactory(object):
|
||||
|
||||
def __init__(self):raise NotImplementedError("Not designed (yet?) to be implemented")
|
||||
def __init__(LeFactory):raise NotImplementedError("Not designed (yet?) to be implemented")
|
||||
|
||||
## @brief Convert an EmType or EmClass name in a python class name
|
||||
# @param name str : The name
|
||||
# @return name.title()
|
||||
@staticmethod
|
||||
def name2classname(name):
|
||||
return name.title()
|
||||
|
||||
## @brief Return a call to a FieldType constructor given an EmField
|
||||
# @param emfield EmField : An EmField
|
||||
|
|
@ -57,8 +64,13 @@ class LeObject(_LeObject):\n\
|
|||
|
||||
for emclass in model.components(EditorialModel.classes.EmClass):
|
||||
cls_fields = dict()
|
||||
cls_linked_types = list()
|
||||
for field in emclass.fields():
|
||||
cls_fields[field.name] = LeFactory.fieldtype_construct_from_field(field)
|
||||
fti = field.fieldtype_instance()
|
||||
if fti.name == 'rel2type':
|
||||
#relationnal field/fieldtype
|
||||
cls_linked_types.append(LeFactory.name2classname(model.component(fti.rel_to_type_id).name))
|
||||
cls_fieldgroup = dict()
|
||||
for fieldgroup in emclass.fieldgroups():
|
||||
cls_fieldgroup[fieldgroup.name] = list()
|
||||
|
|
@ -68,21 +80,42 @@ class LeObject(_LeObject):\n\
|
|||
result += "\n\
|
||||
class %s(LeObject, LeClass):\n\
|
||||
_fieldtypes = %s\n\
|
||||
_linked_types = [%s]\n\
|
||||
_fieldgroups = %s\n\
|
||||
\n\
|
||||
"%(emclass.name.title(), cls_fields.__repr__(), cls_fieldgroup.__repr__())
|
||||
"%(
|
||||
LeFactory.name2classname(emclass.name),
|
||||
cls_fields.__repr__(),
|
||||
','.join(cls_linked_types),
|
||||
cls_fieldgroup.__repr__()
|
||||
)
|
||||
|
||||
for emtype in model.components(EditorialModel.types.EmType):
|
||||
type_fields = list()
|
||||
type_superiors = list()
|
||||
for field in emtype.fields():
|
||||
type_fields.append(field.name)
|
||||
|
||||
for nat, sup_l in emtype.superiors().items():
|
||||
type_superiors.append('%s:[%s]'%(
|
||||
nat.__repr__(),
|
||||
','.join([ LeFactory.name2classname(sup.name) for sup in sup_l])
|
||||
))
|
||||
|
||||
|
||||
result += "\n\
|
||||
class %s(%s,LeType):\n\
|
||||
_fields = %s\n\
|
||||
_superiors = {%s}\n\
|
||||
_leClass = %s\n\
|
||||
\n\
|
||||
"%(emtype.name.title(), emtype.em_class.name.title(), type_fields.__repr__(), emtype.em_class.name.title())
|
||||
"%(
|
||||
LeFactory.name2classname(emtype.name),
|
||||
LeFactory.name2classname(emtype.em_class.name),
|
||||
type_fields.__repr__(),
|
||||
','.join(type_superiors),
|
||||
LeFactory.name2classname(emtype.em_class.name)
|
||||
)
|
||||
|
||||
return result
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ class LeType(object):
|
|||
|
||||
## @brief Stores selected fields with key = name
|
||||
_fields = list()
|
||||
## @brief Allowed LeType superiors
|
||||
_superiors = list()
|
||||
## @brief Stores the class of LeClass
|
||||
_leclass = None
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue