From 0db0e66e96a8f6b393565da6fbb8dce25ec960b7 Mon Sep 17 00:00:00 2001
From: Mohit Ghodasara <mgh@odoo.com>
Date: Thu, 31 Jan 2019 10:44:15 +0000
Subject: [PATCH] [IMP] mail: improve usability of activities

Purpose of the task is to improve the usability of activity types and
activity scheduling.

Another purpose is alto to protect some master data activity types by flagging
them and preventing their deletion.

Containing
 * make the activity type as master data so deletion will be prevented;
 * improve the usability of the activity type;
 * prevent the deletion of activity type which are used for automated activity;
 * change the usability of activity scheduling modal;

This commit is linked to task ID 1907970 and PR #29257.
---
 addons/calendar/views/mail_activity_views.xml |  2 +-
 addons/mail/data/mail_activity_data.xml       |  4 ++++
 addons/mail/models/mail_activity.py           | 18 +++++++++++++-----
 addons/mail/static/src/js/activity.js         |  1 +
 addons/mail/static/tests/chatter_tests.js     |  1 +
 addons/mail/views/mail_activity_views.xml     |  8 ++++++--
 6 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/addons/calendar/views/mail_activity_views.xml b/addons/calendar/views/mail_activity_views.xml
index cacdbe408f23..53be74a3b911 100644
--- a/addons/calendar/views/mail_activity_views.xml
+++ b/addons/calendar/views/mail_activity_views.xml
@@ -13,7 +13,7 @@
                   <attribute name="attrs">{'invisible': [('activity_category', '=', 'meeting')]}</attribute>
             </xpath>
             <xpath expr="//button[@name='action_close_dialog']" position="attributes">
-                  <attribute name="attrs">{'invisible': [('activity_category', '=', 'meeting')]}</attribute>
+                  <attribute name="attrs">{'invisible': ['|', ('activity_category', '=', 'meeting'), ('id', '!=', False)]}</attribute>
             </xpath>
             <xpath expr="//button[@name='action_done']" position="attributes">
                   <attribute name="attrs">{'invisible': ['|', ('activity_category', '=', 'meeting'), ('force_next', '=', True)]}</attribute>
diff --git a/addons/mail/data/mail_activity_data.xml b/addons/mail/data/mail_activity_data.xml
index f05d7695ec17..f3594e44fc2e 100644
--- a/addons/mail/data/mail_activity_data.xml
+++ b/addons/mail/data/mail_activity_data.xml
@@ -11,17 +11,20 @@
             <field name="icon">fa-phone</field>
             <field name="delay_count">2</field>
             <field name="sequence">6</field>
+            <field name="is_master_data">True</field>
         </record>
         <record id="mail_activity_data_meeting" model="mail.activity.type">
             <field name="name">Meeting</field>
             <field name="icon">fa-users</field>
             <field name="sequence">9</field>
+            <field name="is_master_data">True</field>
         </record>
         <record id="mail_activity_data_todo" model="mail.activity.type">
             <field name="name">To Do</field>
             <field name="icon">fa-tasks</field>
             <field name="delay_count">5</field>
             <field name="sequence">12</field>
+            <field name="is_master_data">True</field>
         </record>
         <record id="mail_activity_data_upload_document" model="mail.activity.type">
             <field name="name">Upload Document</field>
@@ -36,6 +39,7 @@
             <field name="delay_count">0</field>
             <field name="sequence">99</field>
             <field name="decoration_type">warning</field>
+            <field name="is_master_data">True</field>
         </record>
     </data>
 </odoo>
diff --git a/addons/mail/models/mail_activity.py b/addons/mail/models/mail_activity.py
index 9d9a5d06b2fd..0a58ef5aa889 100644
--- a/addons/mail/models/mail_activity.py
+++ b/addons/mail/models/mail_activity.py
@@ -33,12 +33,12 @@ class MailActivityType(models.Model):
         return super(MailActivityType, self).default_get(fields)
 
     name = fields.Char('Name', required=True, translate=True)
