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 31KB

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