From 115e6f9687d14137293c80d2298263a2656fd309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Tue, 28 Aug 2012 13:31:28 +0200 Subject: [PATCH] [IMP] [TEST] mail.notification: added an SQL constraint on mail.notification, to have default false value on read column even when writing into the table using write on the partner_ids column of mail.message (therefore by-passing the default_get of the orm for mail_notification model). Added tests for needaction, based on mail.message, that by the way helped trigerred this bug. Hooray. bzr revid: tde@openerp.com-20120828113128-70gjunh6bx3pho30 --- addons/mail/mail_followers.py | 35 +++++++++++++++++++++++----------- addons/mail/tests/test_mail.py | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 11 deletions(-) diff --git a/addons/mail/mail_followers.py b/addons/mail/mail_followers.py index 129f4b479b7..f54b2bd0317 100644 --- a/addons/mail/mail_followers.py +++ b/addons/mail/mail_followers.py @@ -24,12 +24,12 @@ from osv import fields class mail_followers(osv.Model): """ mail_followers holds the data related to the follow mechanism inside - OpenERP. Partners can choose to follow documents (records) of any kind that - inherits from mail.thread. Following documents allow to receive - notifications for new messages. - A subscription is characterized by: - :param: res_model: model of the followed objects - :param: res_id: ID of resource (may be 0 for every objects) + OpenERP. Partners can choose to follow documents (records) of any kind + that inherits from mail.thread. Following documents allow to receive + notifications for new messages. + A subscription is characterized by: + :param: res_model: model of the followed objects + :param: res_id: ID of resource (may be 0 for every objects) """ _name = 'mail.followers' _rec_name = 'partner_id' @@ -48,8 +48,7 @@ class mail_followers(osv.Model): class mail_notification(osv.Model): """ Class holding notifications pushed to partners. Followers and partners - added in 'contacts to notify' receive notifications. Hiding notifications - in the wall consists in setting them as 'read'. """ + added in 'contacts to notify' receive notifications. """ _name = 'mail.notification' _rec_name = 'partner_id' _log_access = False @@ -64,9 +63,23 @@ class mail_notification(osv.Model): _defaults = { 'read': False, } - # FP Note: todo: check that we can not create a notification for - # a message we can not read. - # def create(self, ...) + + def init(self, cr): + """ Set a postgresql NOT NULL constraint with default value false for + the read column. The reason is that when writing in this table using + partner_ids of mail.message model, it bypasses the ORM default + values, leading to 'None' values for read field. This broke the + needaction mechanism for mail.message. """ + cr.execute("ALTER TABLE mail_notification ALTER read SET NOT NULL") + cr.execute("ALTER TABLE mail_notification ALTER read SET DEFAULT false") + + def create(self, cr, uid, vals, context=None): + """ Override of create to check that we can not create a notification + for a message the user can not read. """ + if self.pool.get('mail.message').check_access_rights(cr, uid, 'read'): + return super(self, mail_notification).create(cr, uid, vals, context=context) + else: + return False def notify(self, cr, uid, partner_ids, msg_id, context=None): """ Send by email the notification depending on the user preferences """ diff --git a/addons/mail/tests/test_mail.py b/addons/mail/tests/test_mail.py index f425007b4c1..bd1134f2bdc 100644 --- a/addons/mail/tests/test_mail.py +++ b/addons/mail/tests/test_mail.py @@ -344,3 +344,38 @@ class test_mail(common.TransactionCase): self.assertTrue(len(res[0]['child_ids'][0]['child_ids']) == 1, 'Incorrect number of child in message_read') # trees = self.mail_message.message_read_tree_flatten_main(cr, uid, res, thread_level=0) # print trees + + def test_40_needaction(self): + """ Tests for mail.message needaction. """ + cr, uid = self.cr, self.uid + group_pigs = self.mail_group.browse(cr, uid, self.group_pigs_id) + user_admin = self.res_users.browse(cr, uid, uid) + + # Demo values: check unread notification = needaction on mail.message + notif_ids = self.mail_notification.search(cr, uid, [ + ('partner_id', '=', user_admin.partner_id.id), + ('read', '=', False) + ]) + na_count = self.mail_message._needaction_count(cr, uid, domain = []) + self.assertTrue(len(notif_ids) == na_count, + 'Number of unread notifications (%s) does not match the needaction count (%s)' % (len(notif_ids), na_count)) + + # Post 4 message on group_pigs + msgid1 = group_pigs.message_post(body='My Body') + msgid2 = group_pigs.message_post(body='My Body') + msgid3 = group_pigs.message_post(body='My Body') + msgid4 = group_pigs.message_post(body='My Body') + + # Check there are 4 new needaction on mail.message + notif_ids = self.mail_notification.search(cr, uid, [ + ('partner_id', '=', user_admin.partner_id.id), + ('read', '=', False) + ]) + na_count = self.mail_message._needaction_count(cr, uid, domain = []) + self.assertTrue(len(notif_ids) == na_count, + 'Number of unread notifications after posting messages (%s) does not match the needaction count (%s)' % (len(notif_ids), na_count)) + + # Check there are 4 needaction on mail.message with particular domain + na_count = self.mail_message._needaction_count(cr, uid, domain = [('model', '=', 'mail.group'), ('res_id', '=', self.group_pigs_id)]) + self.assertTrue(na_count == 4, + 'Number of posted message (4) does not match the needaction count with domain mail.group - group pigs (%s)' % (na_count))