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.

239 lines
7.8 KiB

7 years ago
  1. ###################################################################################
  2. #
  3. # Copyright (C) 2018 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 _
  21. from odoo import models, api, fields
  22. from odoo.exceptions import ValidationError
  23. _logger = logging.getLogger(__name__)
  24. class AutoVacuumRules(models.Model):
  25. _name = 'muk_autovacuum.rules'
  26. _description = "Auto Vacuum Rules"
  27. _order = "sequence asc"
  28. #----------------------------------------------------------
  29. # Defaults
  30. #----------------------------------------------------------
  31. def _default_sequence(self):
  32. record = self.sudo().search([], order='sequence desc', limit=1)
  33. if record.exists():
  34. return record.sequence + 1
  35. else:
  36. return 1
  37. #----------------------------------------------------------
  38. # Database
  39. #----------------------------------------------------------
  40. name = fields.Char(
  41. string='Name',
  42. required=True)
  43. active = fields.Boolean(
  44. string='Active',
  45. default=True)
  46. state = fields.Selection(
  47. selection=[
  48. ('time', 'Time Based'),
  49. ('size', 'Size Based'),
  50. ('domain', 'Domain Based')],
  51. string='Rule Type',
  52. default='time',
  53. required=True)
  54. sequence = fields.Integer(
  55. string='Sequence',
  56. default=_default_sequence,
  57. required=True)
  58. model = fields.Many2one(
  59. comodel_name='ir.model',
  60. string="Model",
  61. required=True,
  62. ondelete='cascade',
  63. help="Model on which the rule is applied.")
  64. model_name = fields.Char(
  65. related='model.model',
  66. string="Model Name",
  67. readonly=True,
  68. store=True)
  69. time_field = fields.Many2one(
  70. comodel_name='ir.model.fields',
  71. domain="[('model_id', '=', model), ('ttype', '=', 'datetime')]",
  72. string='Time Field',
  73. ondelete='cascade',
  74. states={
  75. 'time': [('required', True)],
  76. 'size': [('invisible', True)],
  77. 'domain': [('invisible', True)]})
  78. time_type = fields.Selection(
  79. selection=[
  80. ('minutes', 'Minutes'),
  81. ('hours', 'Hours'),
  82. ('days', 'Days'),
  83. ('weeks', 'Weeks'),
  84. ('months', 'Months'),
  85. ('years', 'Years')],
  86. string='Time Unit',
  87. default='months',
  88. states={
  89. 'time': [('required', True)],
  90. 'size': [('invisible', True)],
  91. 'domain': [('invisible', True)]})
  92. time = fields.Integer(
  93. string='Time',
  94. default=1,
  95. states={
  96. 'time': [('required', True)],
  97. 'size': [('invisible', True)],
  98. 'domain': [('invisible', True)]},
  99. help="Delete older data than x.")
  100. size_type = fields.Selection(
  101. selection=[
  102. ('fixed', 'Fixed Value'),
  103. ('parameter', 'System Parameter')],
  104. string='Size Type',
  105. default='fixed',
  106. states={
  107. 'time': [('invisible', True)],
  108. 'size': [('required', True)],
  109. 'domain': [('invisible', True)]})
  110. size_parameter = fields.Many2one(
  111. comodel_name='ir.config_parameter',
  112. string='System Parameter',
  113. ondelete='cascade',
  114. states={
  115. 'time': [('invisible', True)],
  116. 'size': [('required', True)],
  117. 'domain': [('invisible', True)]})
  118. size_parameter_value = fields.Integer(
  119. compute='_compute_size_parameter_value',
  120. string='Size',
  121. states={
  122. 'time': [('invisible', True)],
  123. 'size': [('readonly', True)],
  124. 'domain': [('invisible', True)]},
  125. help="Delete records with am index greater than x.")
  126. size_order = fields.Char(
  127. string='Size Order',
  128. default='create_date desc',
  129. states={
  130. 'time': [('invisible', True)],
  131. 'size': [('required', True)],
  132. 'domain': [('invisible', True)]},
  133. help="Order by which the index is defined.")
  134. size = fields.Integer(
  135. string='Size',
  136. default=200,
  137. states={
  138. 'time': [('invisible', True)],
  139. 'size': [('required', True)],
  140. 'domain': [('invisible', True)]},
  141. help="Delete records with am index greater than x.")
  142. domain = fields.Char(
  143. string='Before Update Domain',
  144. states={
  145. 'time': [('invisible', True)],
  146. 'size': [('invisible', True)],
  147. 'domain': [('required', True)]},
  148. help="Delete all records which match the domain.")
  149. protect_starred = fields.Boolean(
  150. string='Protect Starred',
  151. default=True,
  152. states={
  153. 'time': [('invisible', False)],
  154. 'size': [('invisible', True)],
  155. 'domain': [('invisible', True)]},
  156. help="Do not delete starred records.")
  157. only_inactive = fields.Boolean(
  158. string='Only Archived',
  159. default=False,
  160. states={
  161. 'time': [('invisible', False)],
  162. 'size': [('invisible', True)],
  163. 'domain': [('invisible', True)]},
  164. help="Only delete archived records.")
  165. only_attachments = fields.Boolean(
  166. string='Only Attachments',
  167. default=False,
  168. help="Only delete record attachments.")
  169. #----------------------------------------------------------
  170. # View
  171. #----------------------------------------------------------
  172. @api.onchange('model')
  173. def _onchange_model(self):
  174. field_domain = [
  175. ('model_id', '=', self.model.id),
  176. ('ttype', '=', 'datetime'),
  177. ('name', '=', 'create_date')]
  178. record = self.env['ir.model.fields'].sudo().search(field_domain, limit=1)
  179. if record.exists():
  180. self.time_field = record
  181. else:
  182. return None
  183. #----------------------------------------------------------
  184. # Read
  185. #----------------------------------------------------------
  186. @api.depends('size_parameter')
  187. def _compute_size_parameter_value(self):
  188. for record in self:
  189. try:
  190. record.size_parameter_value = int(record.size_parameter.value)
  191. except ValueError:
  192. record.size_parameter_value = None
  193. #----------------------------------------------------------
  194. # Create, Update, Delete
  195. #----------------------------------------------------------
  196. @api.constrains(
  197. 'state', 'model', 'domain',
  198. 'time_field', 'time_type', 'time',
  199. 'size_type', 'size_parameter', 'size_order', 'size')
  200. def validate(self):
  201. validators = {
  202. 'time': lambda rec: rec.time_field and rec.time_type and rec.time,
  203. 'size': lambda rec: rec.size_order and (rec.size_parameter or rec.size),
  204. 'domain': lambda rec: rec.domain,
  205. }
  206. for record in self:
  207. if not validators[record.state](record):
  208. raise ValidationError(_("Rule validation has failed!"))