From e4afa5d6d0e97cfb1395407b0c0bd8388d81f544 Mon Sep 17 00:00:00 2001 From: ryv-odoo Date: Thu, 5 Sep 2019 15:14:31 +0000 Subject: [PATCH] [IMP] sms: allow to create a sms context action In the SMS template, we add the ability to create (and remove) a contextual action on the targeted model from a sms template. We change the sms composer to guess the composition mode, and then to be usable by contextual action. The guess compostion mode works differently if there are multiple target selected (mass) or a single target (comment). TASK_ID : 2053631 --- addons/sms/models/sms_template.py | 42 +++++++++- addons/sms/views/sms_template_views.xml | 25 +++++- addons/sms/wizard/sms_composer.py | 19 +++-- .../test_mail_full/tests/test_sms_composer.py | 76 +++++++++++++++++++ 4 files changed, 150 insertions(+), 12 deletions(-) diff --git a/addons/sms/models/sms_template.py b/addons/sms/models/sms_template.py index 5439c9cbb22..3d5c229458d 100644 --- a/addons/sms/models/sms_template.py +++ b/addons/sms/models/sms_template.py @@ -24,6 +24,9 @@ class SMSTemplate(models.Model): model = fields.Char('Related Document Model', related='model_id.model', index=True, store=True, readonly=True) body = fields.Char('Body', translate=True, required=True) lang = fields.Char('Language', placeholder="${object.partner_id.lang}") + sidebar_action_id = fields.Many2one('ir.actions.act_window', 'Sidebar action', readonly=True, copy=False, + help="Sidebar action to make this template available on records " + "of the related document model") # Fake fields used to implement the placeholder assistant model_object_field = fields.Many2one('ir.model.fields', string="Field", store=False, help="Select target field from the related document model.\n" @@ -86,6 +89,32 @@ class SMSTemplate(models.Model): name=_("%s (copy)") % self.name) return super(SMSTemplate, self).copy(default=default) + def action_create_sidebar_action(self): + ActWindow = self.env['ir.actions.act_window'] + view = self.env.ref('sms.sms_composer_view_form') + + for template in self: + button_name = _('Send SMS (%s)') % template.name + action = ActWindow.create({ + 'name': button_name, + 'type': 'ir.actions.act_window', + 'res_model': 'sms.composer', + # Add default_composition_mode to guess to determine if need to use mass or comment composer + 'context': "{'default_template_id' : %d, 'default_composition_mode': 'guess', 'default_res_ids': active_ids, 'default_res_id': active_id}" % (template.id), + 'view_mode': 'form', + 'view_id': view.id, + 'target': 'new', + 'binding_model_id': template.model_id.id, + }) + template.write({'sidebar_action_id': action.id}) + return True + + def action_unlink_sidebar_action(self): + for template in self: + if template.sidebar_action_id: + template.sidebar_action_id.unlink() + return True + def _get_context_lang_per_id(self, res_ids): self.ensure_one() if res_ids is None: @@ -96,8 +125,7 @@ class SMSTemplate(models.Model): results = dict((res_id, self.with_context(lang=lang)) for res_id in res_ids) else: rendered_langs = self._render_template(self.lang, self.model, res_ids) - results = dict( - (res_id, self.with_context(lang=lang) if lang else self) + results = dict((res_id, self.with_context(lang=lang) if lang else self) for res_id, lang in rendered_langs.items()) return results @@ -112,6 +140,16 @@ class SMSTemplate(models.Model): return tpl_to_rids + def _get_translated_bodies(self, res_ids): + """ return sms translated bodies into a dict {'res_id':'body'} """ + self.ensure_one() + lang_to_rids = self._get_ids_per_lang(res_ids) + all_bodies = {} + for lang, rids in lang_to_rids.items(): + template = self.with_context(lang=lang) + all_bodies.update(template._render_template(template.body, self.model, rids)) + return all_bodies + @api.model def _render_template(self, template_txt, model, res_ids): """ Render the jinja template """ diff --git a/addons/sms/views/sms_template_views.xml b/addons/sms/views/sms_template_views.xml index 24acfcac134..5672e37d9c8 100644 --- a/addons/sms/views/sms_template_views.xml +++ b/addons/sms/views/sms_template_views.xml @@ -7,9 +7,28 @@
- + +