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.

51 lines
2.2 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. from openerp import api, models
  5. class ModuleUpgrade(models.TransientModel):
  6. _inherit = 'base.module.upgrade'
  7. @api.model
  8. def get_module_list(self):
  9. """Set modules to upgrade searching by their dir checksum."""
  10. Module = self.env["ir.module.module"]
  11. installed_modules = Module.search([('state', '=', 'installed')])
  12. upgradeable_modules = installed_modules.filtered(
  13. lambda r: r.checksum_dir != r.checksum_installed,
  14. )
  15. upgradeable_modules.button_upgrade()
  16. return super(ModuleUpgrade, self).get_module_list()
  17. @api.multi
  18. def upgrade_module(self):
  19. """Make a fully automated addon upgrade."""
  20. # Compute updates by checksum when called in @api.model fashion
  21. if not self:
  22. self.get_module_list()
  23. Module = self.env["ir.module.module"]
  24. # Get every addon state before updating
  25. pre_states = {addon["name"]: addon["state"]
  26. for addon in Module.search_read([], ["name", "state"])}
  27. # Perform upgrades, possibly in a limited graph that excludes me
  28. self.env.cr.autocommit(True) # Avoid transaction lock
  29. result = super(ModuleUpgrade, self).upgrade_module()
  30. self.env.cr.autocommit(False)
  31. # Reload environments, anything may have changed
  32. self.env.clear()
  33. # Update addons checksum if state changed and I wasn't uninstalled
  34. own = Module.search_read(
  35. [("name", "=", "module_auto_update")],
  36. ["state"],
  37. limit=1)
  38. if own and own[0]["state"] != "uninstalled":
  39. for addon in Module.search([]):
  40. if addon.state != pre_states.get(addon.name):
  41. # Trigger the write hook that should have been
  42. # triggered when the module was [un]installed/updated in
  43. # the limited module graph inside above call to super(),
  44. # and updates its dir checksum as needed
  45. addon.latest_version = addon.latest_version
  46. return result