mirror of
https://github.com/yweber/lodel2.git
synced 2026-03-22 02:52:02 +01:00
Updated fieldtypes to make easier the check_data_value inheritance
This commit is contained in:
parent
9cc1e8ab96
commit
df40d5f17d
3 changed files with 4 additions and 12 deletions
|
|
@ -37,16 +37,10 @@ class GenericFieldType(object):
|
||||||
if ftype not in self._allowed_ftype:
|
if ftype not in self._allowed_ftype:
|
||||||
raise AttributeError("Ftype '%s' not known" % ftype)
|
raise AttributeError("Ftype '%s' not known" % ftype)
|
||||||
|
|
||||||
if check_data_value is None:
|
|
||||||
check_data_value = self.dummy_check_data_value
|
|
||||||
elif not callable(check_data_value):
|
|
||||||
raise AttributeError("check_data_value argument has to be a function it is a %s" % type(check_data_value))
|
|
||||||
|
|
||||||
if ftype != self.__class__.ftype:
|
if ftype != self.__class__.ftype:
|
||||||
raise RuntimeError("The ftype is not the same for the instance and the class. Maybe %s reimplement ftype at class level but shouldn't" % self.name)
|
raise RuntimeError("The ftype is not the same for the instance and the class. Maybe %s reimplement ftype at class level but shouldn't" % self.name)
|
||||||
|
|
||||||
self.ftype = ftype
|
self.ftype = ftype
|
||||||
self._check_data_value = check_data_value
|
|
||||||
self.nullable = bool(nullable)
|
self.nullable = bool(nullable)
|
||||||
self.uniq = bool(uniq)
|
self.uniq = bool(uniq)
|
||||||
|
|
||||||
|
|
@ -92,10 +86,10 @@ class GenericFieldType(object):
|
||||||
def check_data_consistency(self, lec, fname, datas):
|
def check_data_consistency(self, lec, fname, datas):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
## @brief Check if a value is correct
|
## @brief Dummy check, designed to be implemented in child classes
|
||||||
# @param value * : The value
|
# @param value * : The value
|
||||||
# @return (checked_and_casted_value, Exception|None)
|
# @return (checked_and_casted_value, Exception|None)
|
||||||
def dummy_check_data_value(self, value):
|
def _check_data_value(self, value):
|
||||||
return (value, None)
|
return (value, None)
|
||||||
|
|
||||||
## @brief Given a fieldtype name return the associated python class
|
## @brief Given a fieldtype name return the associated python class
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,9 @@ class EmFieldType(GenericFieldType):
|
||||||
ftype = 'int'
|
ftype = 'int'
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
if 'check_data_value' not in kwargs:
|
|
||||||
kwargs['check_data_value'] = self.check_value
|
|
||||||
super(EmFieldType, self).__init__(ftype='int', **kwargs)
|
super(EmFieldType, self).__init__(ftype='int', **kwargs)
|
||||||
|
|
||||||
def check_value(self, value):
|
def _check_data_value(self, value):
|
||||||
error = None
|
error = None
|
||||||
try:
|
try:
|
||||||
value = int(value)
|
value = int(value)
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ class EmFieldType(char.EmFieldType):
|
||||||
|
|
||||||
super(EmFieldType, self).__init__(check_data_value=check_value, max_length=max_length, **kwargs)
|
super(EmFieldType, self).__init__(check_data_value=check_value, max_length=max_length, **kwargs)
|
||||||
|
|
||||||
def check_value(value):
|
def _check_data_value(self,value):
|
||||||
error = None
|
error = None
|
||||||
if not self.compiled_re.match(value):
|
if not self.compiled_re.match(value):
|
||||||
value = ''
|
value = ''
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue