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.

83 lines
2.4 KiB

7 years ago
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 LasLabs Inc.
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  4. import logging
  5. from odoo import api, fields, models
  6. from odoo.modules.module import get_module_path
  7. _logger = logging.getLogger(__name__)
  8. try:
  9. from checksumdir import dirhash
  10. except ImportError:
  11. _logger.debug('Cannot `import checksumdir`.')
  12. class Module(models.Model):
  13. _inherit = 'ir.module.module'
  14. checksum_dir = fields.Char(
  15. compute='_compute_checksum_dir',
  16. )
  17. checksum_installed = fields.Char()
  18. @api.depends('name')
  19. def _compute_checksum_dir(self):
  20. exclude = self.env["ir.config_parameter"].get_param(
  21. "module_auto_update.checksum_excluded_extensions",
  22. "pyc,pyo",
  23. ).split(",")
  24. for r in self:
  25. r.checksum_dir = dirhash(
  26. get_module_path(r.name),
  27. 'sha1',
  28. excluded_extensions=exclude,
  29. )
  30. def _store_checksum_installed(self, vals):
  31. if self.env.context.get('retain_checksum_installed'):
  32. return
  33. if 'checksum_installed' not in vals:
  34. if vals.get('state') == 'installed':
  35. for r in self:
  36. r.checksum_installed = r.checksum_dir
  37. elif vals.get('state') == 'uninstalled':
  38. self.write({'checksum_installed': False})
  39. @api.multi
  40. def button_uninstall_cancel(self):
  41. return super(
  42. Module,
  43. self.with_context(retain_checksum_installed=True),
  44. ).button_uninstall_cancel()
  45. @api.multi
  46. def button_upgrade_cancel(self):
  47. return super(
  48. Module,
  49. self.with_context(retain_checksum_installed=True),
  50. ).button_upgrade_cancel()
  51. @api.model
  52. def create(self, vals):
  53. res = super(Module, self).create(vals)
  54. res._store_checksum_installed(vals)
  55. return res
  56. @api.model
  57. def update_list(self):
  58. res = super(Module, self).update_list()
  59. installed_modules = self.search([('state', '=', 'installed')])
  60. upgradeable_modules = installed_modules.filtered(
  61. lambda r: r.checksum_dir != r.checksum_installed,
  62. )
  63. upgradeable_modules.write({'state': "to upgrade"})
  64. return res
  65. @api.multi
  66. def write(self, vals):
  67. res = super(Module, self).write(vals)
  68. self._store_checksum_installed(vals)
  69. return res