From c329c618cf804b668e8b8f8e66052b0413c3d93d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Tue, 2 Jun 2020 11:59:51 +0000 Subject: [PATCH] [REF] digest: reorganize code to ease future changes PURPOSE Make digest email and tips more appealing. The goals of these tips are * to encourage the adoption of other apps (Did you know ?); * to make Odoo look more fun (Fun tips and tricks, young and dynamic style); * to show social proof and increase trust (emphasis on already existing projects / customers to); SPECIFICATIONS Slightly reorganize code to ease future changes, notably in KPIs computation that will be made a bit more generic. LINKS Task ID 2197417 PR #51619 --- addons/digest/models/digest.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/addons/digest/models/digest.py b/addons/digest/models/digest.py index ca2104efb1e..609f7198478 100644 --- a/addons/digest/models/digest.py +++ b/addons/digest/models/digest.py @@ -80,6 +80,10 @@ class Digest(models.Model): vals['next_run_date'] = date.today() + relativedelta(days=3) return super(Digest, self).create(vals) + # ------------------------------------------------------------ + # ACTIONS + # ------------------------------------------------------------ + def action_subscribe(self): if self.env.user not in self.user_ids: self.sudo().user_ids |= self.env.user @@ -101,6 +105,20 @@ class Digest(models.Model): digest.template_id.with_context(user=user).send_mail(digest.id, force_send=True, raise_exception=True, email_values={'email_to': user.email, 'subject': subject}) digest.next_run_date = digest._get_next_run_date() + + @api.model + def _cron_send_digest_email(self): + digests = self.search([('next_run_date', '<=', fields.Date.today()), ('state', '=', 'activated')]) + for digest in digests: + try: + digest.action_send() + except MailDeliveryException as e: + _logger.warning('MailDeliveryException while sending digest %d. Digest is now scheduled for next cron update.') + + # ------------------------------------------------------------ + # KPIS + # ------------------------------------------------------------ + def compute_kpis(self, company, user): self.ensure_one() res = {} @@ -176,6 +194,10 @@ class Digest(models.Model): (start_date + relativedelta(months=-2), start_date + relativedelta(months=-1))), } + # ------------------------------------------------------------ + # FORMATTING / TOOLS + # ------------------------------------------------------------ + def _get_margin_value(self, value, previous_value=0.0): margin = 0.0 if (value != previous_value) and (value != 0.0 and previous_value != 0.0): @@ -193,12 +215,3 @@ class Digest(models.Model): return "%3.2f%s%s" % (amount, unit, suffix) amount /= 1000.0 return "%.2f%s%s" % (amount, 'T', suffix) - - @api.model - def _cron_send_digest_email(self): - digests = self.search([('next_run_date', '<=', fields.Date.today()), ('state', '=', 'activated')]) - for digest in digests: - try: - digest.action_send() - except MailDeliveryException as e: - _logger.warning('MailDeliveryException while sending digest %d. Digest is now scheduled for next cron update.')