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

leobject.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #-*- coding: utf-8 -*-
  2. ## Main class to handle objects defined by the types of an Editorial Model
  3. # an instance of these objects is pedantically called LeObject !
  4. from EditorialModel.types import EmType
  5. class _LeObject(object):
  6. ## @brief The editorial model
  7. _model = None
  8. ## @brief The datasource
  9. _datasource = None
  10. ## @brief Instantiate with a Model and a DataSource
  11. # @param **kwargs dict : datas usefull to instanciate a _LeObject
  12. def __init__(self, **kwargs):
  13. raise NotImplementedError("Abstract constructor")
  14. ## @brief create a new LeObject
  15. # @param data dict: a dictionnary of field:value to save
  16. # @return lodel_id int: new lodel_id of the newly created LeObject
  17. def insert(self, data):
  18. try:
  19. checked_data = self._check_data(data)
  20. lodel_id = self.datasource.insert(checked_data)
  21. except:
  22. raise
  23. return lodel_id
  24. ## @brief update an existing LeObject
  25. # @param lodel_id int | (int): lodel_id of the object(s) where to apply changes
  26. # @param data dict: dictionnary of field:value to save
  27. # @param update_filters string | (string): list of string of update filters
  28. # @return okay bool: True on success, it will raise on failure
  29. def update(self, lodel_id, data, update_filters=None):
  30. if not lodel_id:
  31. lodel_id = ()
  32. elif isinstance(lodel_id, int):
  33. lodel_id = (lodel_id)
  34. try:
  35. checked_data = self._check_data(data)
  36. datasource_filters = self._prepare_filters(update_filters)
  37. okay = self.datasource.update(lodel_id, checked_data, datasource_filters)
  38. except:
  39. raise
  40. return okay
  41. ## @brief delete an existing LeObject
  42. # @param lodel_id int | (int): lodel_id of the object(s) to delete
  43. # @param delete_filters string | (string): list of string of delete filters
  44. # @return okay bool: True on success, it will raise on failure
  45. def delete(self, lodel_id, delete_filters=None):
  46. if not lodel_id:
  47. lodel_id = ()
  48. elif isinstance(lodel_id, int):
  49. lodel_id = (lodel_id)
  50. try:
  51. datasource_filters = self._prepare_filters(delete_filters)
  52. okay = self.datasource.delete(lodel_id, datasource_filters)
  53. except:
  54. raise
  55. return okay
  56. ## @brief make a search to retrieve a collection of LeObject
  57. # @param query_filters string | (string): list of string of query filters
  58. # @return responses ({string:*}): a list of dict with field:value
  59. def get(self, query_filters):
  60. try:
  61. datasource_filters = self._prepare_filters(query_filters)
  62. responses = self.datasource.get(datasource_filters)
  63. except:
  64. raise
  65. return responses
  66. ## @brief check if data dict fits with the model
  67. # @param data dict: dictionnary of field:value to check
  68. # @return checked_data ({string:*}): a list of dict with field:value
  69. # @todo implent !
  70. def _check_data(self, data):
  71. checked_data = data
  72. return checked_data
  73. ## @brief check and prepare query for the datasource
  74. # @param query_filters (string): list of string of query filters
  75. # @todo implent !
  76. def _prepare_filters(self, query_filters):
  77. if query_filters is None:
  78. return ()
  79. elif isinstance(query_filters[0], str):
  80. query_filters = (query_filters)
  81. fields, operators, queries = zip(*query_filters)
  82. # find name of the type in filters
  83. try:
  84. type_index = fields.index('type')
  85. if operators[type_index] != '=':
  86. raise ValueError
  87. type_name = queries[type_index]
  88. del query_filters[type_index]
  89. except ValueError:
  90. print ("Le champ type est obligatoire dans une requête")
  91. raise
  92. comps = self.model.components(EmType)
  93. for comp in comps:
  94. if comp.name == type_name:
  95. em_type = comp
  96. break
  97. class_name = em_type.em_class.name
  98. fields = em_type.fields()
  99. field_list = [f.name for f in fields]
  100. print (em_type, class_name, type_name, fields, field_list)
  101. prepared_filters = query_filters
  102. return prepared_filters