-    summary = fields.Char('Summary', translate=True)
+    summary = fields.Char('Default Summary', translate=True)
     sequence = fields.Integer('Sequence', default=10)
     active = fields.Boolean(default=True)
     create_uid = fields.Many2one('res.users', index=True)
     delay_count = fields.Integer(
-        'After', default=0, oldname='days',
+        'Scheduled Date', default=0, oldname='days',
         help='Number of days/week/month before executing the action. It allows to plan the action deadline.')
     delay_unit = fields.Selection([
         ('days', 'days'),
@@ -59,7 +59,7 @@ class MailActivityType(models.Model):
              ' and not available when managing activities for other models.')
     default_next_type_id = fields.Many2one('mail.activity.type', 'Default Next Activity',
         domain="['|', ('res_model_id', '=', False), ('res_model_id', '=', res_model_id)]")
-    force_next = fields.Boolean("Auto Schedule Next Activity", default=False)
+    force_next = fields.Boolean("Trigger Next Activity", default=False)
     next_type_ids = fields.Many2many(
         'mail.activity.type', 'mail_activity_rel', 'activity_id', 'recommended_id',
         domain="['|', ('res_model_id', '=', False), ('res_model_id', '=', res_model_id)]",
@@ -69,7 +69,7 @@ class MailActivityType(models.Model):
         domain="['|', ('res_model_id', '=', False), ('res_model_id', '=', res_model_id)]",
         string='Preceding Activities')
     category = fields.Selection([
-        ('default', 'Other'), ('upload_file', 'Upload Document')
+        ('default', 'Standard'), ('upload_file', 'Upload Document')
     ], default='default', string='Category',
         help='Categories may trigger specific behavior like opening calendar view or automatically mark as done when a document is uploaded')
     mail_template_ids = fields.Many2many('mail.template', string='Mails templates')
@@ -78,6 +78,7 @@ class MailActivityType(models.Model):
     initial_res_model_id = fields.Many2one('ir.model', 'Initial model', compute="_compute_initial_res_model_id", store=False,
             help='Technical field to keep trace of the model at the beginning of the edition for UX related behaviour')
     res_model_change = fields.Boolean(string="Model has change", help="Technical field for UX related behaviour", default=False, store=False)
+    is_master_data = fields.Boolean(string="Master data", help="This field is used to prevent master data from the deletion.")
 
     @api.onchange('res_model_id')
     def _onchange_res_model_id(self):
@@ -88,6 +89,13 @@ class MailActivityType(models.Model):
         for activity_type in self:
             activity_type.initial_res_model_id = activity_type.res_model_id
 
+    @api.multi
+    def unlink(self):
+        for activity_type in self:
+            if activity_type.is_master_data:
+                raise exceptions.ValidationError("You can not delete activity type that are used as master data.")
+        return super(MailActivityType, self).unlink()
+
 
 class MailActivity(models.Model):
     """ An actual activity to perform. Activities are linked to
@@ -120,7 +128,7 @@ class MailActivity(models.Model):
         help="Display name of the related document.", readonly=True)
     # activity
     activity_type_id = fields.Many2one(
-        'mail.activity.type', 'Activity',
+        'mail.activity.type', string='Activity Type',
         domain="['|', ('res_model_id', '=', False), ('res_model_id', '=', res_model_id)]", ondelete='restrict')
     activity_category = fields.Selection(related='activity_type_id.category', readonly=False)
     activity_decoration = fields.Selection(related='activity_type_id.decoration_type', readonly=False)
diff --git a/addons/mail/static/src/js/activity.js b/addons/mail/static/src/js/activity.js
index a0cb23b940d5..5cb82f3f4de6 100644
--- a/addons/mail/static/src/js/activity.js
+++ b/addons/mail/static/src/js/activity.js
@@ -214,6 +214,7 @@ var BasicActivity = AbstractField.extend({
     _openActivityForm: function (id, callback) {
         var action = {
             type: 'ir.actions.act_window',
+            name: _t("Schedule Activity"),
             res_model: 'mail.activity',
             view_mode: 'form',
             view_type: 'form',
diff --git a/addons/mail/static/tests/chatter_tests.js b/addons/mail/static/tests/chatter_tests.js
index 63f4f3bf53d2..e75157d3ad70 100644
--- a/addons/mail/static/tests/chatter_tests.js
+++ b/addons/mail/static/tests/chatter_tests.js
@@ -1832,6 +1832,7 @@ QUnit.test('form activity widget: edit next activity', function (assert) {
                       default_res_id: 2,
                       default_res_model: "partner"
                     },
+                    name: "Schedule Activity",
                     res_id: 1,
                     res_model: "mail.activity",
                     target: "new",
diff --git a/addons/mail/views/mail_activity_views.xml b/addons/mail/views/mail_activity_views.xml
index 8bca8e907b3c..0ee562df52ee 100644
--- a/addons/mail/views/mail_activity_views.xml
+++ b/addons/mail/views/mail_activity_views.xml
@@ -32,6 +32,7 @@
                                 </div>
                                 <field name="delay_from"/>
                             </div>
+                            <field name="is_master_data" groups="base.group_no_one"/>
                         </group>
                         <group name="activity_planning">
                             <field name="force_next"/>
@@ -127,8 +128,11 @@
                     </group>
                     <field name="note" placeholder="Log a note..."/>
                     <footer>
-                        <button string="Schedule" name="action_close_dialog"
-                            type="object" class="btn-primary"/>
+                        <field name="id" invisible="1"/>
+                        <button string="Schedule" name="action_close_dialog" type="object" class="btn-primary"
+                            attrs="{'invisible': [('id', '!=', False)]}"/>
+                        <button string="Save" name="action_close_dialog" type="object" class="btn-primary"
+                            attrs="{'invisible': [('id', '=', False)]}"/>
                         <button attrs="{'invisible': [('force_next', '=', True)]}" string="Mark as Done" name="action_done"
                             type="object" class="btn-secondary"
                             context="{'mail_activity_quick_update': True}"/>
-- 
GitLab