mirror of
https://github.com/yweber/lodel2.git
synced 2026-05-10 16:15:58 +02:00
Added 2 fieldtypes : leo and naturerelation + add a uidname method to _LeCrud
This commit is contained in:
parent
9d16c6a4c8
commit
798ad5a68e
3 changed files with 61 additions and 1 deletions
34
EditorialModel/fieldtypes/leo.py
Normal file
34
EditorialModel/fieldtypes/leo.py
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
#-*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from .generic import GenericFieldType
|
||||||
|
|
||||||
|
class EmFieldType(GenericFieldType):
|
||||||
|
|
||||||
|
help = 'Fieldtypes designed to handle pk of LeObject in LeRelations'
|
||||||
|
|
||||||
|
ftype = 'leobject'
|
||||||
|
|
||||||
|
def __init__(self, superior=True, **kwargs):
|
||||||
|
super(EmFieldType, self).__init__(ftype = 'leobject', **kwargs)
|
||||||
|
|
||||||
|
def _check_data_value(self, value):
|
||||||
|
import leapi.lecrud as lecrud
|
||||||
|
err = None
|
||||||
|
if not isinstance(value, int):
|
||||||
|
if not isinstance(value, lecrud._LeCrud.name2class('LeType')):
|
||||||
|
return (None, ValueError("An instance of a child class of LeType was expected"))
|
||||||
|
if not hasattr(value, 'lodel_id'):
|
||||||
|
return (None, ValueError("The LeType instance given has no lodel_id !"))
|
||||||
|
return (value, None)
|
||||||
|
|
||||||
|
def construct_data(self, lec, fname, datas):
|
||||||
|
if isinstance(datas[fname], int):
|
||||||
|
leobject = lecrud._LeCrud.name2class('LeObject')
|
||||||
|
qfilter = '{uid_name} = {uid}'.format(
|
||||||
|
uid_name = leobject.uidname(),
|
||||||
|
uid = datas[fname],
|
||||||
|
)
|
||||||
|
|
||||||
|
return leobject.get([qfilter])
|
||||||
|
else:
|
||||||
|
return datas[fname]
|
||||||
16
EditorialModel/fieldtypes/naturerelation.py
Normal file
16
EditorialModel/fieldtypes/naturerelation.py
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
#-*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from .char import EmFieldType
|
||||||
|
|
||||||
|
import EditorialModel.classtypes as classtypes
|
||||||
|
|
||||||
|
class EmFieldType(EmFieldType):
|
||||||
|
|
||||||
|
help = 'Stores a relation\'s nature'
|
||||||
|
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
kwargs.update({'nullable': True, 'check_data_value': EmFieldType.check_data_value})
|
||||||
|
super(EmFieldType, self).__init__(**kwargs)
|
||||||
|
|
||||||
|
def check_data_value(self, value):
|
||||||
|
return value is None or ( value in classtypes.getall())
|
||||||
|
|
@ -70,6 +70,7 @@ class _LeCrud(object):
|
||||||
raise NotImplementedError("Abstract method") #child classes should return their uid fieldtype
|
raise NotImplementedError("Abstract method") #child classes should return their uid fieldtype
|
||||||
|
|
||||||
## @return A dict with fieldtypes marked as internal
|
## @return A dict with fieldtypes marked as internal
|
||||||
|
# @todo check if this method is in use, else delete it
|
||||||
@classmethod
|
@classmethod
|
||||||
def fieldtypes_internal(self):
|
def fieldtypes_internal(self):
|
||||||
return { fname: ft for fname, ft in cls.fieldtypes().items() if hasattr(ft, 'internal') and ft.internal }
|
return { fname: ft for fname, ft in cls.fieldtypes().items() if hasattr(ft, 'internal') and ft.internal }
|
||||||
|
|
@ -79,6 +80,15 @@ class _LeCrud(object):
|
||||||
def fieldlist(cls):
|
def fieldlist(cls):
|
||||||
return cls.fieldtypes().keys()
|
return cls.fieldtypes().keys()
|
||||||
|
|
||||||
|
## @return The name of the uniq id field
|
||||||
|
# @todo test for abstract method !!!
|
||||||
|
@classmethod
|
||||||
|
def uidname(cls):
|
||||||
|
if len(cls._uid_fieldtype) == 0:
|
||||||
|
raise NotImplementedError("Abstract method uid_name for %s!"%cls.__name__)
|
||||||
|
return list(cls._uid_fieldtype.keys())[0]
|
||||||
|
|
||||||
|
|
||||||
## @brief Returns object datas
|
## @brief Returns object datas
|
||||||
# @param
|
# @param
|
||||||
# @return a dict of fieldname : value
|
# @return a dict of fieldname : value
|
||||||
|
|
@ -222,7 +232,7 @@ class _LeCrud(object):
|
||||||
# @warning assert that the uid is not composed with multiple fieldtypes
|
# @warning assert that the uid is not composed with multiple fieldtypes
|
||||||
# @return A filter of the form tuple(UID, '=', self.UID)
|
# @return A filter of the form tuple(UID, '=', self.UID)
|
||||||
def _id_filter(self):
|
def _id_filter(self):
|
||||||
id_name = list(self._uid_fieldtype.keys())[0]
|
id_name = self.uidname()
|
||||||
return ( id_name, '=', getattr(self, id_name) )
|
return ( id_name, '=', getattr(self, id_name) )
|
||||||
|
|
||||||
## @brief Construct datas values
|
## @brief Construct datas values
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue