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.

lecrud.py 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. #-*- coding: utf-8 -*-
  2. ## @package leapi.lecrud
  3. # @brief This package contains the abstract class representing Lodel Editorial components
  4. #
  5. import copy
  6. import warnings
  7. import importlib
  8. import re
  9. from EditorialModel.fieldtypes.generic import DatasConstructor
  10. REL_SUP = 0
  11. REL_SUB = 1
  12. class LeApiErrors(Exception):
  13. ## @brief Instanciate a new exceptions handling multiple exceptions
  14. # @param msg str : Exception message
  15. # @param exceptions dict : A list of data check Exception with concerned field (or stuff) as key
  16. def __init__(self, msg = "Unknow error", exceptions = None):
  17. self._msg = msg
  18. self._exceptions = dict() if exceptions is None else exceptions
  19. def __repr__(self):
  20. return self.__str__()
  21. def __str__(self):
  22. msg = self._msg
  23. for obj, expt in self._exceptions.items():
  24. msg += "\n\t{expt_obj} : ({expt_name}) {expt_msg}; ".format(
  25. expt_obj = obj,
  26. expt_name=expt.__class__.__name__,
  27. expt_msg=str(expt)
  28. )
  29. return msg
  30. ## @brief When an error concern a query
  31. class LeApiQueryError(LeApiErrors): pass
  32. ## @brief When an error concerns a datas
  33. class LeApiDataCheckError(LeApiErrors): pass
  34. ## @brief Main class to handler lodel editorial components (relations and objects)
  35. class _LeCrud(object):
  36. ## @brief The datasource
  37. _datasource = None
  38. ## @brief abstract property to store the fieldtype representing the component identifier
  39. _uid_fieldtype = None #Will be a dict fieldname => fieldtype
  40. ## @brief will store all the fieldtypes (child classes handle it)
  41. _fieldtypes_all = None
  42. ## @brief Stores a regular expression to parse query filters strings
  43. _query_re = None
  44. ## @brief Stores Query filters operators
  45. _query_operators = ['=', '<=', '>=', '!=', '<', '>', ' in ', ' not in ', ' like ', ' not like ']
  46. ## @brief Asbtract constructor for every child classes
  47. # @param uid int : lodel_id if LeObject, id_relation if its a LeRelation
  48. # @param **kwargs : datas !
  49. # @throw NotImplementedError if trying to instanciate a class that cannot be instanciated
  50. def __init__(self, uid, **kwargs):
  51. if len(kwargs) > 0:
  52. if not self.implements_leobject() and not self.implements_lerelation():
  53. raise NotImplementedError("Abstract class !")
  54. # Try to get the name of the uid field (lodel_id for objects, id_relation for relations)
  55. try:
  56. uid_name = self.uidname()
  57. except NotImplementedError: #Should never append
  58. raise NotImplementedError("Abstract class ! You can only do partial instanciation on classes that have an uid name ! (LeObject & childs + LeRelation & childs)")
  59. # Checking uid value
  60. uid, err = self._uid_fieldtype[uid_name].check_data_value(uid)
  61. if isinstance(err, Exception):
  62. raise err
  63. setattr(self, uid_name, uid)
  64. if uid_name in kwargs:
  65. warnings.warn("When instanciating the uid was given in the uid argument but was also provided in kwargs. Droping the kwargs uid")
  66. del(kwargs[uid_name])
  67. # Populating the object with given datas
  68. errors = dict()
  69. for name, value in kwargs.items():
  70. if name not in self.fieldlist():
  71. errors[name] = AttributeError("No such field '%s' for %s"%(name, self.__class__.__name__))
  72. else:
  73. cvalue, err = self.fieldtypes()[name].check_data_value(value)
  74. if isinstance(err, Exception):
  75. errors[name] = err
  76. else:
  77. setattr(self, name, cvalue)
  78. if len(errors) > 0:
  79. raise LeApiDataCheckError("Invalid arguments given to constructor", errors)
  80. ## @brief A flag to indicate if the object was fully intanciated or not
  81. self._instanciation_complete = len(kwargs) + 1 == len(self.fieldlist())
  82. ## @brief Convert an EmType or EmClass name in a python class name
  83. # @param name str : The name
  84. # @return name.title()
  85. @staticmethod
  86. def name2classname(name):
  87. if not isinstance(name, str):
  88. raise AttributeError("Argument name should be a str and not a %s" % type(name))
  89. return name.title()
  90. ## @brief Convert an EmCalss and EmType name in a rel2type class name
  91. # @param class_name str : The name of concerned class
  92. # @param type_name str : The name of the concerned type
  93. # @param relation_name str : The name of the relation (the name of the rel2type field in the LeClass)
  94. # @return name.title()
  95. @staticmethod
  96. def name2rel2type(class_name, type_name, relation_name):
  97. cls_name = "Rel%s%s%s"%(_LeCrud.name2classname(class_name), _LeCrud.name2classname(type_name), relation_name.title())
  98. return cls_name
  99. ## @brief Given a dynamically generated class name return the corresponding python Class
  100. # @param name str : a concrete class name
  101. # @param cls
  102. # @return False if no such component
  103. @classmethod
  104. def name2class(cls, name):
  105. if not isinstance(name, str):
  106. raise ValueError("Expected name argument as a string but got %s instead"%(type(name)))
  107. mod = importlib.import_module(cls.__module__)
  108. try:
  109. return getattr(mod, name)
  110. except AttributeError:
  111. return False
  112. ## @return LeObject class
  113. @classmethod
  114. def leobject(cls):
  115. return cls.name2class('LeObject')
  116. ## @return A dict with key field name and value a fieldtype instance
  117. @classmethod
  118. def fieldtypes(cls):
  119. raise NotImplementedError("Abstract method") #child classes should return their uid fieldtype
  120. ## @return A dict with fieldtypes marked as internal
  121. # @todo check if this method is in use, else delete it
  122. @classmethod
  123. def fieldtypes_internal(self):
  124. return { fname: ft for fname, ft in cls.fieldtypes().items() if hasattr(ft, 'internal') and ft.internal }
  125. ## @return A list of field name
  126. @classmethod
  127. def fieldlist(cls):
  128. return list(cls.fieldtypes().keys())
  129. ## @return The name of the uniq id field
  130. # @todo test for abstract method !!!
  131. @classmethod
  132. def uidname(cls):
  133. raise NotImplementedError("Abstract method uid_name for %s!"%cls.__name__)
  134. ## @return maybe Bool: True if cls implements LeType
  135. # @param cls Class: a Class or instanciated object
  136. @classmethod
  137. def implements_letype(cls):
  138. return hasattr(cls, '_leclass')
  139. ## @return maybe Bool: True if cls implements LeClass
  140. # @param cls Class: a Class or instanciated object
  141. @classmethod
  142. def implements_leclass(cls):
  143. return hasattr(cls, '_class_id')
  144. ## @return maybe Bool: True if cls implements LeObject
  145. # @param cls Class: a Class or instanciated object
  146. @classmethod
  147. def implements_leobject(cls):
  148. return hasattr(cls, '_me_uid')
  149. ## @return maybe Bool: True if cls is a LeType or an instance of LeType
  150. # @param cls Class: a Class or instanciated object
  151. @classmethod
  152. def is_letype(cls):
  153. return cls.implements_letype()
  154. ## @return maybe Bool: True if cls is a LeClass or an instance of LeClass
  155. # @param cls Class: a Class or instanciated object
  156. @classmethod
  157. def is_leclass(cls):
  158. return cls.implements_leclass() and not cls.implements_letype()
  159. ## @return maybe Bool: True if cls is a LeClass or an instance of LeClass
  160. # @param cls Class: a Class or instanciated object
  161. @classmethod
  162. def is_leobject(cls):
  163. return cls.implements_leobject() and not cls.implements_leclass()
  164. ## @return maybe Bool: True if cls implements LeRelation
  165. # @param cls Class: a Class or instanciated object
  166. @classmethod
  167. def implements_lerelation(cls):
  168. return hasattr(cls, '_superior_field_name')
  169. ## @return maybe Bool: True if cls implements LeRel2Type
  170. # @param cls Class: a Class or instanciated object
  171. @classmethod
  172. def implements_lerel2type(cls):
  173. return hasattr(cls, '_rel_attr_fieldtypes')
  174. ## @return maybe Bool: True if cls is a LeHierarch or an instance of LeHierarch
  175. # @param cls Class: a Class or instanciated object
  176. @classmethod
  177. def is_lehierarch(cls):
  178. return cls.implements_lerelation() and not cls.implements_lerel2type()
  179. ## @return maybe Bool: True if cls is a LeRel2Type or an instance of LeRel2Type
  180. # @param cls Class: a Class or instanciated object
  181. @classmethod
  182. def is_lerel2type(cls):
  183. return cls.implements_lerel2type()
  184. def uidget(self):
  185. return getattr(self, self.uidname())
  186. ## @brief Returns object datas
  187. # @param internal bool : If True return all datas including internal fields
  188. # @param lang str | None : if None return datas indexed with field name, else datas are indexed with field name translation
  189. # @return a dict of fieldname : value
  190. def datas(self, internal = True, lang = None):
  191. res = dict()
  192. for fname, ftt in self.fieldtypes().items():
  193. if (internal or (not internal and not ftt.is_internal)) and hasattr(self, fname):
  194. if lang is None:
  195. res[fname] = getattr(self, fname)
  196. else:
  197. res[self.ml_fields_strings[fname][lang]] = getattr(self, fname)
  198. return res
  199. ## @brief Indicates if an instance is complete
  200. # @return a bool
  201. def is_complete(self):
  202. return self._instanciation_complete
  203. ## @brief Populate the LeType wih datas from DB
  204. # @param field_list None|list : List of fieldname to fetch. If None fetch all the missing datas
  205. # @todo Add checks to forbid the use of this method on abtract classes (LeObject, LeClass, LeType, LeRel2Type, LeRelation etc...)
  206. def populate(self, field_list=None):
  207. if not self.is_complete():
  208. if field_list == None:
  209. field_list = [ fname for fname in self.fieldlist() if not hasattr(self, fname) ]
  210. filters = [self._id_filter()]
  211. rel_filters = []
  212. # Getting datas from db
  213. fdatas = self._datasource.select(self.__class__, field_list, filters, rel_filters)
  214. if fdatas is None or len(fdatas) == 0:
  215. raise LeApiQueryError("Error when trying to populate an object. For type %s id : %d"% (self.__class__.__name__, self.lodel_id))
  216. # Setting datas
  217. for fname, fval in fdatas[0].items():
  218. setattr(self, fname, fval)
  219. self._instanciation_complete = True
  220. ## @brief Return the corresponding instance
  221. #
  222. # @note this method is a kind of factory. Allowing to make a partial instance
  223. # of abstract types using only an uid and then fetching an complete instance of
  224. # the correct class
  225. # @return Corresponding populated LeObject
  226. def get_instance(self):
  227. if self.is_complete():
  228. return self
  229. uid_fname = self.uidname()
  230. qfilter = '{uid_fname} = {uid}'.format(uid_fname = uid_fname, uid = getattr(self, uid_fname))
  231. return leobject.get([qfilter])[0]
  232. ## @brief Update a component in DB
  233. # @param datas dict : If None use instance attributes to update de DB
  234. # @return True if success
  235. # @todo better error handling
  236. # @todo for check_data_consistency, datas must be populated to make update safe !
  237. def update(self, datas=None):
  238. if not self.is_complete():
  239. self.populate()
  240. warnings.warn("\nThis object %s is not complete and has been populated when update was called. This is very unsafe\n" % self)
  241. datas = self.datas(internal=False) if datas is None else datas
  242. upd_datas = self.prepare_datas(datas, complete = False, allow_internal = False)
  243. filters = [self._id_filter()]
  244. rel_filters = []
  245. ret = self._datasource.update(self.__class__, self.uidget(), **upd_datas)
  246. if ret == 1:
  247. return True
  248. else:
  249. #ERROR HANDLING
  250. return False
  251. ## @brief Delete a component
  252. # @return True if success
  253. # @todo better error handling
  254. def delete(self):
  255. self._datasource.delete(self.__class__, self.uidget())
  256. ## @brief Check that datas are valid for this type
  257. # @param datas dict : key == field name value are field values
  258. # @param complete bool : if True expect that datas provide values for all non internal fields
  259. # @param allow_internal bool : if True don't raise an error if a field is internal
  260. # @param cls
  261. # @return Checked datas
  262. # @throw LeApiDataCheckError if errors reported during check
  263. @classmethod
  264. def check_datas_value(cls, datas, complete = False, allow_internal = True):
  265. err_l = dict() #Stores errors
  266. correct = [] #Valid fields name
  267. mandatory = [] #mandatory fields name
  268. for fname, ftt in cls.fieldtypes().items():
  269. if allow_internal or not ftt.is_internal():
  270. correct.append(fname)
  271. if complete and not hasattr(ftt, 'default'):
  272. mandatory.append(fname)
  273. mandatory = set(mandatory)
  274. correct = set(correct)
  275. provided = set(datas.keys())
  276. #searching unknow fields
  277. unknown = provided - correct
  278. for u_f in unknown:
  279. #here we can check if the field is unknown or rejected because it is internal
  280. err_l[u_f] = AttributeError("Unknown or unauthorized field '%s'"%u_f)
  281. #searching missings fields
  282. missings = mandatory - provided
  283. for miss_field in missings:
  284. err_l[miss_field] = AttributeError("The data for field '%s' is missing"%miss_field)
  285. #Checks datas
  286. checked_datas = dict()
  287. for name, value in [ (name, value) for name, value in datas.items() if name in correct ]:
  288. ft = cls.fieldtypes()
  289. ft = ft[name]
  290. r = ft.check_data_value(value)
  291. checked_datas[name], err = r
  292. #checked_datas[name], err = cls.fieldtypes()[name].check_data_value(value)
  293. if err:
  294. err_l[name] = err
  295. if len(err_l) > 0:
  296. raise LeApiDataCheckError("Error while checking datas", err_l)
  297. return checked_datas
  298. ## @brief Retrieve a collection of lodel editorial components
  299. #
  300. # @param query_filters list : list of string of query filters (or tuple (FIELD, OPERATOR, VALUE) ) see @ref leobject_filters
  301. # @param field_list list|None : list of string representing fields see @ref leobject_filters
  302. # @param order list : A list of field names or tuple (FIELDNAME, [ASC | DESC])
  303. # @param group list : A list of field names or tuple (FIELDNAME, [ASC | DESC])
  304. # @param limit int : The maximum number of returned results
  305. # @param offset int : offset
  306. # @param instanciate bool : If True return an instance, else return a dict
  307. # @param cls
  308. # @return A list of lodel editorial components instance
  309. # @todo think about LeObject and LeClass instanciation (partial instanciation, etc)
  310. @classmethod
  311. def get(cls, query_filters, field_list=None, order=None, group=None, limit=None, offset=0, instanciate=True):
  312. if field_list is None or len(field_list) == 0:
  313. #default field_list
  314. field_list = cls.fieldlist()
  315. field_list = cls._prepare_field_list(field_list) #Can raise LeApiDataCheckError
  316. #preparing filters
  317. filters, relational_filters = cls._prepare_filters(query_filters)
  318. #preparing order
  319. if order:
  320. order = cls._prepare_order_fields(order)
  321. if isinstance(order, Exception):
  322. raise order #can be buffered and raised later, but _prepare_filters raise when fails
  323. #preparing groups
  324. if group:
  325. group = cls._prepare_order_fields(group)
  326. if isinstance(group, Exception):
  327. raise group # can also be buffered and raised later
  328. #checking limit and offset values
  329. if not (limit is None):
  330. if limit <= 0:
  331. raise ValueError("Invalid limit given : %d"%limit)
  332. if not (offset is None):
  333. if offset < 0:
  334. raise ValueError("Invalid offset given : %d"%offset)
  335. #Fetching editorial components from datasource
  336. results = cls._datasource.select(
  337. target_cls = cls,
  338. field_list = field_list,
  339. filters = filters,
  340. rel_filters = relational_filters,
  341. order=order,
  342. group=group,
  343. limit=limit,
  344. offset=offset,
  345. instanciate=instanciate
  346. )
  347. return results
  348. ## @brief Insert a new component
  349. # @param datas dict : The value of object we want to insert
  350. # @param classname str : The class name
  351. # @param cls
  352. # @return A new id if success else False
  353. @classmethod
  354. def insert(cls, datas, classname=None):
  355. callcls = cls if classname is None else cls.name2class(classname)
  356. if not callcls:
  357. raise LeApiErrors("Error when inserting",{'error':ValueError("The class '%s' was not found"%classname)})
  358. if not callcls.implements_letype() and not callcls.implements_lerelation():
  359. raise ValueError("You can only insert relations and LeTypes objects but tying to insert a '%s'"%callcls.__name__)
  360. insert_datas = callcls.prepare_datas(datas, complete = True, allow_internal = False)
  361. return callcls._datasource.insert(callcls, **insert_datas)
  362. ## @brief Check and prepare datas
  363. #
  364. # @warning when complete = False we are not able to make construct_datas() and _check_data_consistency()
  365. #
  366. # @param datas dict : {fieldname : fieldvalue, ...}
  367. # @param complete bool : If True you MUST give all the datas
  368. # @param allow_internal : Wether or not interal fields are expected in datas
  369. # @param cls
  370. # @return Datas ready for use
  371. # @todo: complete is very unsafe, find a way to get rid of it
  372. @classmethod
  373. def prepare_datas(cls, datas, complete=False, allow_internal=True):
  374. if not complete:
  375. warnings.warn("\nActual implementation can make datas construction and consitency unsafe when datas are not complete\n")
  376. ret_datas = cls.check_datas_value(datas, complete, allow_internal)
  377. if isinstance(ret_datas, Exception):
  378. raise ret_datas
  379. if complete:
  380. ret_datas = cls._construct_datas(ret_datas)
  381. cls._check_datas_consistency(ret_datas)
  382. return ret_datas
  383. #-###################-#
  384. # Private methods #
  385. #-###################-#
  386. ## @brief Build a filter to select an object with a specific ID
  387. # @warning assert that the uid is not composed with multiple fieldtypes
  388. # @return A filter of the form tuple(UID, '=', self.UID)
  389. # @todo This method should not be private
  390. def _id_filter(self):
  391. id_name = self.uidname()
  392. return ( id_name, '=', getattr(self, id_name) )
  393. ## @brief Construct datas values
  394. #
  395. # @param cls
  396. # @param datas dict : Datas that have been returned by LeCrud.check_datas_value() methods
  397. # @return A new dict of datas
  398. @classmethod
  399. def _construct_datas(cls, datas):
  400. constructor = DatasConstructor(cls, datas, cls.fieldtypes())
  401. ret = {
  402. fname:constructor[fname]
  403. for fname, ftype in cls.fieldtypes().items()
  404. if not ftype.is_internal() or ftype.internal != 'autosql'
  405. }
  406. return ret
  407. ## @brief Check datas consistency
  408. # @warning assert that datas is complete
  409. # @param cls
  410. # @param datas dict : Datas that have been returned by LeCrud._construct_datas() method
  411. # @throw LeApiDataCheckError if fails
  412. @classmethod
  413. def _check_datas_consistency(cls, datas):
  414. err_l = []
  415. err_l = dict()
  416. for fname, ftype in cls.fieldtypes().items():
  417. ret = ftype.check_data_consistency(cls, fname, datas)
  418. if isinstance(ret, Exception):
  419. err_l[fname] = ret
  420. if len(err_l) > 0:
  421. raise LeApiDataCheckError("Datas consistency checks fails", err_l)
  422. ## @brief Prepare a field_list
  423. # @param cls
  424. # @param field_list list : List of string representing fields
  425. # @return A well formated field list
  426. # @throw LeApiDataCheckError if invalid field given
  427. @classmethod
  428. def _prepare_field_list(cls, field_list):
  429. err_l = dict()
  430. ret_field_list = list()
  431. for field in field_list:
  432. if cls._field_is_relational(field):
  433. ret = cls._prepare_relational_fields(field)
  434. else:
  435. ret = cls._check_field(field)
  436. if isinstance(ret, Exception):
  437. err_l[field] = ret
  438. else:
  439. ret_field_list.append(ret)
  440. if len(err_l) > 0:
  441. raise LeApiDataCheckError(err_l)
  442. return ret_field_list
  443. ## @brief Check that a relational field is valid
  444. # @param cls
  445. # @param field str : a relational field
  446. # @return a nature
  447. @classmethod
  448. def _prepare_relational_fields(cls, field):
  449. raise NotImplementedError("Abstract method")
  450. ## @brief Check that the field list only contains fields that are in the current class
  451. # @param cls
  452. # @param field : a field
  453. # @return None if no problem, else returns a list of exceptions that occurs during the check
  454. @classmethod
  455. def _check_field(cls, field):
  456. if field not in cls.fieldlist():
  457. return ValueError("No such field '%s' in %s"%(field, cls.__name__))
  458. return field
  459. ## @brief Prepare the order parameter for the get method
  460. # @note if an item in order_list is just a str it is considered as ASC by default
  461. # @param cls
  462. # @param order_field_list list : A list of field name or tuple (FIELDNAME, [ASC|DESC])
  463. # @return a list of tuple (FIELDNAME, [ASC|DESC] )
  464. @classmethod
  465. def _prepare_order_fields(cls, order_field_list):
  466. errors = dict()
  467. result = []
  468. for order_field in order_field_list:
  469. if not isinstance(order_field, tuple):
  470. order_field = (order_field, 'ASC')
  471. if len(order_field) != 2 or order_field[1].upper() not in ['ASC', 'DESC']:
  472. errors[order_field] = ValueError("Expected a string or a tuple with (FIELDNAME, ['ASC'|'DESC']) but got : %s"%order_field)
  473. else:
  474. ret = cls._check_field(order_field[0])
  475. if isinstance(ret, Exception):
  476. errors[order_field] = ret
  477. order_field = (order_field[0], order_field[1].upper())
  478. result.append(order_field)
  479. if len(errors) > 0:
  480. return LeApiErrors("Errors when preparing ordering fields", errors)
  481. return result
  482. ## @brief Prepare filters for datasource
  483. #
  484. # This method divide filters in two categories :
  485. # - filters : standart FIELDNAME OP VALUE filter
  486. # - relationnal_filters : filter on object relation RELATION_NATURE OP VALUE
  487. #
  488. # Both categories of filters are represented in the same way, a tuple with 3 elements (NAME|NAT , OP, VALUE )
  489. #
  490. # @param cls
  491. # @param filters_l list : This list can contain str "FIELDNAME OP VALUE" and tuples (FIELDNAME, OP, VALUE)
  492. # @return a tuple(FILTERS, RELATIONNAL_FILTERS
  493. #
  494. # @see @ref datasource_side
  495. @classmethod
  496. def _prepare_filters(cls, filters_l):
  497. filters = list()
  498. res_filters = list()
  499. rel_filters = list()
  500. err_l = dict()
  501. #Splitting in tuple if necessary
  502. for fil in filters_l:
  503. if len(fil) == 3 and not isinstance(fil, str):
  504. filters.append(tuple(fil))
  505. else:
  506. filters.append(cls._split_filter(fil))
  507. for field, operator, value in filters:
  508. if cls._field_is_relational(field):
  509. #Checks relational fields
  510. ret = cls._prepare_relational_fields(field)
  511. if isinstance(ret, Exception):
  512. err_l[field] = ret
  513. else:
  514. rel_filters.append((ret, operator, value))
  515. else:
  516. #Checks other fields
  517. ret = cls._check_field(field)
  518. if isinstance(ret, Exception):
  519. err_l[field] = ret
  520. else:
  521. res_filters.append((field,operator, value))
  522. if len(err_l) > 0:
  523. raise LeApiDataCheckError("Error while preparing filters : ", err_l)
  524. return (res_filters, rel_filters)
  525. ## @brief Check and split a query filter
  526. # @note The query_filter format is "FIELD OPERATOR VALUE"
  527. # @param query_filter str : A query_filter string
  528. # @param cls
  529. # @return a tuple (FIELD, OPERATOR, VALUE)
  530. @classmethod
  531. def _split_filter(cls, query_filter):
  532. if cls._query_re is None:
  533. cls._compile_query_re()
  534. matches = cls._query_re.match(query_filter)
  535. if not matches:
  536. raise ValueError("The query_filter '%s' seems to be invalid"%query_filter)
  537. result = (matches.group('field'), re.sub(r'\s', ' ', matches.group('operator'), count=0), matches.group('value').strip())
  538. for r in result:
  539. if len(r) == 0:
  540. raise ValueError("The query_filter '%s' seems to be invalid"%query_filter)
  541. return result
  542. ## @brief Compile the regex for query_filter processing
  543. # @note Set _LeObject._query_re
  544. @classmethod
  545. def _compile_query_re(cls):
  546. op_re_piece = '(?P<operator>(%s)'%cls._query_operators[0].replace(' ', '\s')
  547. for operator in cls._query_operators[1:]:
  548. op_re_piece += '|(%s)'%operator.replace(' ', '\s')
  549. op_re_piece += ')'
  550. cls._query_re = re.compile('^\s*(?P<field>(((superior)|(subordinate))\.)?[a-z_][a-z0-9\-_]*)\s*'+op_re_piece+'\s*(?P<value>[^<>=!].*)\s*$', flags=re.IGNORECASE)
  551. pass
  552. ## @brief Check if a field is relational or not
  553. # @param field str : the field to test
  554. # @return True if the field is relational else False
  555. @staticmethod
  556. def _field_is_relational(field):
  557. return field.startswith('superior.') or field.startswith('subordinate.')
  558. ## @page leobject_filters LeObject query filters
  559. # The LeObject API provide methods that accept filters allowing the user
  560. # to query the database and fetch LodelEditorialObjects.
  561. #
  562. # The LeObject API translate those filters for the datasource.
  563. #
  564. # @section api_user_side API user side filters
  565. # Filters are string expressing a condition. The string composition
  566. # is as follow : "<FIELD> <OPERATOR> <VALUE>"
  567. # @subsection fpart FIELD
  568. # @subsubsection standart fields
  569. # Standart fields, represents a value of the LeObject for example "title", "lodel_id" etc.
  570. # @subsubsection rfields relationnal fields
  571. # relationnal fields, represents a relation with the object hierarchy. Those fields are composed as follow :
  572. # "<RELATION>.<NATURE>".
  573. #
  574. # - Relation can takes two values : superiors or subordinates
  575. # - Nature is a relation nature ( see EditorialModel.classtypes )
  576. # Examples : "superiors.parent", "subordinates.translation" etc.
  577. # @note The field_list arguement of leapi.leapi._LeObject.get() use the same syntax than the FIELD filter part
  578. # @subsection oppart OPERATOR
  579. # The OPERATOR part of a filter is a comparison operator. There is
  580. # - standart comparison operators : = , <, > , <=, >=, !=
  581. # - vagueness string comparison 'like' and 'not like'
  582. # - list operators : 'in' and 'not in'
  583. # The list of allowed operators is sotred at leapi.leapi._LeObject._query_operators .
  584. # @subsection valpart VALUE
  585. # The VALUE part of a filter is... just a value...
  586. #
  587. # @section datasource_side Datasource side filters
  588. # As said above the API "translate" filters before forwarding them to the datasource.
  589. #
  590. # The translation process transform filters in tuple composed of 3 elements
  591. # ( @ref fpart , @ref oppart , @ref valpart ). Each element is a string.
  592. #
  593. # There is a special case for @ref rfields : the field element is a tuple composed with two elements
  594. # ( RELATION, NATURE ) where NATURE is a string ( see EditorialModel.classtypes ) and RELATION is one of
  595. # the defined constant :
  596. #
  597. # - leapi.lecrud.REL_SUB for "subordinates"
  598. # - leapi.lecrud.REL_SUP for "superiors"
  599. #
  600. # @note The filters translation process also check if given field are valids compared to the concerned letype and/or the leclass
  601. ## @page lecrud_instanciation LeCrud child classes instanciations
  602. #
  603. # _LeCrud provide a generic __init__ method for all its child classes. The following notes are
  604. # important parts of the instanciation mechanism.
  605. #
  606. # The constructor takes 2 parameters :
  607. # - a uniq identifier (uid)
  608. # - **kwargs for object datas
  609. #
  610. # @section lecrud_pi Partial instancation
  611. #
  612. # You can make partial instanciations by giving only parts of datas and even by giving only a uid
  613. #
  614. # @warning Partial instanciation needs an uid field name (lodel_id for LeObject and id_relation for LeRelation). This implies that you cannot make partial instance of a LeCrud.
  615. #
  616. # @subsection lecrud_pitools Partial instances tools
  617. #
  618. # The _LeCrud.is_complete() method indicates whether or not an instance is partial.
  619. #
  620. # The _LeCrud.populate() method fetch missing datas
  621. #
  622. # You partially instanciate an abtract class (like LeClass or LeRelation) using only a uid. Then you cannot populate this kind of instance (you cannot dinamically change the type of an instance). The _LeCrud.get_instance() method returns a populated instance with the good type.
  623. #