1
0
Fork 0
mirror of https://github.com/yweber/lodel2.git synced 2025-11-25 23:06:55 +01:00

New way to instanciate LeCrud child classes

Note : This commit is not complete. It brakes the partial instanciation of classes etc.
This commit is contained in:
Yann 2016-01-07 13:19:33 +01:00
commit 0b3f2bae1a
5 changed files with 38 additions and 37 deletions

View file

@ -19,12 +19,6 @@ class _LeClass(_LeObject):
## @brief Stores the classtype
_classtype = None
## @brief Instanciate a new LeClass
# @note Abstract method
# @param **kwargs
def __init__(self, lodel_id, **kwargs):
_LeObject.__init__(self, lodel_id, **kwargs)
@classmethod
def fieldtypes(cls):
ret = dict()

View file

@ -55,8 +55,41 @@ class _LeCrud(object):
## @brief Stores Query filters operators
_query_operators = ['=', '<=', '>=', '!=', '<', '>', ' in ', ' not in ', ' like ', ' not like ']
def __init__(self):
raise NotImplementedError("Abstract class")
## @brief Asbtract constructor for every child classes
# @param uid int : lodel_id if LeObject, id_relation if its a LeRelation
# @param **kwargs : datas !
# @raise NotImplementedError if trying to instanciate a class that cannot be instanciated
def __init__(self, uid, **kwargs):
if not self.implements_leobject() and not self.implements_lerelation():
raise NotImplementedError("Abstract class !")
# Try to get the name of the uid field (lodel_id for objects, id_relation for relations)
try:
uid_name = self.uidname()
except NotImplementedError: #Should never append
raise NotImplementedError("Abstract class !")
# Checking uid value
uid, err = self._uid_fieldtype[uid_name].check_data_value(uid)
if isinstance(err, Exception):
raise err
setattr(self, uid_name, uid)
# Populating the object with given datas
errors = dict()
for name, value in kwargs.items():
if name not in self.fieldlist():
errors[name] = AttributeError("No such field '%s' for %s"%(name, self.__class__.__name__))
else:
cvalue, err = self.fieldtypes()[name].check_data_value(value)
if isinstance(err, Exception):
errors[name] = err
else:
setattr(self, name, cvalue)
if len(errors) > 0:
raise LeApiDataCheckError("Invalid arguments given to constructor", errors)
## @brief Given a dynamically generated class name return the corresponding python Class
# @param name str : a concrete class name

View file

@ -33,16 +33,6 @@ class _LeObject(_LeCrud):
## @brief Stores the names of the fields storing the EM class uid and EM type uid
_me_uid_field_names = (None, None)
## @brief Instanciate a partial LeObject with a lodel_id
# @note use the get_instance method to fetch datas and instanciate a concret LeObject
def __init__(self, lodel_id):
#Warning ! Handles only single pk
uid_fname, uid_ft = list(self._uid_fieldtype.items())[0]
new_id, err = uid_ft.check_data_value(lodel_id)
if not (err is None):
raise err
setattr(self, uid_fname, lodel_id)
## @return Corresponding populated LeObject
def get_instance(self):
uid_fname = self.uidname()

View file

@ -17,7 +17,7 @@ class _LeRelation(lecrud._LeCrud):
_rel_fieldtypes = dict()
def __init__(self, id_relation, **kwargs):
self.id_relation = id_relation
super().__init__(id_relation, **kwargs)
## @brief Forge a filter to match the superior
@classmethod

View file

@ -35,30 +35,14 @@ class _LeType(_LeClass):
if self._leclass is None:
raise NotImplementedError("Abstract class")
self.lodel_id, err = self._uid_fieldtype['lodel_id'].check_data_value(lodel_id)
if isinstance(err, Exception):
raise err
if 'type_id' in kwargs:
if self.__class__._type_id != int(kwargs['type_id']):
raise RuntimeError("Trying to instanciate a %s with an type_id that is not correct"%self.__class__.__name__)
if 'class_id' in kwargs:
if self.__class__._class_id != int(kwargs['class_id']):
raise RuntimeError("Trying to instanciate a %s with a clas_id that is not correct"%self.__class__.__name__)
## Populate the object from the datas received in kwargs
err_l = dict()
for name, value in kwargs.items():
if name not in self._fields:
err_l[name] = AttributeError("No such field '%s' for %s"%(name, self.__class__.__name__))
else:
cvalue, err = self.fieldtypes()[name].check_data_value(value)
if isinstance(err, Exception):
err_l[name] = err
else:
setattr(self, name, value)
if len(err_l) > 0:
raise LeApiDataCheckError("Invalid arguments given to constructor", err_l)
super().__init__(lodel_id, **kwargs)
@classmethod
def leo_class(cls):