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.

85 lines
2.4 KiB

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 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. 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(self):
  46. return super(
  47. Module,
  48. self.with_context(module_uninstall=True),
  49. ).button_uninstall()
  50. @api.multi
  51. def button_uninstall_cancel(self):
  52. return super(
  53. Module,
  54. self.with_context(retain_checksum_installed=True),
  55. ).button_uninstall_cancel()
  56. @api.multi
  57. def button_upgrade_cancel(self):
  58. return super(
  59. Module,
  60. self.with_context(retain_checksum_installed=True),
  61. ).button_upgrade_cancel()
  62. @api.model
  63. def create(self, vals):
  64. res = super(Module, self).create(vals)
  65. res._store_checksum_installed(vals)
  66. return res
  67. @api.multi
  68. def write(self, vals):
  69. res = super(Module, self).write(vals)
  70. self._store_checksum_installed(vals)
  71. return res