mirror of
https://github.com/yweber/lodel2.git
synced 2025-11-14 18:09:17 +01:00
Added type check in EmComponent and childs class type check. Added some cast functions to Backend
This commit is contained in:
parent
d6af3aaf46
commit
405dd318dd
6 changed files with 77 additions and 2 deletions
|
|
@ -4,7 +4,20 @@
|
|||
# Load representation of an EditorialModel from a json file
|
||||
|
||||
import json
|
||||
import datetime
|
||||
from Lodel.utils.mlstring import MlString
|
||||
|
||||
def date_cast(date):
|
||||
if len(date):
|
||||
return datetime.datetime(date)
|
||||
else:
|
||||
return None
|
||||
|
||||
def int_or_none(i):
|
||||
if len(i):
|
||||
return int(i)
|
||||
else:
|
||||
return None
|
||||
|
||||
## Manages a Json file based backend structure
|
||||
class EmBackendJson(object):
|
||||
|
|
@ -13,9 +26,19 @@ class EmBackendJson(object):
|
|||
'uid' : int,
|
||||
'rank' : int,
|
||||
'class_id' : int,
|
||||
'fieldgroup_id' : int
|
||||
'fieldgroup_id' : int,
|
||||
'rel_to_type_id' : int_or_none,
|
||||
'rel_field_id' : int_or_none,
|
||||
'optional' : bool,
|
||||
'internal': bool,
|
||||
'string' : MlString.load,
|
||||
'help_text' : MlString.load,
|
||||
'date_create' : date_cast,
|
||||
'date_update' : date_cast,
|
||||
}
|
||||
|
||||
|
||||
|
||||
## Constructor
|
||||
#
|
||||
# @param json_file str: path to the json_file used as data source
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
# @see EditorialModel::classes::EmClass
|
||||
|
||||
from EditorialModel.components import EmComponent
|
||||
from EditorialModel.classtypes import EmClassType
|
||||
import EditorialModel.fieldtypes as ftypes
|
||||
import EditorialModel
|
||||
|
||||
|
|
@ -25,7 +26,15 @@ class EmClass(EmComponent):
|
|||
]
|
||||
|
||||
## EmClass instanciation
|
||||
# @todo Classtype initialisation and test is not good EmClassType should give an answer or something like that
|
||||
# @todo defines types check for icon and sortcolumn
|
||||
def __init__(self, model, uid, name, classtype, icon = '0', sortcolumn = 'rank', string = None, help_text = None, date_update = None, date_create = None, rank = None):
|
||||
|
||||
try:
|
||||
getattr(EmClassType, classtype)
|
||||
except AttributeError:
|
||||
raise AttributeError("Unknown classtype '%s'" %classtype)
|
||||
|
||||
self.classtype = classtype
|
||||
self.icon = icon
|
||||
self.sortcolumn = 'rank'
|
||||
|
|
|
|||
|
|
@ -27,16 +27,35 @@ class EmComponent(object):
|
|||
if type(self) == EmComponent:
|
||||
raise NotImplementedError('Abstract class')
|
||||
|
||||
self.model = model
|
||||
self.model = model # AHAH cannot check type without importing Model ?
|
||||
|
||||
self.uid = uid
|
||||
self.check_type('uid', int)
|
||||
self.name = name
|
||||
self.check_type('name', str)
|
||||
self.string = MlString() if string is None else string
|
||||
self.check_type('string', MlString)
|
||||
self.help_text = MlString() if help_text is None else help_text
|
||||
self.check_type('help_text', MlString)
|
||||
self.date_update = datetime.datetime.now() if date_update is None else date_update #WARNING timezone !
|
||||
self.check_type('date_update', datetime.datetime)
|
||||
self.date_create = datetime.datetime.now() if date_create is None else date_create #WARNING timezone !
|
||||
self.check_type('date_create', datetime.datetime)
|
||||
|
||||
#Handling specials ranks for component creation
|
||||
self.rank = rank
|
||||
|
||||
pass
|
||||
|
||||
## Check the type of attribute named var_name
|
||||
# @param var_name str : the attribute name
|
||||
# @param excepted_type tuple|type : Tuple of type or a type
|
||||
# @throw AttributeError if wrong type detected
|
||||
def check_type(self, var_name, excepted_type):
|
||||
var = getattr(self, var_name)
|
||||
|
||||
if not isinstance(var, excepted_type):
|
||||
raise AttributeError("Excepted %s to be an %s but got %s instead" % (var_name, str(excepted_type), str(type(var))) )
|
||||
pass
|
||||
|
||||
## @brief Hash function that allows to compare two EmComponent
|
||||
|
|
|
|||
|
|
@ -15,7 +15,9 @@ class EmFieldGroup(EmComponent):
|
|||
|
||||
## EmFieldGroup instanciation
|
||||
def __init__(self, model, uid, name, class_id, string = None, help_text = None, date_update = None, date_create = None, rank = None):
|
||||
|
||||
self.class_id = class_id
|
||||
self.check_type('class_id', int)
|
||||
super(EmFieldGroup, self).__init__(model=model, uid=uid, name=name, string=string, help_text=help_text, date_update=date_update, date_create=date_create, rank=rank)
|
||||
pass
|
||||
|
||||
|
|
|
|||
|
|
@ -11,13 +11,21 @@ class EmField(EmComponent):
|
|||
|
||||
ranked_in = 'fieldgroup_id'
|
||||
|
||||
## Instanciate a new EmField
|
||||
# @todo define and test type for icon and fieldtype
|
||||
def __init__(self, model, uid, name, fieldgroup_id, fieldtype, optional = False, internal = False, rel_to_type_id = None, rel_field_id = None, icon = '0', string = None, help_text = None, date_update = None, date_create = None, rank = None):
|
||||
|
||||
self.fieldgroup_id = fieldgroup_id
|
||||
self.check_type('fieldgroup_id', int)
|
||||
self.fieldtype = fieldtype
|
||||
self.optional = optional
|
||||
self.check_type('optional', bool)
|
||||
self.internal = internal
|
||||
self.check_type('internal', bool)
|
||||
self.rel_to_type_id = rel_to_type_id
|
||||
self.check_type('rel_to_type_id', (int, type(None)))
|
||||
self.rel_field_id = rel_field_id
|
||||
self.check_type('rel_field_id', (int, type(None)))
|
||||
self.icon = icon
|
||||
super(EmField, self).__init__(model=model, uid=uid, name=name, string=string, help_text=help_text, date_update=date_update, date_create=date_create, rank=rank)
|
||||
|
||||
|
|
|
|||
|
|
@ -22,10 +22,24 @@ import EditorialModel.classes
|
|||
class EmType(EmComponent):
|
||||
ranked_in = 'class_id'
|
||||
|
||||
## Instanciate a new EmType
|
||||
# @todo define and check types for icon and sortcolumn
|
||||
# @todo better check self.subordinates
|
||||
def __init__(self, model, uid, name, class_id, selected_fields = [], subordinates = [], icon = '0', sortcolumn = 'rank', string = None, help_text = None, date_update = None, date_create = None, rank = None):
|
||||
self.class_id = class_id
|
||||
self.check_type('class_id', int)
|
||||
self.selected_fields = selected_fields
|
||||
self.check_type('selected_fields', list)
|
||||
for l_uid in self.selected_fields:
|
||||
if not isinstance(l_uid, int):
|
||||
raise AttributeError("Excepted selected_fields to be a list of integers, but found a +"+str(type(l_uid))+" in it")
|
||||
|
||||
self.subordinates = subordinates
|
||||
self.check_type('subordinates', list)
|
||||
for l_uid in self.subordinates:
|
||||
if not isinstance(l_uid[0], str) or not isinstance(l_uid[1], int):
|
||||
raise AttributeError("Excepted selected_fields to be a list of nature,int !")
|
||||
|
||||
self.icon = icon
|
||||
self.sortcolumn = sortcolumn
|
||||
super(EmType, self).__init__(model=model, uid=uid, name=name, string=string, help_text=help_text, date_update=date_update, date_create=date_create, rank=rank)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue