[IMP] Improved share-invite mechanism. Also slighty refactored code in emails sending.
[odoo/odoo.git] / addons / share / wizard / share_wizard.py
index 83fdf88..854f021 100644 (file)
@@ -175,6 +175,7 @@ class share_wizard(osv.osv_memory):
         'share_root_url': fields.function(_share_root_url, string='Share Access URL', type='char', size=512, readonly=True,
                                 help='Main access page for users that are granted shared access'),
         'name': fields.char('Share Title', size=64, required=True, help="Title for the share (displayed to users as menu and shortcut name)"),
+        'record_name': fields.char('Record name', size=128, help="Name of the shared record, if sharing a precise record"),
         'message': fields.text("Personal Message", help="An optional personal message, to be included in the e-mail notification."),
 
         'embed_code': fields.function(_embed_code, type='text'),
@@ -201,10 +202,7 @@ class share_wizard(osv.osv_memory):
         if wizard_data.user_type == 'emails' and not self.has_email(cr, uid, context=context):
             raise osv.except_osv(_('No e-mail address configured'),
                                  _('You must configure your e-mail address in the user preferences before using the Share button.'))
-        if wizard_data.invite:
-            model, res_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'share', 'action_share_wizard_step1_mail')
-        else:
-            model, res_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'share', 'action_share_wizard_step1')
+        model, res_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'share', 'action_share_wizard_step1')
         action = self.pool.get(model).read(cr, uid, res_id, context=context)
         action['res_id'] = ids[0]
         action.pop('context', '')
@@ -257,6 +255,7 @@ class share_wizard(osv.osv_memory):
                         'user_email': new_user,
                         'groups_id': [(6,0,[group_id])],
                         'share': True,
+                        'message_email_pref': 'all',
                         'company_id': current_user.company_id.id
                 }, context)
                 new_line = { 'user_id': user_id,
@@ -746,75 +745,140 @@ class share_wizard(osv.osv_memory):
         # refresh wizard_data
         wizard_data = self.browse(cr, uid, ids[0], context=context)
         
-        # Invite (OpenSocial): automatically subscribe users to the record
-        res_id = 0
-        for cond in safe_eval(main_domain):
-            if cond[0] == 'id':
-                res_id = cond[2]
-        if wizard_data.invite and res_id > 0:
+        # EMAILS AND NOTIFICATIONS
+        #  A. Not invite: as before
+        #     -> send emails to destination users
+        #  B. Invite (OpenSocial)
+        #     -> subscribe all users (existing and new) to the record
+        #     -> send a notification with a summary to the current record
+        #     -> send a notification to all users; users allowing to receive
+        #        emails in preferences will receive it
+        #        new users by default receive all notifications by email
+        
+        # A.
+        if not wizard_data.invite:
+            self.send_emails(cr, uid, wizard_data, context=context)
+        # B.
+        else:
+            # Invite (OpenSocial): automatically subscribe users to the record
+            res_id = 0
+            for cond in safe_eval(main_domain):
+                if cond[0] == 'id':
+                    res_id = cond[2]
+            # Record id not found: issue
+            if res_id <= 0:
+                raise osv.except_osv(_('Record id not found'), _('The share engine has not been able to fetch a record_id for your invitation.'))
             self.pool.get(model.model).message_subscribe(cr, uid, [res_id], new_ids + existing_ids, context=context)
+            self.send_invite_email(cr, uid, wizard_data, context=context)
+            self.send_invite_note(cr, uid, model.model, res_id, wizard_data, context=context)
+        
+        # CLOSE
+        #  A. Not invite: as before
+        #  B. Invite: skip summary screen, get back to the record
+        
+        # A.
+        if not wizard_data.invite:
+            dummy, step2_form_view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'share', 'share_step2_form')
+            return {
+                'name': _('Shared access created!'),
+                'view_type': 'form',
+                'view_mode': 'form',
+                'res_model': 'share.wizard',
+                'view_id': False,
+                'res_id': ids[0],
+                'views': [(step2_form_view_id, 'form'), (False, 'tree'), (False, 'calendar'), (False, 'graph')],
+                'type': 'ir.actions.act_window',
+                'target': 'new'
+            }
+        # B.
+        else:
+            return {
+                'view_type': 'form',
+                'view_mode': 'form',
+                'res_model': model.model,
+                'view_id': False,
+                'res_id': res_id,
+                'views': [(False, 'form'), (False, 'tree'), (False, 'calendar'), (False, 'graph')],
+                'type': 'ir.actions.act_window',
+            }
+            
 
