~vauxoo/addons-vauxoo/8.0-import_tax_tariff-dev-yani-rev-2

« back to all changes in this revision

Viewing changes to add_followers/wizard/add_followers.py

  • Committer: Jose Morales
  • Date: 2013-05-18 03:08:16 UTC
  • mto: (543.6.1 merge_7)
  • mto: This revision was merged to the branch mainline in revision 797.
  • Revision ID: jose@vauxoo.com-20130518030816-9d4364toeqiwtpvc
 
[ADD] In many cases there is the need to add multiple users to multiple tasks or poryectos, which is a tedious task because you have to open each document and do it manually.

That is why you add a module that meets this need and allows you to add many followers to many tasks and projects in one step, telling new followers just happened

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2012-Today OpenERP SA (<http://www.openerp.com>)
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU General Public License as published by
 
9
#    the Free Software Foundation, either version 3 of the License, or
 
10
#    (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>
 
19
#
 
20
##############################################################################
 
21
 
 
22
from openerp import tools
 
23
from openerp.osv import osv
 
24
from openerp.osv import fields
 
25
from openerp.tools.translate import _
 
26
 
 
27
 
 
28
class invite_wizard(osv.osv_memory):
 
29
    """ Wizard to invite partners and make them followers. """
 
30
    _inherit = 'mail.wizard.invite'
 
31
    _description = 'Invite wizard'
 
32
 
 
33
    def default_get(self, cr, uid, fields, context=None):
 
34
        '''
 
35
        Overwrite the original model to add model for the context in the at
 
36
        time use and add in the message all documents to follow
 
37
        '''
 
38
 
 
39
        result = super(invite_wizard, self).default_get(cr, uid, fields, context=context)
 
40
        model_obj = self.pool.get(result.get('res_model',False) or \
 
41
                                  context.get('active_model'))
 
42
        
 
43
 
 
44
        if len(context.get('active_ids',[])) > 1:
 
45
            result.update({'res_model':context.get('active_model')})
 
46
            message = _('<div>You have been invited to follow are '
 
47
                        'documents: </div>')
 
48
            for ids in context.get('active_ids',[]):
 
49
                document_name = model_obj.name_get(cr, uid, [ids],
 
50
                                                       context=context)[0][1]
 
51
                message = message + '\n<div>' + document_name + '</div>'
 
52
                    
 
53
            result['message'] = message
 
54
        elif 'message' in fields and result.get('res_model') and result.get('res_id'):
 
55
            document_name = self.pool.get(result.get('res_model')).name_get(cr, uid, [result.get('res_id')], context=context)[0][1]
 
56
            message = _('<div>You have been invited to follow %s.</div>' % document_name)
 
57
            result['message'] = message
 
58
 
 
59
        return result
 
60
 
 
61
    _columns = {
 
62
 
 
63
        'groups':fields.boolean('Groups', help='Used to add a followers '
 
64
                                                    'group from mail group '
 
65
                                                    'and not for Users '
 
66
                                                    'directly'), 
 
67
        'partners':fields.boolean('Partners', help='Used to add a followers '
 
68
                                                    'group by users' ), 
 
69
        'p_a_g':fields.boolean('Group and Partner', help='Used to add a '
 
70
                                                    'followers for partner '
 
71
                                                    'and group at the same '
 
72
                                                    'time'), 
 
73
        
 
74
 
 
75
        'mail_groups':fields.many2many('mail.group', string='Mail Groups',
 
76
                                       help='Select the mail.groups that you '
 
77
                                            'want add with followers'), 
 
78
        
 
79
    }
 
80
 
 
81
 
 
82
    def add_followers(self, cr, uid, ids, context=None):
 
83
        '''
 
84
        Overwrite the original model work with many documents at the same time
 
85
        and add followers in eech.
 
86
 
 
87
        Each id is get by context field
 
88
        '''
 
89
        res = {'type': 'ir.actions.act_window_close'}
 
90
        for wizard in self.browse(cr, uid, ids, context=context):
 
91
            if context.get('second',False):
 
92
                for res_id in context.get('active_ids',[]):
 
93
                    model_obj = self.pool.get(wizard.res_model)
 
94
                    document = model_obj.browse(cr, uid, res_id,
 
95
                                                context=context)
 
96
                    new_follower_ids = [p.id for p in wizard.partner_ids\
 
97
                                                if p.id not in\
 
98
                                                document.message_follower_ids]
 
99
 
 
100
                    # filter partner_ids to get the new followers, to avoid sending email to already following partners
 
101
                    model_obj.message_subscribe(cr, uid, [res_id],
 
102
                                                new_follower_ids,
 
103
                                                context=context)
 
104
 
 
105
                    # send an email only if a personal message exists
 
106
                    if wizard.message and not wizard.message == '<br>':  # when deleting the message, cleditor keeps a <br>
 
107
                        # add signature
 
108
                        user_id = self.pool.get("res.users").\
 
109
                                        read(cr, uid, [uid],
 
110
                                             fields=["signature"],
 
111
                                             context=context)[0]
 
112
 
 
113
                        signature = user_id and user_id["signature"] or ''
 
114
                        if signature:
 
115
                            wizard.message = \
 
116
                                   tools.append_content_to_html(wizard.message,
 
117
                                                                signature,
 
118
                                                                plaintext=True,
 
119
                                                                container_tag=\
 
120
                                                                        'div')
 
121
                        # FIXME 8.0: use notification_email_send, send a wall message and let mail handle email notification + message box
 
122
                        for follower_id in new_follower_ids:
 
123
                            mail_mail = self.pool.get('mail.mail')
 
124
                            # the invite wizard should create a private message not related to any object -> no model, no res_id
 
125
                            mail_id = mail_mail.create(cr, uid, {
 
126
                                'model': wizard.res_model,
 
127
                                'res_id': res_id,
 
128
                                'subject': 'Invitation to follow %s' %
 
129
                                                     document.name_get()[0][1],
 
130
                                'body_html': '%s' % wizard.message,
 
131
                                'auto_delete': True,
 
132
                                }, context=context)
 
133
                            mail_mail.send(cr, uid, [mail_id],
 
134
                                           recipient_ids=[follower_id],
 
135
                                           context=context)
 
136
            else:
 
137
                res = super(invite_wizard,self).add_followers(cr, uid, ids,
 
138
                                                              context=context)
 
139
                
 
140
        return res
 
141
 
 
142
 
 
143
    def load_partners(self, cr, uid, ids, mail_groups, check, check2,
 
144
                      context=None):
 
145
        ''' Used to add all partnes in mail.group selected in the view and
 
146
            return it
 
147
        '''
 
148
        if context is None:
 
149
            context = {}
 
150
        res = {'value':{}}
 
151
        mail_obj = self.pool.get('mail.group')
 
152
        partner_ids = []
 
153
        
 
154
        if check or check2:
 
155
            for group in mail_groups: 
 
156
                group_ids = group and len(group) == 3 and group[2] or []
 
157
                for groups in mail_obj.read(cr, uid,
 
158
                                            group_ids,
 
159
                                            ['message_follower_ids'],
 
160
                                            context):
 
161
                    partner_ids += groups.get('message_follower_ids',[])
 
162
 
 
163
        partner_ids and res['value'].update({'partner_ids':partner_ids})
 
164
        return res