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.

117 lines
4.7 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright (C) 2017 - Today: GRAP (http://www.grap.coop)
  3. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from openerp import api, fields, models
  6. class WizardModuleUninstall(models.TransientModel):
  7. _name = 'wizard.module.uninstall'
  8. def _default_module_id(self):
  9. return self._context.get('active_id', False)
  10. module_id = fields.Many2one(
  11. string='Module', comodel_name='ir.module.module', required=True,
  12. domain=[('state', 'not in', ['uninstalled', 'uninstallable'])],
  13. default=_default_module_id,
  14. help="Choose a module. The wizard will display all the models"
  15. " and fields linked to that module, that will be dropped,"
  16. " if selected module is uninstalled.\n"
  17. " Note : Only Non Transient items will be displayed")
  18. module_ids = fields.Many2many(
  19. string='Impacted modules', compute='_compute_module_ids',
  20. multi='module_ids',
  21. comodel_name='ir.module.module', readonly=True,
  22. help="Modules list that will be uninstalled by dependency")
  23. module_qty = fields.Integer(
  24. string='Impacted modules Quantity', compute='_compute_module_ids',
  25. multi='module_ids', readonly=True)
  26. module_name = fields.Char(
  27. string='Module Name', related='module_id.name', readonly=True)
  28. model_line_ids = fields.One2many(
  29. comodel_name='wizard.module.uninstall.line', readonly=True,
  30. inverse_name='wizard_id', domain=[('line_type', '=', 'model')])
  31. field_line_ids = fields.One2many(
  32. comodel_name='wizard.module.uninstall.line', readonly=True,
  33. inverse_name='wizard_id', domain=[('line_type', '=', 'field')])
  34. # Compute Section
  35. @api.multi
  36. @api.depends('module_id')
  37. def _compute_module_ids(self):
  38. for wizard in self:
  39. if wizard.module_id:
  40. res = wizard.module_id.downstream_dependencies()
  41. wizard.module_ids = res
  42. wizard.module_qty = len(res)
  43. else:
  44. wizard.module_ids = False
  45. wizard.module_qty = 0
  46. # OnChange Section
  47. @api.multi
  48. @api.onchange('module_id')
  49. def onchange_module_id(self):
  50. model_data_obj = self.env['ir.model.data']
  51. model_obj = self.env['ir.model']
  52. field_obj = self.env['ir.model.fields']
  53. for wizard in self:
  54. wizard.model_line_ids = False
  55. wizard.field_line_ids = False
  56. for wizard in self:
  57. model_ids = []
  58. module_names = wizard.module_ids.mapped('name')\
  59. + [wizard.module_id.name]
  60. # Get Models
  61. models_data = []
  62. all_model_ids = model_data_obj.search([
  63. ('module', 'in', module_names),
  64. ('model', '=', 'ir.model')]).mapped('res_id')
  65. all_model_ids = list(set(all_model_ids))
  66. for model in model_obj.browse(all_model_ids).filtered(
  67. lambda x: not x.osv_memory):
  68. # Filter models that are not associated to other modules,
  69. # and that will be removed, if the selected module is
  70. # uninstalled
  71. other_declarations = model_data_obj.search([
  72. ('module', 'not in', module_names),
  73. ('model', '=', 'ir.model'),
  74. ('res_id', '=', model.id)])
  75. if not len(other_declarations):
  76. models_data.append((0, 0, {
  77. 'line_type': 'model',
  78. 'model_id': model.id,
  79. }))
  80. model_ids.append(model.id)
  81. wizard.model_line_ids = models_data
  82. # Get Fields
  83. fields_data = []
  84. all_field_ids = model_data_obj.search([
  85. ('module', 'in', module_names),
  86. ('model', '=', 'ir.model.fields')]).mapped('res_id')
  87. for field in field_obj.search([
  88. ('id', 'in', all_field_ids),
  89. ('model_id', 'not in', model_ids),
  90. ('ttype', 'not in', ['one2many'])],
  91. order='model_id, name'):
  92. other_declarations = model_data_obj.search([
  93. ('module', 'not in', module_names),
  94. ('model', '=', 'ir.model.field'),
  95. ('res_id', '=', model.id)])
  96. if not len(other_declarations)\
  97. and not field.model_id.osv_memory:
  98. fields_data.append((0, 0, {
  99. 'line_type': 'field',
  100. 'field_id': field.id,
  101. }))
  102. wizard.field_line_ids = fields_data