Coverage for ingadhoc-odoo-saas-adhoc / saas_provider_upgrade / wizards / automatic_upgrade_request_wizard.py: 21%

47 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-09 19:24 +0000

1from odoo import _, fields, models 

2from odoo.exceptions import UserError 

3 

4 

5class SaasAutomaticUpgradeRequestWizard(models.TransientModel): 

6 """ 

7 Wizard to be called whenever a database type needs to be chosen 

8 e.g. restore, duplicate, etc. 

9 """ 

10 

11 _name = "saas.automatic.upgrade.request.wizard" 

12 _description = "saas.automatic.upgrade.request.wizard" 

13 _inherit = ["saas.provider.upgrade.util"] 

14 

15 planned_start = fields.Datetime( 

16 string="Planned Date", 

17 help="Date and time when the upgrade will start.", 

18 default=fields.Datetime.now, 

19 ) 

20 aim = fields.Selection( 

21 [("test", "Test"), ("production", "Production")], 

22 help="If 'test' is selected, a copy of the production database will be created " 

23 "without shutting it down. If 'production' is selected, the current production " 

24 "type will be switched to another one, and this instance will become the backup base.", 

25 required=True, 

26 default="test", 

27 ) 

28 odoo_aim = fields.Selection( 

29 [("test", "Test"), ("production", "Production")], 

30 required=True, 

31 default="production", 

32 help='The type of upgrade requested from Odoo. Defaults to "Production" to prevent Odoo ' 

33 "from running neutralize scripts that might alter the database.", 

34 ) 

35 bypass_restrictions = fields.Boolean( 

36 help="Skips restrictions when creating the request (for upgrade lines of type 'on_create').", 

37 ) 

38 hide_from_portal = fields.Boolean( 

39 default=False, 

40 help="If checked, this request will be hidden from the portal view.", 

41 ) 

42 stop_at = fields.Selection( 

43 selection=lambda self: self.env["helpdesk.ticket.upgrade.request"]._get_stop_at_states(), 

44 help="When set, the automatic request will stop at this state and notify the assigned technician.", 

45 ) 

46 

47 def confirm(self) -> dict | bool | None: 

48 """ 

49 Action to create the upgrade request(s) for the selected ticket(s). 

50 

51 :return: A warning action if the wizard was called from the portal upgrade, None otherwise 

52 """ 

53 self.ensure_one() 

54 active_model = self.env.context.get("active_model") 

55 upgrade_ticket = self.env.context.get("upgrade_ticket") 

56 from_portal_upgrade = self.env.context.get("from_portal_upgrade") 

57 # If the wizard was called from the upgrade ticket 

58 if active_model == "helpdesk.ticket": 

59 ticket_ids = self.env.context.get("active_ids") 

60 elif from_portal_upgrade and upgrade_ticket: 

61 active_model = "helpdesk.ticket" 

62 ticket_ids = [upgrade_ticket] 

63 else: 

64 return True 

65 

66 tickets = self.env[active_model].sudo().browse(ticket_ids).filtered("upgrade_team") 

67 for ticket in tickets: 

68 try: 

69 if self.aim == "production" and ticket.last_production_request_line_id: 

70 raise UserError( 

71 _("There is already a production upgrade scheduled, only one per database is allowed.") 

72 ) 

73 if self.aim == "production" and self.planned_start: 

74 self.env["calendar.event"].create_from_ticket_upgrade(ticket, self.planned_start) 

75 else: 

76 self.env["helpdesk.ticket.upgrade.request"]._create_request( 

77 ticket.id, 

78 self.aim, 

79 odoo_aim=self.odoo_aim, 

80 date_start=self.planned_start, 

81 automatic=True, 

82 hide_from_portal=self.hide_from_portal, 

83 bypass=self.bypass_restrictions, 

84 stop_at=self.stop_at, 

85 ) 

86 except (UserError, Exception) as e: 

87 # Only show the first error because notifications do not render html 

88 msg = str(e).split("\n")[0] 

89 if from_portal_upgrade: 

90 # We have to do a rollback because we catch the UserError and the danger notification does not do it. 

91 self.env.cr.rollback() 

92 title = _("Cannot create upgrade request") 

93 return self.build_display_notification_action(title, msg, "warning") 

94 else: 

95 raise UserError(e) 

96 

97 if self.aim == "production": 

98 title = _("Upgrade programmed") 

99 msg = _("An upgrade request has been created for %s.") % self.planned_start 

100 return self.build_display_notification_action(title, msg) 

101 

102 if from_portal_upgrade: 

103 title = _("Test Upgrade Created") 

104 msg = _( 

105 "A test upgrade request has been created. The process may take a few hours. You will be able to access the test databases once they are ready." 

106 ) 

107 return self.build_display_notification_action(title, msg) 

108 return True