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.

221 lines
7.7 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. ###################################################################################
  2. #
  3. # Copyright (C) 2017 MuK IT GmbH
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. ###################################################################################
  19. import logging
  20. from odoo import models, api
  21. _logger = logging.getLogger(__name__)
  22. class BaseModelExtension(models.AbstractModel):
  23. _name = 'muk_utils.model'
  24. _description = 'MuK Base Model'
  25. #----------------------------------------------------------
  26. # Function
  27. #----------------------------------------------------------
  28. @api.multi
  29. def notify_change(self, values, *largs, **kwargs):
  30. pass
  31. @api.multi
  32. def trigger_computation(self, fields, *largs, **kwargs):
  33. pass
  34. @api.multi
  35. def check_existence(self):
  36. records = self.exists()
  37. if not (len(records) == 0 or (len(records) == 1 and records.id == False)):
  38. return records
  39. else:
  40. return False
  41. #----------------------------------------------------------
  42. # Read
  43. #----------------------------------------------------------
  44. @api.model
  45. def browse(self, arg=None, prefetch=None):
  46. arg = self._before_browse(arg)
  47. result = super(BaseModelExtension, self).browse(arg, prefetch)
  48. result = self._after_browse(result)
  49. return result
  50. @api.model
  51. def _before_browse(self, arg, *largs, **kwargs):
  52. return arg
  53. @api.model
  54. def _after_browse(self, result, *largs, **kwargs):
  55. return result
  56. @api.multi
  57. def read(self, fields=None, load='_classic_read'):
  58. fields = self._before_read(fields)
  59. result = super(BaseModelExtension, self).read(fields, load)
  60. for index, record in enumerate(self.exists()):
  61. try:
  62. result[index] = record._after_read_record(result[index])
  63. except IndexError:
  64. _logger.exception("Something went wrong!")
  65. result = self._after_read(result)
  66. return result
  67. @api.multi
  68. def _before_read(self, fields, *largs, **kwargs):
  69. return fields
  70. @api.multi
  71. def _after_read_record(self, values, *largs, **kwargs):
  72. return values
  73. @api.multi
  74. def _after_read(self, result, *largs, **kwargs):
  75. return result
  76. @api.model
  77. @api.returns('self',
  78. upgrade=lambda self, value, args, offset=0, limit=None, order=None, count=False: value if count else self.browse(value),
  79. downgrade=lambda self, value, args, offset=0, limit=None, order=None, count=False: value if count else value.ids)
  80. def search(self, args, offset=0, limit=None, order=None, count=False):
  81. args, offset, limit, order, count = self._before_search(args, offset, limit, order, count)
  82. result = super(BaseModelExtension, self).search(args, offset, limit, order, count)
  83. result = self._after_search(result)
  84. return result
  85. @api.model
  86. def _before_search(self, args, offset, limit, order, count, *largs, **kwargs):
  87. return args, offset, limit, order, count
  88. @api.model
  89. def _after_search(self, result, *largs, **kwargs):
  90. return result
  91. @api.model
  92. def name_search(self, name='', args=None, operator='ilike', limit=100):
  93. name, args, operator, limit = self._before_name_search(name, args, operator, limit)
  94. result = super(BaseModelExtension, self).name_search(name, args, operator, limit)
  95. result = self._after_name_search(result)
  96. return result
  97. @api.model
  98. def _before_name_search(self, name, args, operator, limit, *largs, **kwargs):
  99. return name, args, operator, limit
  100. @api.model
  101. def _after_name_search(self, result, *largs, **kwargs):
  102. return result
  103. @api.model
  104. def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
  105. domain, fields, groupby, offset, limit, orderby, lazy = self._before_read_group(domain, fields, groupby, offset, limit, orderby, lazy)
  106. result = super(BaseModelExtension, self).read_group(domain, fields, groupby, offset, limit, orderby, lazy)
  107. result = self._after_read_group(result)
  108. return result
  109. @api.model
  110. def _before_read_group(self, domain, fields, groupby, offset, limit, orderby, lazy, *largs, **kwargs):
  111. return domain, fields, groupby, offset, limit, orderby, lazy
  112. @api.model
  113. def _after_read_group(self, result, *largs, **kwargs):
  114. return result
  115. #----------------------------------------------------------
  116. # Create, Update, Delete
  117. #----------------------------------------------------------
  118. @api.model
  119. def create(self, vals):
  120. vals = self._before_create(vals)
  121. result = super(BaseModelExtension, self).create(vals)
  122. result = result._after_create(vals)
  123. return result
  124. @api.model
  125. def _before_create(self, vals, *largs, **kwargs):
  126. return vals
  127. @api.model
  128. def _after_create(self, vals, *largs, **kwargs):
  129. self._check_recomputation(vals, [])
  130. return self
  131. @api.multi
  132. def write(self, vals):
  133. olds = []
  134. vals = self._before_write(vals)
  135. if 'track_old_values' in self.env.context:
  136. olds = [{key: record[key] for key in vals} for record in self]
  137. result = super(BaseModelExtension, self).write(vals)
  138. for record in self:
  139. record._after_write_record(vals)
  140. result = self._after_write(result, vals, olds)
  141. return result
  142. @api.multi
  143. def _before_write(self, vals, *largs, **kwargs):
  144. return vals
  145. @api.multi
  146. def _after_write_record(self, vals, *largs, **kwargs):
  147. return vals
  148. @api.multi
  149. def _after_write(self, result, vals, olds, *largs, **kwargs):
  150. self._check_recomputation(vals, olds)
  151. self._check_notification(vals)
  152. return result
  153. @api.multi
  154. def unlink(self):
  155. info = self._before_unlink()
  156. infos = []
  157. for record in self:
  158. infos.append(record._before_unlink_record())
  159. result = super(BaseModelExtension, self).unlink()
  160. self._after_unlink(result, info, infos)
  161. return result
  162. @api.multi
  163. def _before_unlink(self, *largs, **kwargs):
  164. return {}
  165. @api.multi
  166. def _before_unlink_record(self, *largs, **kwargs):
  167. return {}
  168. @api.multi
  169. def _after_unlink(self, result, info, infos, *largs, **kwargs):
  170. pass
  171. #----------------------------------------------------------
  172. # Helper
  173. #----------------------------------------------------------
  174. @api.multi
  175. def _check_recomputation(self, vals, olds, *largs, **kwargs):
  176. # self.trigger_computation(fields)
  177. pass
  178. @api.multi
  179. def _check_notification(self, vals, *largs, **kwargs):
  180. # self.notify_change(change)
  181. pass