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
3.7 KiB

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