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.

84 lines
3.6 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  1. # Copyright 2017 LasLabs Inc.
  2. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  3. import logging
  4. from odoo import api, models
  5. from ..models.module_deprecated import PARAM_DEPRECATED
  6. _logger = logging.getLogger(__name__)
  7. class ModuleUpgrade(models.TransientModel):
  8. _inherit = 'base.module.upgrade'
  9. @api.model
  10. def _autoupdate_deprecated(self):
  11. """Know if we should enable deprecated features."""
  12. deprecated = (
  13. self.env["ir.config_parameter"].get_param(PARAM_DEPRECATED))
  14. if deprecated is False:
  15. # Enable deprecated features if this is the 1st automated update
  16. # after the version that deprecated them (X.Y.2.0.0)
  17. own_module = self.env["ir.module.module"].search([
  18. ("name", "=", "module_auto_update"),
  19. ])
  20. try:
  21. if own_module.latest_version.split(".")[2] == "1":
  22. deprecated = "1"
  23. except AttributeError:
  24. pass # 1st install, there's no latest_version
  25. return deprecated == "1"
  26. @api.model
  27. def get_module_list(self):
  28. """Set modules to upgrade searching by their dir checksum."""
  29. if self._autoupdate_deprecated():
  30. Module = self.env["ir.module.module"]
  31. installed_modules = Module.search([('state', '=', 'installed')])
  32. upgradeable_modules = installed_modules.filtered(
  33. lambda r: r.checksum_dir != r.checksum_installed,
  34. )
  35. upgradeable_modules.button_upgrade()
  36. return super(ModuleUpgrade, self).get_module_list()
  37. @api.multi
  38. def upgrade_module(self):
  39. """Make a fully automated addon upgrade."""
  40. if self._autoupdate_deprecated():
  41. _logger.warning(
  42. "You are possibly using an unsupported upgrade system; "
  43. "set '%s' system parameter to '0' and start calling "
  44. "`env['ir.module.module'].upgrade_changed_checksum()` from "
  45. "now on to get rid of this message. See module's README's "
  46. "Known Issues section for further information on the matter."
  47. )
  48. # Compute updates by checksum when called in @api.model fashion
  49. self.env.cr.autocommit(True) # Avoid transaction lock
  50. if not self:
  51. self.get_module_list()
  52. Module = self.env["ir.module.module"]
  53. # Get every addon state before updating
  54. pre_states = {addon["name"]: addon["state"] for addon
  55. in Module.search_read([], ["name", "state"])}
  56. # Perform upgrades, possibly in a limited graph that excludes me
  57. result = super(ModuleUpgrade, self).upgrade_module()
  58. if self._autoupdate_deprecated():
  59. self.env.cr.autocommit(False)
  60. # Reload environments, anything may have changed
  61. self.env.clear()
  62. # Update addons checksum if state changed and I wasn't uninstalled
  63. own = Module.search_read(
  64. [("name", "=", "module_auto_update")],
  65. ["state"],
  66. limit=1)
  67. if own and own[0]["state"] != "uninstalled":
  68. for addon in Module.search([]):
  69. if addon.state != pre_states.get(addon.name):
  70. # Trigger the write hook that should have been
  71. # triggered when the module was [un]installed/updated
  72. # in the limited module graph inside above call to
  73. # super(), and updates its dir checksum as needed
  74. addon.latest_version = addon.latest_version
  75. return result