Files
odoo_source/addons/onboarding/models/onboarding_onboarding_step.py
Florian Charlier 518a4e4c43 [IMP] onboarding: prepare all onboardings migration
### enable onboarding step sharing

Purpose: Allow to use the same step (and progress) for multiple
onboardings.
This is required for the migration of some onboardings such as the
`account` invoicing and sale `quotation` onboardings which both use
common steps (s.g., "Set up company data").

This is done by mutating the onboarding - onboarding step relation and
progress - progress step models relationships to m2m:
* Progress step records are now linked to several progress records if
the  step they track is used in several onboardings.
* There will still be multiple progress step records for per-company
tracking, if applicable.

### Allow steps w/o onboarding

This commit enables creating and tracking the completion of
onboarding steps that are not included in an onboarding panel.

Before this PR, such steps could already be defined and used.
This is the case of the `payment_provider` step from the payment
module, that could be included in an onboarding panel defined
in website_sale_dashboard (both are migrated in later commits of
this PR and related ENT).

### Support changing is_per_company

Having onboarding (steps) being immutably per-company or not
per-company is not convenient. We are here taking advantage of the
migration of onboardings (next commits) to have a working framework
for these changes.

The alternative solution of having to change all onboardings and linked
steps together and ensuring all or none are per-company was in fact
more complex to support and fragile as depending on python constraints
on the many2many relationship.

We also considered it wasn't worth the complexity to identify which
onboarding_progress record could safely be removed when its last
'per-company' step is updated to not per-company.
In that case, all companies will have to close the panel independently,
but the completion of each remaining step will not have to be done for
each company.

### Miscellaneous

#### add safe close action

This eases the handling closing an onboarding panel that
may have been deleted. The code for each panel can
therefore be simplified.

#### add onboarding step form controller

Extracted from `appointment` to be reused in other modules'
onboardings.
We are also cleaning the closing behavior which is no longer
required as dialogs now do close on save/discard.

Tests are added and updated.

Task-3025136

Part-of: odoo/odoo#104223
2023-06-30 23:37:50 +02:00

134 lines
6.2 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import _, api, Command, fields, models
from odoo.addons.onboarding.models.onboarding_progress import ONBOARDING_PROGRESS_STATES
from odoo.exceptions import ValidationError
class OnboardingStep(models.Model):
_name = 'onboarding.onboarding.step'
_description = 'Onboarding Step'
_order = 'sequence asc, id asc'
_rec_name = 'title'
onboarding_ids = fields.Many2many('onboarding.onboarding', string='Onboardings')
title = fields.Char('Title', translate=True)
description = fields.Char('Description', translate=True)
button_text = fields.Char(
'Button text', required=True, default=_("Let's do it"), translate=True,
help="Text on the panel's button to start this step")
done_icon = fields.Char('Font Awesome Icon when completed', default='fa-star')
done_text = fields.Char(
'Text to show when step is completed', default=_('Step Completed!'), translate=True)
step_image = fields.Binary("Step Image")
step_image_filename = fields.Char("Step Image Filename")
step_image_alt = fields.Char(
'Alt Text for the Step Image', default='Onboarding Step Image', translate=True,
help='Show when impossible to load the image')
panel_step_open_action_name = fields.Char(
string='Opening action', required=False,
help='Name of the onboarding step model action to execute when opening the step, '
'e.g. action_open_onboarding_1_step_1')
current_progress_step_id = fields.Many2one(
'onboarding.progress.step', string='Step Progress',
compute='_compute_current_progress', help='Onboarding Progress Step for the current context (company).')
current_step_state = fields.Selection(
ONBOARDING_PROGRESS_STATES, string='Completion State', compute='_compute_current_progress')
progress_ids = fields.One2many(
'onboarding.progress.step', 'step_id', string='Onboarding Progress Step Records', readonly=True,
help='All related Onboarding Progress Step Records (across companies)')
is_per_company = fields.Boolean('Is per company', default=True)
sequence = fields.Integer(default=10)
@api.depends_context('company')
@api.depends('progress_ids', 'progress_ids.step_state')
def _compute_current_progress(self):
# When `is_per_company` is changed, `progress_ids` is updated (see `write`) which triggers this `_compute`.
existing_progress_steps = self.progress_ids.filtered_domain([
('step_id', 'in', self.ids),
('company_id', 'in', [False, self.env.company.id]),
])
for step in self:
if step in existing_progress_steps.step_id:
current_progress_step_id = existing_progress_steps.filtered(
lambda progress_step: progress_step.step_id == step)
step.current_progress_step_id = current_progress_step_id
step.current_step_state = current_progress_step_id.step_state
else:
step.current_progress_step_id = False
step.current_step_state = 'not_done'
@api.constrains('onboarding_ids')
def check_step_on_onboarding_has_action(self):
if steps_without_action := self.filtered(lambda step: step.onboarding_ids and not step.panel_step_open_action_name):
raise ValidationError(_(
'An "Opening Action" is required for the following steps to be '
'linked to an onboarding panel: %(step_titles)s',
step_titles=steps_without_action.mapped('title'),
))
def write(self, vals):
new_is_per_company = vals.get('is_per_company')
steps_changing_is_per_company = (
self.browse() if new_is_per_company is None
else self.filtered(lambda step: step.is_per_company != new_is_per_company)
)
already_linked_onboardings = self.onboarding_ids
res = super().write(vals)
# Progress is reset (to be done per-company or, for steps, to have a single record)
if steps_changing_is_per_company:
steps_changing_is_per_company.progress_ids.unlink()
self.onboarding_ids.action_refresh_progress_ids()
if self.onboarding_ids - already_linked_onboardings:
self.onboarding_ids.progress_ids._recompute_progress_step_ids()
return res
def action_set_just_done(self):
# Make sure progress records exist for the current context (company)
steps_without_progress = self.filtered(lambda step: not step.current_progress_step_id)
steps_without_progress._create_progress_steps()
return self.current_progress_step_id.action_set_just_done().step_id
@api.model
def action_validate_step(self, xml_id):
step = self.env.ref(xml_id, raise_if_not_found=False)
if not step:
return "NOT_FOUND"
return "JUST_DONE" if step.action_set_just_done() else "WAS_DONE"
@api.model
def _get_placeholder_filename(self, field):
if field == "step_image":
return 'base/static/img/onboarding_default.png'
return super()._get_placeholder_filename(field)
def _create_progress_steps(self):
"""Create progress step records as necessary to validate steps.
Only considers existing `onboarding.progress` records for the current
company or without company (depending on `is_per_company`).
"""
onboarding_progress_records = self.env['onboarding.progress'].search([
('onboarding_id', 'in', self.onboarding_ids.ids),
('company_id', 'in', [False, self.env.company.id])
])
progress_step_values = [
{
'step_id': step_id.id,
'progress_ids': [
Command.link(onboarding_progress_record.id)
for onboarding_progress_record
in onboarding_progress_records.filtered(lambda p: step_id in p.onboarding_id.step_ids)],
'company_id': self.env.company.id if step_id.is_per_company else False,
} for step_id in self
]
return self.env['onboarding.progress.step'].create(progress_step_values)