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

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