1
0
Fork 0
mirror of https://github.com/yweber/lodel2.git synced 2026-08-05 11:58:37 +02:00

Update the _LeObject.get() method to handle relationnal filters

This commit is contained in:
Yann 2015-10-16 16:22:10 +02:00
commit 6123d23be4
3 changed files with 69 additions and 49 deletions

View file

@ -1,5 +1,7 @@
#-*- coding: utf-8 -*-
import importlib
import EditorialModel
from EditorialModel.model import Model
from EditorialModel.fieldtypes.generic import GenericFieldType
@ -9,8 +11,21 @@ from EditorialModel.fieldtypes.generic import GenericFieldType
# The name is not good but i've no other ideas for the moment
class LeFactory(object):
output_file = 'dyn.py'
def __init__(LeFactory):raise NotImplementedError("Not designed (yet?) to be implemented")
## @brief Return a LeObject child class given its name
# @return a python class or False
@staticmethod
def leobj_from_name(name):
mod = importlib.import_module('leobject.'+LeFactory.output_file.split('.')[-1])
try:
res = getattr(mod, name)
except AttributeError:
return False
return res
## @brief Convert an EmType or EmClass name in a python class name
# @param name str : The name
# @return name.title()

View file

@ -70,8 +70,9 @@ class _LeObject(object):
return okay
## @brief make a search to retrieve a collection of LeObject
# @param
# @param query_filters list : list of string of query filters (or tuple (FIELD, OPERATOR, VALUE) )
# @param typename str : The name of the LeType we want
# @param classname str : The name of the LeClass we want
# @return responses ({string:*}): a list of dict with field:value
def get(self, query_filters, typename = None, classname = None):
filters = list()
@ -85,42 +86,56 @@ class _LeObject(object):
#Fetching EmType
if typename is None:
emtype = None
letype = None
else:
emtype = self._model.component_from_name(typename, 'EmType')
if not emtype:
raise LeObjectQueryError("No such EmType : '%s'"%typename)
letype = leobject.lefactory.LeFactory.leobj_from_name(typename)
#Fetching EmClass
if classname is None:
emclass = None
leclass = None
else:
emclass = self._model.component_from_name(classname, 'EmClass')
leclass = leobject.lefactory.LeFactory.leobj_from_name(classname)
if not emclass:
raise LeObjectQueryError("No such EmClass : '%s'"%classname)
#Checking relational filters (for the moment fields like superior.NATURE)
relational_filters = list()
for i in range(len(filters),0,-1): #reverse iteration to be able to delete relational filters from the list
field,_,_ = filters[i]
if field.startwith('superior.'):
#relationnal field
spl = field.split('.')
if len(spl) != 2:
raise LeObjectQueryError("Not a valid relationnal field : '%s'"%(field))
nature = spl[1]
if nature not in EditorialModel.classtypes.EmNature.getall():
raise LeObjectQueryError("'%s' is not a valid nature in the field %s"%(nature, field))
relational_filters.append(nature)
#Checking that fields in the query_filters are correct
if emtype is None and emclass is None:
if letype is None and leclass is None:
#Only fields from the object table are allowed
for field,_,_ in filters:
if field not in EditorialModel.classtype.common_fields:
raise LeObjectQueryError("Not typename and no classname given, but the field %s is not in the common_fields list"%field)
else:
if emtype is None:
field_l = emclass.fields()
if letype is None:
field_l = leclass._fieldtypes.keys()
else:
if not (emclass is None):
if emtype.em_class != emclass:
if not (leclass is None):
if letype._leclass != leclass:
raise LeObjectQueryError("The EmType %s is not a specialisation of the EmClass %s"%(typename, classname))
else:
#Set emclass (to query the db ?
emclass = emtype.em_class
field_l = emtype.fields()
leclass = emtype._leclass
field_l = letype._fields
#Checks that fields are in this type
for field,_,_ in filters:
if field not in [ f.name for f in fields_l ]:
if field not in fields_l:
raise LeObjectQueryError("No field named '%s' in '%s'"%(field, typename))
return self._datasource.get(emclass, emtype, filters)
return self._datasource.get(emclass, emtype, filters, relational_filters)
## @brief check if data dict fits with the model
# @param data dict: dictionnary of field:value to check

View file

@ -28,13 +28,32 @@ class LeType(object):
if name not in self._fields:
raise AttributeError("No such field '%s' for %s"%(name, self.__class__.__name__)
setattr(self, name, value)
## @brief Delete the LeType from Db
# @note equivalent to LeType::delete(this.lodel_id)
@classinstancemethod
def delete(self):
pass
## @brief Delete a LeType from the datasource
# @param lodel_id int : The lodel_id identifying the LeType
# @param return True if deleted False if not existing
# @throw InvalidArgumentError if invalid parameters
# @throw Leo exception if the lodel_id identify an object from another type
@delete.classmethod
def delete(cls, uid):
def delete(cls, uid_l):
pass
@classinstancemethod
## @brief Update a LeType in db
def update(self):
pass
@update.classmethod
## @brief Update some LeType in db
# @param datas : keys are lodel_id and value are dict of fieldname => value
# @throw Some exception for some errors in datas
def update(cls, datas):
pass
## @brief Insert a new LeType in the datasource
@ -43,29 +62,10 @@ class LeType(object):
# @thorw A leo exception if invalid stuff
# @throw InvalidArgumentError if invalid argument
@classmethod
def insert(cls, **datas):
def insert(self, **datas):
super(LeType, self).insert(typename=self.__class__.__name__, classname=self._leclass.__name__, **datas)
pass
## @brief Delete a LeType from the datasource
# @param lodel_id int : The lodel_id identifying the LeType
# @param return True if deleted False if not existing
# @throw InvalidArgumentError if invalid parameters
# @throw Leo exception if the lodel_id identify an object from another type
@classmethod
def c_delete(cls, lodel_id):
pass
## @brief Update some objects in db
# @param lodel_id_l list : A list of lodel_id to update
# @param data dict : Represent the datas to update
# @return True if updated else False
# @throw InvalidArgumentError if invalid parameters
# @throw other Leo exceptions
@classmethod
def c_update(cls, lodel_id_l, datas):
pass
## @brief Check that datas are valid for this type
# @param datas dict : key == field name value are field values
# @param complete bool : if True expect that datas provide values for all non internal fields
@ -106,16 +106,6 @@ class LeType(object):
self._fields[name].check_or_raise(value)
return super(LeType, self).__setattr__(name, value)
## @brief Delete the LeType
# @return True if deleted False if not
def delete(self):
return self.__class__.delete(self.lodel_id)
## @brief Update a LeType
# @return True if ok else False
def update(self):
return self.__class__.update(self.lodel_id, self._datas)
## @brief Fetch superiors
# @param nature str : The relation nature
# @return if no nature given return a dict with nature as key and arrays of LeObject as value. Else return an array of LeObject
@ -134,6 +124,6 @@ class LeType(object):
# @param return True if done False if already done
# @throw A Leo exception if trying to link with an invalid leo
# @throw InvalidArgumentError if invalid argument
def add_superior(self, nature, leo):
def set_superior(self, nature, leo):
pass