No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

leobject.py 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. #-*- coding: utf-8 -*-
  2. import importlib
  3. import warnings
  4. import copy
  5. from lodel.context import LodelContext
  6. LodelContext.expose_modules(globals(), {
  7. 'lodel.logger': 'logger',
  8. 'lodel.settings': 'Settings',
  9. 'lodel.settings.utils': 'SettingsError',
  10. 'lodel.leapi.query': ['LeInsertQuery', 'LeUpdateQuery', 'LeDeleteQuery',
  11. 'LeGetQuery'],
  12. 'lodel.leapi.exceptions': ['LeApiError', 'LeApiErrors',
  13. 'LeApiDataCheckError', 'LeApiDataCheckErrors', 'LeApiQueryError',
  14. 'LeApiQueryErrors'],
  15. 'lodel.plugin.exceptions': ['PluginError', 'PluginTypeError',
  16. 'LodelScriptError', 'DatasourcePluginError'],
  17. 'lodel.exceptions': ['LodelFatalError'],
  18. 'lodel.plugin.hooks': ['LodelHook'],
  19. 'lodel.plugin': ['Plugin', 'DatasourcePlugin'],
  20. 'lodel.leapi.datahandlers.base_classes': ['DatasConstructor', 'Reference']})
  21. # @brief Stores the name of the field present in each LeObject that indicates
  22. # the name of LeObject subclass represented by this object
  23. CLASS_ID_FIELDNAME = "classname"
  24. # @brief Wrapper class for LeObject getter & setter
  25. #
  26. # This class intend to provide easy & friendly access to LeObject fields values
  27. # without name collision problems
  28. # @note Wrapped methods are : LeObject.data() & LeObject.set_data()
  29. class LeObjectValues(object):
  30. # @brief Construct a new LeObjectValues
  31. # @param fieldnames_callback method
  32. # @param set_callback method : The LeObject.set_datas() method of corresponding LeObject class
  33. # @param get_callback method : The LeObject.get_datas() method of corresponding LeObject class
  34. def __init__(self, fieldnames_callback, set_callback, get_callback):
  35. self._setter = set_callback
  36. self._getter = get_callback
  37. # @brief Provide read access to datas values
  38. # @note Read access should be provided for all fields
  39. # @param fname str : Field name
  40. def __getattribute__(self, fname):
  41. getter = super().__getattribute__('_getter')
  42. return getter(fname)
  43. # @brief Provide write access to datas values
  44. # @note Write acces shouldn't be provided for internal or immutable fields
  45. # @param fname str : Field name
  46. # @param fval * : the field value
  47. def __setattribute__(self, fname, fval):
  48. setter = super().__getattribute__('_setter')
  49. return setter(fname, fval)
  50. class LeObject(object):
  51. # @brief boolean that tells if an object is abtract or not
  52. _abstract = None
  53. # @brief A dict that stores DataHandler instances indexed by field name
  54. _fields = None
  55. # @brief A tuple of fieldname (or a uniq fieldname) representing uid
  56. _uid = None
  57. # @brief Read only datasource ( see @ref lodel2_datasources )
  58. _ro_datasource = None
  59. # @brief Read & write datasource ( see @ref lodel2_datasources )
  60. _rw_datasource = None
  61. # @brief Store the list of child classes
  62. _child_classes = None
  63. # @brief Name of the datasource plugin
  64. _datasource_name = None
  65. def __new__(cls, **kwargs):
  66. self = object.__new__(cls)
  67. # @brief A dict that stores fieldvalues indexed by fieldname
  68. self.__datas = {fname: None for fname in self._fields}
  69. # @brief Store a list of initianilized fields when instanciation not complete else store True
  70. self.__initialized = list()
  71. # @brief Datas accessor. Instance of @ref LeObjectValues
  72. self.d = LeObjectValues(self.fieldnames, self.set_data, self.data)
  73. for fieldname, fieldval in kwargs.items():
  74. self.__datas[fieldname] = fieldval
  75. self.__initialized.append(fieldname)
  76. self.__is_initialized = False
  77. self.__set_initialized()
  78. return self
  79. # @brief Construct an object representing an Editorial component
  80. # @note Can be considered as EmClass instance
  81. def __init__(self, **kwargs):
  82. if self._abstract:
  83. raise NotImplementedError(
  84. "%s is abstract, you cannot instanciate it." % self.__class__.__name__)
  85. # Checks that uid is given
  86. for uid_name in self._uid:
  87. if uid_name not in kwargs:
  88. raise LeApiError("Cannot instanciate a LeObject without it's identifier")
  89. self.__datas[uid_name] = kwargs[uid_name]
  90. del(kwargs[uid_name])
  91. self.__initialized.append(uid_name)
  92. # Processing given fields
  93. allowed_fieldnames = self.fieldnames(include_ro=False)
  94. err_list = dict()
  95. for fieldname, fieldval in kwargs.items():
  96. if fieldname not in allowed_fieldnames:
  97. if fieldname in self._fields:
  98. err_list[fieldname] = LeApiError(
  99. "Value given but the field is internal")
  100. else:
  101. err_list[fieldname] = LeApiError(
  102. "Unknown fieldname : '%s'" % fieldname)
  103. else:
  104. self.__datas[fieldname] = fieldval
  105. self.__initialized.append(fieldname)
  106. if len(err_list) > 0:
  107. raise LeApiErrors(msg="Unable to __init__ %s" % self.__class__,
  108. exceptions=err_list)
  109. self.__set_initialized()
  110. #-----------------------------------#
  111. # Fields datas handling methods #
  112. #-----------------------------------#
  113. # @brief Property method True if LeObject is initialized else False
  114. @property
  115. def initialized(self):
  116. return self.__is_initialized
  117. # @return The uid field name
  118. @classmethod
  119. def uid_fieldname(cls):
  120. return cls._uid
  121. # @brief Return a list of fieldnames
  122. # @param include_ro bool : if True include read only field names
  123. # @return a list of str
  124. @classmethod
  125. def fieldnames(cls, include_ro=False):
  126. if not include_ro:
  127. return [fname for fname in cls._fields if not cls._fields[fname].is_internal()]
  128. else:
  129. return list(cls._fields.keys())
  130. @classmethod
  131. def name2objname(cls, name):
  132. return name.title()
  133. # @brief Return the datahandler asssociated with a LeObject field
  134. # @param fieldname str : The fieldname
  135. # @return A data handler instance
  136. #@todo update class of exception raised
  137. @classmethod
  138. def data_handler(cls, fieldname):
  139. if not fieldname in cls._fields:
  140. raise NameError("No field named '%s' in %s" % (fieldname, cls.__name__))
  141. return cls._fields[fieldname]
  142. # @brief Getter for references datahandlers
  143. #@param with_backref bool : if true return only references with back_references
  144. #@return <code>{'fieldname': datahandler, ...}</code>
  145. @classmethod
  146. def reference_handlers(cls, with_backref=True):
  147. return {fname: fdh
  148. for fname, fdh in cls.fields(True).items()
  149. if fdh.is_reference() and
  150. (not with_backref or fdh.back_reference is not None)}
  151. # @brief Return a LeObject child class from a name
  152. # @warning This method has to be called from dynamically generated LeObjects
  153. # @param leobject_name str : LeObject name
  154. # @return A LeObject child class
  155. # @throw NameError if invalid name given
  156. @classmethod
  157. def name2class(cls, leobject_name):
  158. if cls.__module__ == 'lodel.leapi.leobject':
  159. raise NotImplementedError("Abstract method")
  160. mod = importlib.import_module(cls.__module__)
  161. try:
  162. return getattr(mod, leobject_name)
  163. except (AttributeError, TypeError):
  164. raise LeApiError("No LeObject named '%s'" % leobject_name)
  165. @classmethod
  166. def is_abstract(cls):
  167. return cls._abstract
  168. # @brief Field data handler getter
  169. #@param fieldname str : The field name
  170. #@return A datahandler instance
  171. #@throw NameError if the field doesn't exist
  172. @classmethod
  173. def field(cls, fieldname):
  174. try:
  175. return cls._fields[fieldname]
  176. except KeyError:
  177. raise NameError("No field named '%s' in %s" % (fieldname,
  178. cls.__name__))
  179. # @return A dict with fieldname as key and datahandler as instance
  180. @classmethod
  181. def fields(cls, include_ro=False):
  182. if include_ro:
  183. return copy.copy(cls._fields)
  184. else:
  185. return {fname: cls._fields[fname] for fname in cls._fields\
  186. if not cls._fields[fname].is_internal()}
  187. # @brief Return the list of parents classes
  188. #
  189. #@note the first item of the list is the current class, the second is it's
  190. # parent etc...
  191. #@param cls
  192. #@warning multiple inheritance broken by this method
  193. #@return a list of LeObject child classes
  194. #@todo multiple parent capabilities implementation
  195. @classmethod
  196. def hierarch(cls):
  197. res = [cls]
  198. cur = cls
  199. while True:
  200. cur = cur.__bases__[0] # Multiple inheritance broken HERE
  201. if cur in (LeObject, object):
  202. break
  203. else:
  204. res.append(cur)
  205. return res
  206. # @brief Return a tuple a child classes
  207. #@return a tuple of child classes
  208. @classmethod
  209. def child_classes(cls):
  210. return copy.copy(cls._child_classes)
  211. # @brief Return the parent class that is the "source" of uid
  212. #
  213. # The method goal is to return the parent class that defines UID.
  214. #@return a LeObject child class or false if no UID defined
  215. @classmethod
  216. def uid_source(cls):
  217. if cls._uid is None or len(cls._uid) == 0:
  218. return False
  219. hierarch = cls.hierarch()
  220. prev = hierarch[0]
  221. uid_handlers = set(cls._fields[name] for name in cls._uid)
  222. for pcls in cls.hierarch()[1:]:
  223. puid_handlers = set(cls._fields[name] for name in pcls._uid)
  224. if set(pcls._uid) != set(prev._uid) \
  225. or puid_handlers != uid_handlers:
  226. break
  227. prev = pcls
  228. return prev
  229. # @brief Initialise both datasources (ro and rw)
  230. #
  231. # This method is used once at dyncode load to replace the datasource string
  232. # by a datasource instance to avoid doing this operation for each query
  233. #@see LeObject::_init_datasource()
  234. @classmethod
  235. def _init_datasources(cls):
  236. if isinstance(cls._datasource_name, str):
  237. rw_ds = ro_ds = cls._datasource_name
  238. else:
  239. ro_ds, rw_ds = cls._datasource_name
  240. # Read only datasource initialisation
  241. cls._ro_datasource = DatasourcePlugin.init_datasource(ro_ds, True)
  242. if cls._ro_datasource is None:
  243. log_msg = "No read only datasource set for LeObject %s"
  244. log_msg %= cls.__name__
  245. logger.debug(log_msg)
  246. else:
  247. log_msg = "Read only datasource '%s' initialized for LeObject %s"
  248. log_msg %= (ro_ds, cls.__name__)
  249. logger.debug(log_msg)
  250. # Read write datasource initialisation
  251. cls._rw_datasource = DatasourcePlugin.init_datasource(rw_ds, False)
  252. if cls._ro_datasource is None:
  253. log_msg = "No read/write datasource set for LeObject %s"
  254. log_msg %= cls.__name__
  255. logger.debug(log_msg)
  256. else:
  257. log_msg = "Read/write datasource '%s' initialized for LeObject %s"
  258. log_msg %= (ro_ds, cls.__name__)
  259. logger.debug(log_msg)
  260. # @brief Return the uid of the current LeObject instance
  261. #@return the uid value
  262. #@warning Broke multiple uid capabilities
  263. def uid(self):
  264. return self.data(self._uid[0])
  265. # @brief Read only access to all datas
  266. # @note for fancy data accessor use @ref LeObject.g attribute @ref LeObjectValues instance
  267. # @param field_name str : field name
  268. # @return the Value
  269. # @throw RuntimeError if the field is not initialized yet
  270. # @throw NameError if name is not an existing field name
  271. def data(self, field_name):
  272. if field_name not in self._fields.keys():
  273. raise NameError("No such field in %s : %s" % (self.__class__.__name__, field_name))
  274. if not self.initialized and field_name not in self.__initialized:
  275. raise RuntimeError(
  276. "The field %s is not initialized yet (and have no value)" % field_name)
  277. return self.__datas[field_name]
  278. # @brief Read only access to all datas
  279. #@return a dict representing datas of current instance
  280. def datas(self, internal=False):
  281. return {fname: self.data(fname) for fname in self.fieldnames(internal)}
  282. # @brief Datas setter
  283. # @note for fancy data accessor use @ref LeObject.g attribute @ref LeObjectValues instance
  284. # @param fname str : field name
  285. # @param fval * : field value
  286. # @return the value that is really set
  287. # @throw NameError if fname is not valid
  288. # @throw AttributeError if the field is not writtable
  289. def set_data(self, fname, fval):
  290. if fname not in self.fieldnames(include_ro=False):
  291. if fname not in self._fields.keys():
  292. raise NameError("No such field in %s : %s" % (self.__class__.__name__, fname))
  293. else:
  294. raise AttributeError("The field %s is read only" % fname)
  295. self.__datas[fname] = fval
  296. if not self.initialized and fname not in self.__initialized:
  297. # Add field to initialized fields list
  298. self.__initialized.append(fname)
  299. self.__set_initialized()
  300. if self.initialized:
  301. # Running full value check
  302. ret = self.__check_modified_values()
  303. if ret is None:
  304. return self.__datas[fname]
  305. else:
  306. raise LeApiErrors("Data check error", ret)
  307. else:
  308. # Doing value check on modified field
  309. # We skip full validation here because the LeObject is not fully initialized yet
  310. val, err = self._fields[fname].check_data_value(fval)
  311. if isinstance(err, Exception):
  312. # Revert change to be in valid state
  313. del(self.__datas[fname])
  314. del(self.__initialized[-1])
  315. raise LeApiErrors("Data check error", {fname: err})
  316. else:
  317. self.__datas[fname] = val
  318. # @brief Update the __initialized attribute according to LeObject internal state
  319. #
  320. # Check the list of initialized fields and set __initialized to True if all fields initialized
  321. def __set_initialized(self):
  322. if isinstance(self.__initialized, list):
  323. expected_fields = self.fieldnames(include_ro=False) + self._uid
  324. if set(expected_fields) == set(self.__initialized):
  325. self.__is_initialized = True
  326. # @brief Designed to be called when datas are modified
  327. #
  328. # Make different checks on the LeObject given it's state (fully initialized or not)
  329. # @return None if checks succeded else return an exception list
  330. def __check_modified_values(self):
  331. err_list = dict()
  332. if self.__initialized is True:
  333. # Data value check
  334. for fname in self.fieldnames(include_ro=False):
  335. val, err = self._fields[fname].check_data_value(self.__datas[fname])
  336. if err is not None:
  337. err_list[fname] = err
  338. else:
  339. self.__datas[fname] = val
  340. # Data construction
  341. if len(err_list) == 0:
  342. for fname in self.fieldnames(include_ro=True):
  343. try:
  344. field = self._fields[fname]
  345. self.__datas[fname] = field.construct_data(self,
  346. fname,
  347. self.__datas,
  348. self.__datas[fname]
  349. )
  350. except Exception as exp:
  351. err_list[fname] = exp
  352. # Datas consistency check
  353. if len(err_list) == 0:
  354. for fname in self.fieldnames(include_ro=True):
  355. field = self._fields[fname]
  356. ret = field.check_data_consistency(self, fname, self.__datas)
  357. if isinstance(ret, Exception):
  358. err_list[fname] = ret
  359. else:
  360. # Data value check for initialized datas
  361. for fname in self.__initialized:
  362. val, err = self._fields[fname].check_data_value(self.__datas[fname])
  363. if err is not None:
  364. err_list[fname] = err
  365. else:
  366. self.__datas[fname] = val
  367. return err_list if len(err_list) > 0 else None
  368. #--------------------#
  369. # Other methods #
  370. #--------------------#
  371. # @brief Temporary method to set private fields attribute at dynamic code generation
  372. #
  373. # This method is used in the generated dynamic code to set the _fields attribute
  374. # at the end of the dyncode parse
  375. # @warning This method is deleted once the dynamic code loaded
  376. # @param field_list list : list of EmField instance
  377. # @param cls
  378. @classmethod
  379. def _set__fields(cls, field_list):
  380. cls._fields = field_list
  381. # @brief Check that datas are valid for this type
  382. # @param datas dict : key == field name value are field values
  383. # @param complete bool : if True expect that datas provide values for all non internal fields
  384. # @param allow_internal bool : if True don't raise an error if a field is internal
  385. # @param cls
  386. # @return Checked datas
  387. # @throw LeApiDataCheckError if errors reported during check
  388. @classmethod
  389. def check_datas_value(cls, datas, complete=False, allow_internal=True):
  390. err_l = dict() # Error storing
  391. correct = set() # valid fields name
  392. mandatory = set() # mandatory fields name
  393. for fname, datahandler in cls._fields.items():
  394. if allow_internal or not datahandler.is_internal():
  395. correct.add(fname)
  396. if complete and not hasattr(datahandler, 'default'):
  397. mandatory.add(fname)
  398. provided = set(datas.keys())
  399. # searching for unknow fields
  400. for u_f in provided - correct:
  401. # Here we can check if the field is invalid or rejected because
  402. # it is internel
  403. err_l[u_f] = AttributeError("Unknown or unauthorized field '%s'" % u_f)
  404. # searching for missing mandatory fieldsa
  405. for missing in mandatory - provided:
  406. err_l[missing] = AttributeError("The data for field '%s' is missing" % missing)
  407. # Checks datas
  408. checked_datas = dict()
  409. for name, value in [(name, value) for name, value in datas.items() if name in correct]:
  410. dh = cls._fields[name]
  411. res = dh.check_data_value(value)
  412. checked_datas[name], err = res
  413. if err:
  414. err_l[name] = err
  415. if len(err_l) > 0:
  416. raise LeApiDataCheckErrors("Error while checking datas", err_l)
  417. return checked_datas
  418. # @brief Check and prepare datas
  419. #
  420. # @warning when complete = False we are not able to make construct_datas() and _check_data_consistency()
  421. #
  422. # @param datas dict : {fieldname : fieldvalue, ...}
  423. # @param complete bool : If True you MUST give all the datas
  424. # @param allow_internal : Wether or not interal fields are expected in datas
  425. # @param cls
  426. # @return Datas ready for use
  427. # @todo: complete is very unsafe, find a way to get rid of it
  428. @classmethod
  429. def prepare_datas(cls, datas, complete=False, allow_internal=True):
  430. if not complete:
  431. warnings.warn("\nActual implementation can make broken datas \
  432. construction and consitency when datas are not complete\n")
  433. ret_datas = cls.check_datas_value(datas, complete, allow_internal)
  434. if isinstance(ret_datas, Exception):
  435. raise ret_datas
  436. if complete:
  437. ret_datas = cls._construct_datas(ret_datas)
  438. cls._check_datas_consistency(ret_datas)
  439. return ret_datas
  440. # @brief Construct datas values
  441. #
  442. # @param cls
  443. # @param datas dict : Datas that have been returned by LeCrud.check_datas_value() methods
  444. # @return A new dict of datas
  445. # @todo IMPLEMENTATION
  446. @classmethod
  447. def _construct_datas(cls, datas):
  448. constructor = DatasConstructor(cls, datas, cls._fields)
  449. ret = {
  450. fname: constructor[fname]
  451. for fname, ftype in cls._fields.items()
  452. if not ftype.is_internal() or ftype.internal != 'autosql'
  453. }
  454. return ret
  455. # @brief Check datas consistency
  456. # @warning assert that datas is complete
  457. # @param cls
  458. # @param datas dict : Datas that have been returned by LeCrud._construct_datas() method
  459. # @throw LeApiDataCheckError if fails
  460. @classmethod
  461. def _check_datas_consistency(cls, datas):
  462. err_l = []
  463. err_l = dict()
  464. for fname, dh in cls._fields.items():
  465. ret = dh.check_data_consistency(cls, fname, datas)
  466. if isinstance(ret, Exception):
  467. err_l[fname] = ret
  468. if len(err_l) > 0:
  469. raise LeApiDataCheckError("Datas consistency checks fails", err_l)
  470. # @brief Check datas consistency
  471. # @warning assert that datas is complete
  472. # @param cls
  473. # @param datas dict : Datas that have been returned by LeCrud.prepare_datas() method
  474. # @param type_query str : Type of query to be performed , default value : insert
  475. @classmethod
  476. def make_consistency(cls, datas, type_query='insert'):
  477. for fname, dh in cls._fields.items():
  478. ret = dh.make_consistency(fname, datas, type_query)
  479. # @brief Add a new instance of LeObject
  480. # @return a new uid en case of success, False otherwise
  481. @classmethod
  482. def insert(cls, datas):
  483. query = LeInsertQuery(cls)
  484. return query.execute(datas)
  485. # @brief Update an instance of LeObject
  486. #
  487. #@param datas : list of new datas
  488. def update(self, datas=None):
  489. datas = self.datas(internal=False) if datas is None else datas
  490. uids = self._uid
  491. query_filter = list()
  492. for uid in uids:
  493. query_filter.append((uid, '=', self.data(uid)))
  494. try:
  495. query = LeUpdateQuery(self.__class__, query_filter)
  496. except Exception as err:
  497. raise err
  498. try:
  499. result = query.execute(datas)
  500. except Exception as err:
  501. raise err
  502. return result
  503. # @brief Delete an instance of LeObject
  504. #
  505. #@return 1 if the objet has been deleted
  506. def delete(self):
  507. uids = self._uid
  508. query_filter = list()
  509. for uid in uids:
  510. query_filter.append((uid, '=', self.data(uid)))
  511. query = LeDeleteQuery(self.__class__, query_filter)
  512. result = query.execute()
  513. return result
  514. # @brief Delete instances of LeObject
  515. #@param query_filters list
  516. #@returns the number of deleted items
  517. @classmethod
  518. def delete_bundle(cls, query_filters):
  519. deleted = 0
  520. try:
  521. query = LeDeleteQuery(cls, query_filters)
  522. except Exception as err:
  523. raise err
  524. try:
  525. result = query.execute()
  526. except Exception as err:
  527. raise err
  528. if not result is None:
  529. deleted += result
  530. return deleted
  531. # @brief Get instances of LeObject
  532. #
  533. #@param query_filters dict : (filters, relational filters), with filters is a list of tuples : (FIELD, OPERATOR, VALUE) )
  534. #@param field_list list|None : list of string representing fields see
  535. #@ref leobject_filters
  536. #@param order list : A list of field names or tuple (FIELDNAME,[ASC | DESC])
  537. #@param group list : A list of field names or tuple (FIELDNAME,[ASC | DESC])
  538. #@param limit int : The maximum number of returned results
  539. #@param offset int : offset
  540. #@return a list of items (lists of (fieldname, fieldvalue))
  541. @classmethod
  542. def get(cls, query_filters, field_list=None, order=None, group=None, limit=None, offset=0):
  543. if field_list is not None:
  544. for uid in [uidname
  545. for uidname in cls.uid_fieldname()
  546. if uidname not in field_list]:
  547. field_list.append(uid)
  548. if CLASS_ID_FIELDNAME not in field_list:
  549. field_list.append(CLASS_ID_FIELDNAME)
  550. try:
  551. query = LeGetQuery(
  552. cls, query_filters=query_filters, field_list=field_list,
  553. order=order, group=group, limit=limit, offset=offset)
  554. except ValueError as err:
  555. raise err
  556. try:
  557. result = query.execute()
  558. except Exception as err:
  559. raise err
  560. objects = list()
  561. for res in result:
  562. res_cls = cls.name2class(res[CLASS_ID_FIELDNAME])
  563. inst = res_cls.__new__(res_cls, **res)
  564. objects.append(inst)
  565. return objects
  566. # @brief Retrieve an object given an UID
  567. #@todo broken multiple UID
  568. @classmethod
  569. def get_from_uid(cls, uid):
  570. if cls.uid_fieldname() is None:
  571. raise LodelFatalError(
  572. "No uid defined for class %s" % cls.__name__)
  573. uidname = cls.uid_fieldname()[0] # Brokes composed UID
  574. res = cls.get([(uidname, '=', uid)])
  575. # dedoublonnage vu que query ou la datasource est bugué
  576. if len(res) > 1:
  577. res_cp = res
  578. res = []
  579. while len(res_cp) > 0:
  580. cur_res = res_cp.pop()
  581. if cur_res.uid() in [r.uid() for r in res_cp]:
  582. logger.error("DOUBLON detected in query results !!!")
  583. else:
  584. res.append(cur_res)
  585. if len(res) > 1:
  586. raise LodelFatalError("Get from uid returned more than one \
  587. object ! For class %s with uid value = %s" % (cls, uid))
  588. elif len(res) == 0:
  589. return None
  590. return res[0]
  591. # @brief Checks if an object exists
  592. @classmethod
  593. def is_exist(cls, uid):
  594. if cls.uid_fieldname() is None:
  595. raise LodelFatalError(
  596. "No uid defined for class %s" % cls.__name__)
  597. from .query import is_exist
  598. return is_exist(cls, uid)