1
0
Fork 0
mirror of https://github.com/yweber/lodel2.git synced 2026-07-10 09:20:49 +02:00

New version of EmFieldtype

Now fieldtypes are not derivated from EmField anymore
This commit is contained in:
Yann 2015-10-14 14:05:36 +02:00
commit 5cd8e140bd
9 changed files with 105 additions and 57 deletions

View file

@ -1,16 +1,13 @@
#-*- coding: utf-8 -*-
from EditorialModel.fields import EmField
from EditorialModel.fieldtypes.generic import GenericFieldType
class EmFieldBool(GenericFieldType):
class EmFieldBool(EmField):
ftype = 'bool'
help = 'A basic boolean field'
## @brief A char field
# @brief max_length int : The maximum length of this field
def __init__(self, **kwargs):
super(EmFieldBool, self).__init__(**kwargs)
super(EmFieldBool, self).__init__(ftype='bool',**kwargs)
fclass = EmFieldBool

View file

@ -1,17 +1,14 @@
#-*- coding: utf-8 -*-
from EditorialModel.fields import EmField
from EditorialModel.fieldtypes.generic import GenericFieldType
class EmFieldChar(GenericFieldType):
class EmFieldChar(EmField):
ftype = 'char'
help = 'Basic string (varchar) field. Take max_length=64 as option'
## @brief A char field
# @brief max_length int : The maximum length of this field
def __init__(self, max_length=64, **kwargs):
self.max_length = max_length
super(EmFieldChar, self).__init__(**kwargs)
super(EmFieldChar, self).__init__(ftype = 'char', **kwargs)
fclass = EmFieldChar

View file

@ -5,8 +5,6 @@ from EditorialModel.fields import EmField
class EmFieldDatetime(EmField):
ftype = 'datetime'
help = 'A datetime field. Take two boolean options now_on_update and now_on_create'
## @brief A datetime field
@ -15,6 +13,5 @@ class EmFieldDatetime(EmField):
def __init__(self, now_on_update=False, now_on_create=False, **kwargs):
self.now_on_update = now_on_update
self.now_on_create = now_on_create
super(EmFieldDatetime, self).__init__(**kwargs)
super(EmFieldDatetime, self).__init__(ftype='datetime',**kwargs)
fclass = EmFieldDatetime

View file

@ -5,13 +5,11 @@ from EditorialModel.fields import EmField
class EmFieldFile(EmField):
ftype = 'file'
help = 'A file field. With one options upload_path'
## @brief A char field
# @brief max_length int : The maximum length of this field
def __init__(self, upload_path=None, **kwargs):
self.upload_path = upload_path
super(EmFieldFile, self).__init__(**kwargs)
super(EmFieldFile, self).__init__(ftype='char',**kwargs)
fclass = EmFieldFile

View file

@ -0,0 +1,82 @@
#-*- coding: utf-8 -*-
import types
## @brief Abstract class representing a fieldtype
class GenericFieldType(object):
## @brief Text describing the fieldtype
help = 'Generic field type : abstract class for every fieldtype'
## @brief Allowed type for handled datas
_allowed_ftype = ['char', 'str', 'int', 'bool', 'datetime', 'text', 'rel2type']
## @brief Instanciate a new fieldtype
# @param ftype str : The type of datas handled by this fieldtype
# @param default ? : The default value
# @param nullable bool : is None allowed as value ?
# @param check_function function : A callback check function that takes 1 argument and raise a TypeError if the validation fails
# @param **kwargs dict : Other arguments
# @throw NotImplementedError if called directly
# @throw AttributeError if bad ftype
# @throw AttributeError if bad check_function
def __init__(self, ftype, default = None, nullable = False, check_function = None, **kwargs):
if self.__class__ == GenericFieldType:
raise NotImplementedError("Abstract class")
if ftype not in self._allowed_ftype:
raise AttributeError("Ftype '%s' not known"%ftype)
if check_function is None:
check_function = self.dummy_check
elif not isinstance(check_function, types.FunctionType):
raise AttributeError("check_function argument has to be a function")
self.ftype = ftype
self.check_function = check_function
self.nullalble = bool(nullable)
self.check_or_raise(default)
self.default = default
for argname,argvalue in kwargs.items():
setattr(self, argname, argvalue)
## @brief Check if a value is correct
# @param value * : The value
# @throw TypeError if not valid
@staticmethod
def dummy_check(value):
pass
## @brief Transform a value into a valid python representation according to the fieldtype
# @param value ? : The value to cast
# @param kwargs dict : optionnal cast arguments
# @return Something (depending on the fieldtype
# @throw AttributeError if error in argument given to the method
# @throw TypeError if the cast is not possible
def cast(self, value, **kwargs):
if len(kwargs) > 0:
raise AttributeError("No optionnal argument allowed for %s cast method"%self.__class__.__name__)
return value
## @brief Check if a value is correct
# @param value * : The value to check
# @return True if valid else False
def check(self, value):
try:
self.check_or_raise(value)
except TypeError as e:
return False
return True
## @brief Check if a value is correct
# @param value * : The value
# @throw TypeError if not valid
def check_or_raise(self, value):
if value is None and not self.nullable:
raise TypeError("Not nullable field")
self.check_function(value)
class FieldTypeError(Exception):
pass

