Files
odoo_source/addons/onboarding/models/onboarding_progress.py
T
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

84 lines
3.5 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models
ONBOARDING_PROGRESS_STATES = [
('not_done', 'Not done'),
('just_done', 'Just done'),
('done', 'Done'),
]
class OnboardingProgress(models.Model):
_name = 'onboarding.progress'
_description = 'Onboarding Progress Tracker'
_rec_name = 'onboarding_id'
onboarding_state = fields.Selection(
ONBOARDING_PROGRESS_STATES, string='Onboarding progress', compute='_compute_onboarding_state', store=True)
is_onboarding_closed = fields.Boolean('Was panel closed?')
company_id = fields.Many2one('res.company')
onboarding_id = fields.Many2one(
'onboarding.onboarding', 'Related onboarding tracked', required=True, ondelete='cascade')
progress_step_ids = fields.Many2many('onboarding.progress.step', string='Progress Steps Trackers')
def init(self):
"""Make sure there aren't multiple records for the same onboarding and company."""
# not in _sql_constraint because COALESCE is not supported for PostgreSQL constraint
self.env.cr.execute("""
CREATE UNIQUE INDEX IF NOT EXISTS onboarding_progress_onboarding_company_uniq
ON onboarding_progress (onboarding_id, COALESCE(company_id, 0))
""")
@api.depends('onboarding_id.step_ids', 'progress_step_ids', 'progress_step_ids.step_state')
def _compute_onboarding_state(self):
for progress in self:
progress.onboarding_state = (
'not_done' if (
len(progress.progress_step_ids.filtered(lambda p: p.step_state in {'just_done', 'done'}))
!= len(progress.onboarding_id.step_ids)
)
else 'done'
)
def _recompute_progress_step_ids(self):
"""Update progress steps when a step (with existing progress) is added to an onboarding."""
for progress in self:
progress.progress_step_ids = progress.onboarding_id.step_ids.current_progress_step_id
def action_close(self):
self.is_onboarding_closed = True
def action_toggle_visibility(self):
for progress in self:
progress.is_onboarding_closed = not progress.is_onboarding_closed
def _get_and_update_onboarding_state(self):
"""Fetch the progress of an onboarding for rendering its panel.
This method is expected to only be called by the onboarding controller.
It also has the responsibility of updating the 'just_done' state into
'done' so that the 'just_done' states are only rendered once.
"""
self.ensure_one()
onboarding_states_values = {}
progress_steps_to_consolidate = self.env['onboarding.progress.step']
# Iterate over onboarding step_ids and not self.progress_step_ids because 'not_done' steps
# may not have a progress_step record.
for step in self.onboarding_id.step_ids:
step_state = step.current_step_state
if step_state == 'just_done':
progress_steps_to_consolidate |= step.current_progress_step_id
onboarding_states_values[step.id] = step_state
progress_steps_to_consolidate.action_consolidate_just_done()
if self.is_onboarding_closed:
onboarding_states_values['onboarding_state'] = 'closed'
elif self.onboarding_state == 'done':
onboarding_states_values['onboarding_state'] = 'just_done' if progress_steps_to_consolidate else 'done'
return onboarding_states_values