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.

80 lines
2.4 KiB

7 years ago
7 years ago
7 years ago
7 years ago
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 openerp import api, fields, models
  6. from openerp.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. try:
  26. r.checksum_dir = dirhash(
  27. get_module_path(r.name),
  28. 'sha1',
  29. excluded_extensions=exclude,
  30. )
  31. except TypeError:
  32. _logger.debug(
  33. "Cannot compute dir hash for %s, module not found",
  34. r.display_name)
  35. def _store_checksum_installed(self, vals):
  36. if self.env.context.get('retain_checksum_installed'):
  37. return
  38. if 'checksum_installed' not in vals:
  39. if vals.get('state') == 'installed':
  40. for r in self:
  41. r.checksum_installed = r.checksum_dir
  42. elif vals.get('state') == 'uninstalled':
  43. self.write({'checksum_installed': False})
  44. @api.multi
  45. def button_uninstall_cancel(self):
  46. # TODO Use super() like in v10 after pull is merged
  47. # HACK https://github.com/odoo/odoo/pull/18597
  48. return self.with_context(retain_checksum_installed=True).write({
  49. 'state': 'installed',
  50. })
  51. @api.multi
  52. def button_upgrade_cancel(self):
  53. # TODO Use super() like in v10 after pull is merged
  54. # HACK https://github.com/odoo/odoo/pull/18597
  55. return self.with_context(retain_checksum_installed=True).write({
  56. 'state': 'installed',
  57. })
  58. @api.model
  59. def create(self, vals):
  60. res = super(Module, self).create(vals)
  61. res._store_checksum_installed(vals)
  62. return res
  63. @api.multi
  64. def write(self, vals):
  65. res = super(Module, self).write(vals)
  66. self._store_checksum_installed(vals)
  67. return res