82dcb4bc50bebb56d3834a06b16c231bb4670211
[odoo/odoo.git] / addons / mail / mail_group.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2010-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 Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (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 Affero General Public License for more details
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>
19 #
20 ##############################################################################
21
22 import datetime as DT
23 import openerp
24 import openerp.tools as tools
25 from operator import itemgetter
26 from osv import osv
27 from osv import fields
28 import tools
29 from tools.translate import _
30 from lxml import etree
31
32 class mail_group(osv.Model):
33     """
34     A mail_group is a collection of users sharing messages in a discussion
35     group. Group users are users that follow the mail group, using the
36     subscription/follow mechanism of OpenSocial. A mail group has nothing
37     in common with res.users.group.
38     """
39     
40     _description = 'Discussion group'
41     _name = 'mail.group'
42     _inherit = ['mail.thread']
43     _inherits = {'mail.alias': 'alias_id'}
44
45     def _get_image(self, cr, uid, ids, name, args, context=None):
46         result = dict.fromkeys(ids, False)
47         for obj in self.browse(cr, uid, ids, context=context):
48             result[obj.id] = tools.image_get_resized_images(obj.image)
49         return result
50     
51     def _set_image(self, cr, uid, id, name, value, args, context=None):
52         return self.write(cr, uid, [id], {'image': tools.image_resize_image_big(value)}, context=context)
53     
54     def _get_last_month_msg_nbr(self, cr, uid, ids, name, args, context=None):
55         result = {}
56         message_obj = self.pool.get('mail.message')
57         for id in ids:
58             lower_date = (DT.datetime.now() - DT.timedelta(days=30)).strftime(tools.DEFAULT_SERVER_DATE_FORMAT)
59             result[id] = self.message_search(cr, uid, [id], limit=None, domain=[('date', '>=', lower_date)], count=True, context=context)
60         return result
61     
62     def _get_default_image(self, cr, uid, context=None):
63         image_path = openerp.modules.get_module_resource('mail', 'static/src/img', 'groupdefault.png')
64         return tools.image_resize_image_big(open(image_path, 'rb').read().encode('base64'))
65     
66     _columns = {
67         'name': fields.char('Name', size=64, required=True),
68         'description': fields.text('Description'),
69         'responsible_id': fields.many2one('res.users', string='Responsible',
70             ondelete='set null', required=True, select=1,
71             help="Responsible of the group that has all rights on the record."),
72         'public': fields.boolean('Visible by non members', help='This group is visible by non members. \
73             Invisible groups can add members through the invite button.'),
74         'group_ids': fields.many2many('res.groups', rel='mail_group_res_group_rel',
75             id1='mail_group_id', id2='groups_id', string='Linked groups',
76             help="Members of those groups will automatically added as followers. "\
77                     "Note that they will be able to manage their subscription manually "\
78                     "if necessary."),
79         'image': fields.binary("Photo",
80             help="This field holds the image used as photo for the "\
81                  "user. The image is base64 encoded, and PIL-supported. "\
82                  "It is limited to a 12024x1024 px image."),
83         'image_medium': fields.function(_get_image, fnct_inv=_set_image,
84             string="Medium-sized photo", type="binary", multi="_get_image",
85             store = {
86                 'mail.group': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
87             },
88             help="Medium-sized photo of the group. It is automatically "\
89                  "resized as a 180x180px image, with aspect ratio preserved. "\
90                  "Use this field in form views or some kanban views."),
91         'image_small': fields.function(_get_image, fnct_inv=_set_image,
92             string="Small-sized photo", type="binary", multi="_get_image",
93             store = {
94                 'mail.group': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
95             },
96             help="Small-sized photo of the group. It is automatically "\
97                  "resized as a 50x50px image, with aspect ratio preserved. "\
98                  "Use this field anywhere a small image is required."),
99         'last_month_msg_nbr': fields.function(_get_last_month_msg_nbr, type='integer',
100             string='Messages count for last month'),
101         'alias_id': fields.many2one('mail.alias', 'Alias', ondelete="cascade", required=True, 
102                                     help="The email address associated with this group. New emails received will automatically "
103                                          "create new topics."),
104     }
105
106     _defaults = {
107         'public': True,
108         'responsible_id': (lambda s, cr, uid, ctx: uid),
109         'image': _get_default_image,
110     }
111
112     def _subscribe_user_with_group_m2m_command(self, cr, uid, ids, group_ids_command, context=None):
113         # form: {'group_ids': [(3, 10), (3, 3), (4, 10), (4, 3)]} or {'group_ids': [(6, 0, [ids]}
114         user_group_ids = [command[1] for command in group_ids_command if command[0] == 4]
115         user_group_ids += [id for command in group_ids_command if command[0] == 6 for id in command[2]]
116         # retrieve the user member of those groups
117         user_ids = []
118         res_groups_obj = self.pool.get('res.groups')
119         for group in res_groups_obj.browse(cr, uid, user_group_ids, context=context):
120             user_ids += [user.id for user in group.users]
121         # subscribe the users
122         return self.message_subscribe(cr, uid, ids, user_ids, context=context)
123
124     def create(self, cr, uid, vals, context=None):
125         alias_pool = self.pool.get('mail.alias')
126         if not vals.get('alias_id'):
127             name = vals.get('alias_name') or vals['name']
128             alias_id = alias_pool.create_unique_alias(cr, uid, 
129                     {'alias_name': "group_"+name}, 
130                     model_name=self._name, context=context)
131             vals['alias_id'] = alias_id
132         mail_group_id = super(mail_group, self).create(cr, uid, vals, context)
133         alias_pool.write(cr, uid, [vals['alias_id']], {"alias_force_thread_id": mail_group_id}, context)
134        
135         if vals.get('group_ids'):
136             self._subscribe_user_with_group_m2m_command(cr, uid, [mail_group_id], vals.get('group_ids'), context=context)
137
138         return mail_group_id
139
140     def unlink(self, cr, uid, ids, context=None):
141         # Cascade-delete mail aliases as well, as they should not exist without the mail group.
142         mail_alias = self.pool.get('mail.alias')
143         alias_ids = [group.alias_id.id for group in self.browse(cr, uid, ids, context=context) if group.alias_id]
144         res = super(mail_group, self).unlink(cr, uid, ids, context=context)
145         mail_alias.unlink(cr, uid, alias_ids, context=context)
146         return res
147
148     def write(self, cr, uid, ids, vals, context=None):
149         if vals.get('group_ids'):
150             self._subscribe_user_with_group_m2m_command(cr, uid, ids, vals.get('group_ids'), context=context)
151         return super(mail_group, self).write(cr, uid, ids, vals, context=context)
152
153     def action_group_join(self, cr, uid, ids, context=None):
154         return self.message_subscribe(cr, uid, ids, context=context)
155
156     def action_group_leave(self, cr, uid, ids, context=None):
157         return self.message_unsubscribe(cr, uid, ids, context=context)