-        # send the confirmation emails:
-        self.send_emails(cr, uid, wizard_data, context=context)
-
-        dummy, step2_form_view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'share', 'share_step2_form')
-        return {
-            'name': _('Shared access created!'),
-            'view_type': 'form',
-            'view_mode': 'form',
-            'res_model': 'share.wizard',
-            'view_id': False,
-            'res_id': ids[0],
-            'views': [(step2_form_view_id, 'form'), (False, 'tree'), (False, 'calendar'), (False, 'graph')],
-            'type': 'ir.actions.act_window',
-            'target': 'new'
-        }
-
+    def send_invite_note(self, cr, uid, model_name, res_id, wizard_data, context=None):
+        subject = _('Invitation')
+        body = 'has been <b>shared</b> with'
+        tmp_idx = 0
+        for result_line in wizard_data.result_line_ids:
+            body += ' @%s' % (result_line.user_id.login)
+            if tmp_idx < len(wizard_data.result_line_ids)-2:
+                body += ','
+            elif tmp_idx == len(wizard_data.result_line_ids)-2:
+                body += ' and'
+        body += '.'
+        return self.pool.get(model_name).message_append_note(cr, uid, [res_id], _('System Notification'), body, context=context)
+    
+    def send_invite_email(self, cr, uid, wizard_data, context=None):
+        message_obj = self.pool.get('mail.message')
+        notification_obj = self.pool.get('mail.notification')
+        user = self.pool.get('res.users').browse(cr, UID_ROOT, uid)
+        if not user.user_email:
+            raise osv.except_osv(_('Email required'), _('The current user must have an email address configured in User Preferences to be able to send outgoing emails.'))
+        
+        # TODO: also send an HTML version of this mail
+        for result_line in wizard_data.result_line_ids:
+            email_to = result_line.user_id.user_email
+            if not email_to:
+                continue
+            subject = _('Invitation to collaborate about %s') % (wizard_data.record_name)
+            body = _("Hello,\n\n")
+            body += _("I have shared %s (%s) with you!\n\n") % (wizard_data.record_name, wizard_data.name)
+            if wizard_data.message:
+                body += "%s\n\n" % (wizard_data.message)
+            if result_line.newly_created:
+                body += _("The documents are not attached, you can view them online directly on my OpenERP server at:\n    %s\n\n") % (result_line.share_url)
+                body += _("These are your credentials to access this protected area:\n")
+                body += "%s: %s" % (_("Username"), result_line.user_id.login) + "\n"
+                body += "%s: %s" % (_("Password"), result_line.password) + "\n"
+                body += "%s: %s" % (_("Database"), cr.dbname) + "\n"
+            body += _("The documents have been automatically added to your subscriptions.\n\n")
+            body += '%s\n\n' % ((user.signature or ''))
+            body += "--\n"
+            body += _("OpenERP is a powerful and user-friendly suite of Business Applications (CRM, Sales, HR, etc.)\n"
+                      "It is open source and can be found on http://www.openerp.com.")
+            msg_id = message_obj.schedule_with_attach(cr, uid, user.user_email, [email_to], subject, body, model='', context=context)
+            notification_obj.create(cr, uid, {'user_id': result_line.user_id.id, 'message_id': msg_id}, context=context)
+    
     def send_emails(self, cr, uid, wizard_data, context=None):
         self._logger.info('Sending share notifications by email...')
         mail_message = self.pool.get('mail.message')
         user = self.pool.get('res.users').browse(cr, UID_ROOT, uid)
-
+        if not user.user_email:
+            raise osv.except_osv(_('Email required'), _('The current user must have an email address configured in User Preferences to be able to send outgoing emails.'))
+        
         # TODO: also send an HTML version of this mail
         msg_ids = []
         for result_line in wizard_data.result_line_ids:
             email_to = result_line.user_id.user_email
             if not email_to:
                 continue
-            if not user.user_email:
-                raise osv.except_osv(_('Email required'), _('The current user must have an email address configured in User Preferences to be able to send outgoing emails.'))
             subject = wizard_data.name
-            body = _("Hello,")
-            body += "\n\n"
-            body += _("I've shared %s with you!") % wizard_data.name
-            body += "\n\n"
-            body += _("The documents are not attached, you can view them online directly on my OpenERP server at:")
-            body += "\n    " + result_line.share_url
-            body += "\n\n"
+            body = _("Hello,\n\n")
+            body += _("I've shared %s with you!\n\n") % wizard_data.name
+            body += _("The documents are not attached, you can view them online directly on my OpenERP server at:\n    %s\n\n") % (result_line.share_url)
             if wizard_data.message:
-                body += wizard_data.message
-                body += "\n\n"
+                body += '%s\n\n' % (wizard_data.message)
             if result_line.newly_created:
                 body += _("These are your credentials to access this protected area:\n")
-                body += "%s: %s" % (_("Username"), result_line.user_id.login) + "\n"
-                body += "%s: %s" % (_("Password"), result_line.password) + "\n"
-                body += "%s: %s" % (_("Database"), cr.dbname) + "\n"
+                body += "%s: %s\n" % (_("Username"), result_line.user_id.login)
+                body += "%s: %s\n" % (_("Password"), result_line.password)
+                body += "%s: %s\n" % (_("Database"), cr.dbname)
             else:
                 body += _("The documents have been automatically added to your current OpenERP documents.\n")
                 body += _("You may use your current login (%s) and password to view them.\n") % result_line.user_id.login
-            body += "\n\n"
-            body += (user.signature or '')
-            body += "\n\n"
+            body += "\n\n%s\n\n" % ( (user.signature or '') )
             body += "--\n"
             body += _("OpenERP is a powerful and user-friendly suite of Business Applications (CRM, Sales, HR, etc.)\n"
                       "It is open source and can be found on http://www.openerp.com.")
-            msg_ids.append(mail_message.schedule_with_attach(cr, uid,
-                                                       user.user_email,
-                                                       [email_to],
-                                                       subject,
-                                                       body,
-                                                       model='share.wizard',
-                                                       context=context))
+            msg_ids.append(mail_message.schedule_with_attach(cr, uid, user.user_email, [email_to], subject, body, model='share.wizard', context=context))
         # force direct delivery, as users expect instant notification
         mail_message.send(cr, uid, msg_ids, context=context)
         self._logger.info('%d share notification(s) sent.', len(msg_ids))