View file

@ -1,15 +1,13 @@
#-*- coding: utf-8 -*-
from EditorialModel.fields import EmField
from EditorialModel.fieldtypes import GenericFieldType
class EmFieldInt(EmField):
ftype = 'int'
class EmFieldInt(GenericFieldType):
help = 'Basic integer field'
def __init__(self, **kwargs):
super(EmFieldInt, self).__init__(**kwargs)
super(EmFieldInt, self).__init__(ftype='int',**kwargs)
fclass = EmFieldInt

View file

@ -1,7 +1,7 @@
#-*- coding: utf-8 -*-
import re
from EditorialModel.fieldtypes.char import EmFieldChar
from EditorialModel.fieldtypes.generic import GenericFieldType
class EmFieldCharRegex(EmFieldChar):
@ -14,22 +14,9 @@ class EmFieldCharRegex(EmFieldChar):
def __init__(self, regex='', **kwargs):
self.regex = regex
v_re = re.compile(regex) # trigger an error if invalid regex
def re_match(value):
if not v_re.match(regex, value):
raise TypeError('"%s" don\'t match the regex "%s"'%(value, regex))
super(EmFieldCharRegex, self).__init__(check_function=re_match,**kwargs)
super(EmFieldCharRegex, self).__init__(**kwargs)
def validation_function(self, raise_e=None, ret_valid=None, ret_invalid=None):
super(EmFieldChar, self).validation_function(raise_e, ret_valid, ret_invalid)
if not raise_e is None:
def v_fun(value):
if not re.match(self.regex):
raise raise_e
else:
def v_fun(value):
if not re.match(self.regex):
return ret_invalid
else:
return ret_valid
return v_fun
fclass = EmFieldCharRegex

View file

@ -1,17 +1,14 @@
#-*- coding: utf-8 -*-
from EditorialModel.fields import EmField
from EditorialModel.fieldtypes.generic import GenericFieldType
class EmFieldRel2Type(EmField):
ftype = 'rel2type'
class EmFieldRel2Type(GenericFieldType):
help = 'Relationnal field (relation2type). Take rel_to_type_id as option (an EmType uid)'
def __init__(self, rel_to_type_id, **kwargs):
self.rel_to_type_id = rel_to_type_id
super(EmFieldRel2Type, self).__init__(**kwargs)
super(EmFieldRel2Type, self).__init__(ftype='rel2type',**kwargs)
def get_related_type(self):
return self.model.component(self.rel_to_type_id)
@ -19,5 +16,3 @@ class EmFieldRel2Type(EmField):
def get_related_fields(self):
return [f for f in self.model.components(EmField) if f.rel_field_id == self.uid]
fclass = EmFieldRel2Type

View file

@ -1,14 +1,11 @@
#-*- coding: utf-8 -*-
from EditorialModel.fields import EmField
from EditorialModel.fieldtypes.generic import GenericFieldType
class EmFieldText(GenericFieldType):
class EmFieldText(EmField):
ftype = 'text'
help = 'A text field (big string)'
def __init__(self, **kwargs):
super(EmFieldText, self).__init__(**kwargs)
super(EmFieldText, self).__init__(ftype='text',**kwargs)
fclass = EmFieldText