[IMP] account: bank statement reconciliation widget (part 2)
[odoo/odoo.git] / addons / website_mail / models / mail_message.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2013-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 from openerp import SUPERUSER_ID
23 from openerp.tools import html2plaintext
24 from openerp.tools.translate import _
25 from openerp.osv import osv, fields, expression
26
27 class MailMessage(osv.Model):
28     _inherit = 'mail.message'
29
30     def _get_description_short(self, cr, uid, ids, name, arg, context=None):
31         res = dict.fromkeys(ids, False)
32         for message in self.browse(cr, uid, ids, context=context):
33             if message.subject:
34                 res[message.id] = message.subject
35             else:
36                 plaintext_ct = html2plaintext(message.body)
37                 res[message.id] = plaintext_ct[:30] + '%s' % (' [...]' if len(plaintext_ct) >= 30 else '')
38         return res
39
40     _columns = {
41         'description': fields.function(
42             _get_description_short, type='char',
43             help='Message description: either the subject, or the beginning of the body'
44         ),
45         'website_published': fields.boolean(
46             'Published', help="Visible on the website as a comment"
47         ),
48     }
49
50     def default_get(self, cr, uid, fields_list, context=None):
51         defaults = super(MailMessage, self).default_get(cr, uid, fields_list, context=context)
52
53         # Note: explicitly implemented in default_get() instead of _defaults,
54         # to avoid setting to True for all existing messages during upgrades.
55         # TODO: this default should probably be dynamic according to the model
56         # on which the messages are attached, thus moved to create().
57         if 'website_published' in fields_list:
58             defaults.setdefault('website_published', True)
59
60         return defaults
61
62     def _search(self, cr, uid, args, offset=0, limit=None, order=None,
63                 context=None, count=False, access_rights_uid=None):
64         """ Override that adds specific access rights of mail.message, to restrict
65         messages to published messages for public users. """
66         if uid != SUPERUSER_ID:
67             group_ids = self.pool.get('res.users').browse(cr, uid, uid, context=context).groups_id
68             group_user_id = self.pool.get("ir.model.data").get_object_reference(cr, uid, 'base', 'group_public')[1]
69             if group_user_id in [group.id for group in group_ids]:
70                 args = expression.AND([[('website_published', '=', True)], list(args)])
71
72         return super(MailMessage, self)._search(cr, uid, args, offset=offset, limit=limit, order=order,
73                                                 context=context, count=count, access_rights_uid=access_rights_uid)
74
75     def check_access_rule(self, cr, uid, ids, operation, context=None):
76         """ Add Access rules of mail.message for non-employee user:
77             - read:
78                 - raise if the type is comment and subtype NULL (internal note)
79         """
80         group_ids = self.pool.get('res.users').browse(cr, uid, uid, context=context).groups_id
81         group_user_id = self.pool.get("ir.model.data").get_object_reference(cr, uid, 'base', 'group_public')[1]
82         if group_user_id in [group.id for group in group_ids]:
83             cr.execute('SELECT id FROM "%s" WHERE website_published IS FALSE AND id = ANY (%%s)' % (self._table), (ids,))
84             if cr.fetchall():
85                 raise osv.except_osv(
86                     _('Access Denied'),
87                     _('The requested operation cannot be completed due to security restrictions. Please contact your system administrator.\n\n(Document type: %s, Operation: %s)') % (self._description, operation))
88
89         return super(MailMessage, self).check_access_rule(cr, uid, ids=ids, operation=operation, context=context)