From 9233c9c0db61d53b103f76bcf132281765bc3a5e Mon Sep 17 00:00:00 2001 From: x620 Date: Thu, 25 Feb 2016 11:21:00 +0500 Subject: [PATCH 01/13] 1. Create two Smart buttons (Income and Sent) 2. Calculate income and sent messages and output values on Smart buttons TODO: Create actions when click Smart buttons --- res_partner_mails_count/__init__.py | 3 + res_partner_mails_count/__openerp__.py | 34 ++++++++ res_partner_mails_count/controllers.py | 20 +++++ res_partner_mails_count/demo.xml | 25 ++++++ res_partner_mails_count/models.py | 50 +++++++++++ .../security/ir.model.access.csv | 2 + res_partner_mails_count/templates.xml | 87 +++++++++++++++++++ 7 files changed, 221 insertions(+) create mode 100644 res_partner_mails_count/__init__.py create mode 100644 res_partner_mails_count/__openerp__.py create mode 100644 res_partner_mails_count/controllers.py create mode 100644 res_partner_mails_count/demo.xml create mode 100644 res_partner_mails_count/models.py create mode 100644 res_partner_mails_count/security/ir.model.access.csv create mode 100644 res_partner_mails_count/templates.xml diff --git a/res_partner_mails_count/__init__.py b/res_partner_mails_count/__init__.py new file mode 100644 index 0000000..c7a6ca6 --- /dev/null +++ b/res_partner_mails_count/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- +import controllers +import models \ No newline at end of file diff --git a/res_partner_mails_count/__openerp__.py b/res_partner_mails_count/__openerp__.py new file mode 100644 index 0000000..250b763 --- /dev/null +++ b/res_partner_mails_count/__openerp__.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +{ + 'name': "res_partner_mails_count", + + 'summary': """ + Short (1 phrase/line) summary of the module's purpose, used as + subtitle on modules listing or apps.openerp.com""", + + 'description': """ + Long description of module's purpose + """, + + 'author': "Your Company", + 'website': "http://www.yourcompany.com", + + # Categories can be used to filter modules in modules listing + # Check https://github.com/odoo/odoo/blob/master/openerp/addons/base/module/module_data.xml + # for the full list + 'category': 'Uncategorized', + 'version': '0.1', + + # any module necessary for this one to work correctly + 'depends': ['base'], + + # always loaded + 'data': [ + # 'security/ir.model.access.csv', + 'templates.xml', + ], + # only loaded in demonstration mode + 'demo': [ + 'demo.xml', + ], +} \ No newline at end of file diff --git a/res_partner_mails_count/controllers.py b/res_partner_mails_count/controllers.py new file mode 100644 index 0000000..a1262c1 --- /dev/null +++ b/res_partner_mails_count/controllers.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +from openerp import http + +# class ResPartnerMailsCount(http.Controller): +# @http.route('/res_partner_mails_count/res_partner_mails_count/', auth='public') +# def index(self, **kw): +# return "Hello, world" + +# @http.route('/res_partner_mails_count/res_partner_mails_count/objects/', auth='public') +# def list(self, **kw): +# return http.request.render('res_partner_mails_count.listing', { +# 'root': '/res_partner_mails_count/res_partner_mails_count', +# 'objects': http.request.env['res_partner_mails_count.res_partner_mails_count'].search([]), +# }) + +# @http.route('/res_partner_mails_count/res_partner_mails_count/objects//', auth='public') +# def object(self, obj, **kw): +# return http.request.render('res_partner_mails_count.object', { +# 'object': obj +# }) \ No newline at end of file diff --git a/res_partner_mails_count/demo.xml b/res_partner_mails_count/demo.xml new file mode 100644 index 0000000..94953d2 --- /dev/null +++ b/res_partner_mails_count/demo.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/res_partner_mails_count/models.py b/res_partner_mails_count/models.py new file mode 100644 index 0000000..1d667c0 --- /dev/null +++ b/res_partner_mails_count/models.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- + +from openerp import models, fields, api + +import logging + + +def getlogger(name): + filename = 'test.log' + logger = logging.getLogger(name) + handler = logging.FileHandler(filename) + formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(name)s: %(message)s') + + handler.setFormatter(formatter) + logger.addHandler(handler) + logger.setLevel(logging.DEBUG) + + return logger + +logger = getlogger(__name__) + + +class res_partner(models.Model): + _inherit = 'res.partner' + income = fields.Integer(compute="_income") + sent = fields.Integer(compute="_sent") + + def _search_partner(self): + return self.env['res.partner'].search([('user_ids', '=', self._uid)]) + + def _search_letters(self, partner, param): + letters = self.env['mail.message'].search([(param, '=', partner.id)]) + # logger.debug('partner.id: %s' % partner.id) + # logger.debug('Income letters: %s' % letters) + return letters + + @api.multi + def _income(self): + partner = self._search_partner() + if partner: + letters = self._search_letters(partner, 'partner_ids') + partner.income = len(letters) + + @api.multi + def _sent(self): + partner = self._search_partner() + if partner: + letters = self._search_letters(partner, 'author_id') + partner.sent = len(letters) + diff --git a/res_partner_mails_count/security/ir.model.access.csv b/res_partner_mails_count/security/ir.model.access.csv new file mode 100644 index 0000000..99d8f27 --- /dev/null +++ b/res_partner_mails_count/security/ir.model.access.csv @@ -0,0 +1,2 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_res_partner_mails_count_res_partner_mails_count,res_partner_mails_count.res_partner_mails_count,model_res_partner_mails_count_res_partner_mails_count,,1,0,0,0 \ No newline at end of file diff --git a/res_partner_mails_count/templates.xml b/res_partner_mails_count/templates.xml new file mode 100644 index 0000000..abf0199 --- /dev/null +++ b/res_partner_mails_count/templates.xml @@ -0,0 +1,87 @@ + + + + + res.partner.mails.count.income + res.partner + + + + + + + + + + + res.partner.mails.count.sent + res.partner + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From b07f52f9e37c65d43173b4a11c3741bd04efcc22 Mon Sep 17 00:00:00 2001 From: x620 Date: Tue, 22 Mar 2016 16:57:40 +0500 Subject: [PATCH 02/13] Corrected wrong calculated mails count on the smart buttons --- res_partner_mails_count/__openerp__.py | 3 +- res_partner_mails_count/models.py | 40 +++++++++----------------- res_partner_mails_count/templates.xml | 28 ++++++------------ 3 files changed, 25 insertions(+), 46 deletions(-) diff --git a/res_partner_mails_count/__openerp__.py b/res_partner_mails_count/__openerp__.py index 250b763..8e67f6b 100644 --- a/res_partner_mails_count/__openerp__.py +++ b/res_partner_mails_count/__openerp__.py @@ -20,11 +20,12 @@ 'version': '0.1', # any module necessary for this one to work correctly - 'depends': ['base'], + 'depends': ['base', 'mail_sent'], # always loaded 'data': [ # 'security/ir.model.access.csv', + 'views/res_partner_mails_count.xml', 'templates.xml', ], # only loaded in demonstration mode diff --git a/res_partner_mails_count/models.py b/res_partner_mails_count/models.py index 1d667c0..f47cc99 100644 --- a/res_partner_mails_count/models.py +++ b/res_partner_mails_count/models.py @@ -22,29 +22,17 @@ logger = getlogger(__name__) class res_partner(models.Model): _inherit = 'res.partner' - income = fields.Integer(compute="_income") - sent = fields.Integer(compute="_sent") - - def _search_partner(self): - return self.env['res.partner'].search([('user_ids', '=', self._uid)]) - - def _search_letters(self, partner, param): - letters = self.env['mail.message'].search([(param, '=', partner.id)]) - # logger.debug('partner.id: %s' % partner.id) - # logger.debug('Income letters: %s' % letters) - return letters - - @api.multi - def _income(self): - partner = self._search_partner() - if partner: - letters = self._search_letters(partner, 'partner_ids') - partner.income = len(letters) - - @api.multi - def _sent(self): - partner = self._search_partner() - if partner: - letters = self._search_letters(partner, 'author_id') - partner.sent = len(letters) - + mails_from = fields.Integer(compute="_mails_from") + mails_to = fields.Integer(compute="_mails_to") + + @api.one + def _mails_from(self): + for r in self: + letters = self.env['mail.message'].search([('partner_ids', 'in', r.id)]) + self.mails_from = len(letters) + + @api.one + def _mails_to(self): + for r in self: + letters = self.env['mail.message'].search([('author_id', '=', r.id)]) + self.mails_to = len(letters) diff --git a/res_partner_mails_count/templates.xml b/res_partner_mails_count/templates.xml index abf0199..2cfcc3a 100644 --- a/res_partner_mails_count/templates.xml +++ b/res_partner_mails_count/templates.xml @@ -2,39 +2,29 @@ - res.partner.mails.count.income + res.partner.mails.count res.partner - - - - - - res.partner.mails.count.sent - res.partner - - - - - - - From 63aa83fe95af04ce9f39c8cffbac98dbc6d93411 Mon Sep 17 00:00:00 2001 From: x620 Date: Tue, 22 Mar 2016 17:02:51 +0500 Subject: [PATCH 03/13] Add views/res_partner_mails_count.xml which contains actions --- .../views/res_partner_mails_count.xml | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 res_partner_mails_count/views/res_partner_mails_count.xml diff --git a/res_partner_mails_count/views/res_partner_mails_count.xml b/res_partner_mails_count/views/res_partner_mails_count.xml new file mode 100644 index 0000000..5731ffc --- /dev/null +++ b/res_partner_mails_count/views/res_partner_mails_count.xml @@ -0,0 +1,54 @@ + + + + + Mails from + mail.wall + mail.message + { + 'search_default_model': False, + 'default_model': 'res.users', + 'default_res_id': uid, + 'thread_model': 'res.partner', + 'active_model': False, + } + + +

+ No private message. +

+ This list contains messages sent to you. +

+
+
+ + Mails to + mail.wall + { + 'default_model': 'res.users', + 'default_res_id': uid, + 'thread_model': 'res.partner', + 'needaction_menu_ref': ['mail.mail_tomefeeds'] + } + + +

+ No message found and no message sent yet. +

+ Click on the top-right icon to compose a message. This + message will be sent by email if it's an internal contact. +

+
+
+
+
From e91db8d44057fb4849474d6adfb0be25d16ca5dc Mon Sep 17 00:00:00 2001 From: x620 Date: Tue, 22 Mar 2016 17:19:52 +0500 Subject: [PATCH 04/13] During calculate mails count added sudo() and change search() -> search_count() --- res_partner_mails_count/models.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/res_partner_mails_count/models.py b/res_partner_mails_count/models.py index f47cc99..8befe5a 100644 --- a/res_partner_mails_count/models.py +++ b/res_partner_mails_count/models.py @@ -28,11 +28,9 @@ class res_partner(models.Model): @api.one def _mails_from(self): for r in self: - letters = self.env['mail.message'].search([('partner_ids', 'in', r.id)]) - self.mails_from = len(letters) + self.mails_from = self.env['mail.message'].sudo().search_count([('partner_ids', 'in', r.id)]) @api.one def _mails_to(self): for r in self: - letters = self.env['mail.message'].search([('author_id', '=', r.id)]) - self.mails_to = len(letters) + self.mails_to = self.env['mail.message'].sudo().search_count([('author_id', '=', r.id)]) From 9c6e120d0c03668235ef717ba22683b4720d4d3f Mon Sep 17 00:00:00 2001 From: x620 Date: Tue, 22 Mar 2016 17:55:02 +0500 Subject: [PATCH 05/13] Corrected count display letters --- res_partner_mails_count/__openerp__.py | 2 +- res_partner_mails_count/templates.xml | 51 +------------------ .../views/res_partner_mails_count.xml | 41 ++++----------- 3 files changed, 11 insertions(+), 83 deletions(-) diff --git a/res_partner_mails_count/__openerp__.py b/res_partner_mails_count/__openerp__.py index 8e67f6b..f2292f1 100644 --- a/res_partner_mails_count/__openerp__.py +++ b/res_partner_mails_count/__openerp__.py @@ -20,7 +20,7 @@ 'version': '0.1', # any module necessary for this one to work correctly - 'depends': ['base', 'mail_sent'], + 'depends': ['base'], # always loaded 'data': [ diff --git a/res_partner_mails_count/templates.xml b/res_partner_mails_count/templates.xml index 2cfcc3a..5adf411 100644 --- a/res_partner_mails_count/templates.xml +++ b/res_partner_mails_count/templates.xml @@ -17,61 +17,12 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
\ No newline at end of file diff --git a/res_partner_mails_count/views/res_partner_mails_count.xml b/res_partner_mails_count/views/res_partner_mails_count.xml index 5731ffc..d9f01d0 100644 --- a/res_partner_mails_count/views/res_partner_mails_count.xml +++ b/res_partner_mails_count/views/res_partner_mails_count.xml @@ -1,52 +1,29 @@ - + Mails from - mail.wall mail.message + tree,form { - 'search_default_model': False, - 'default_model': 'res.users', - 'default_res_id': uid, - 'thread_model': 'res.partner', - 'active_model': False, + 'active_test': False, } -

- No private message. -

- This list contains messages sent to you. + Mails from

- + Mails to - mail.wall + mail.message + tree,form { - 'default_model': 'res.users', - 'default_res_id': uid, - 'thread_model': 'res.partner', - 'needaction_menu_ref': ['mail.mail_tomefeeds'] + 'active_test': False, } -

- No message found and no message sent yet. -

- Click on the top-right icon to compose a message. This - message will be sent by email if it's an internal contact. + Mails to

From ee328d4332cb243aa53b85d41d164dc8ed0e521c Mon Sep 17 00:00:00 2001 From: x620 Date: Tue, 22 Mar 2016 18:06:29 +0500 Subject: [PATCH 06/13] Return mail_sent in __openerp__.py --- res_partner_mails_count/__openerp__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/res_partner_mails_count/__openerp__.py b/res_partner_mails_count/__openerp__.py index f2292f1..8e67f6b 100644 --- a/res_partner_mails_count/__openerp__.py +++ b/res_partner_mails_count/__openerp__.py @@ -20,7 +20,7 @@ 'version': '0.1', # any module necessary for this one to work correctly - 'depends': ['base'], + 'depends': ['base', 'mail_sent'], # always loaded 'data': [ From dfa270255e4a189d0fd6df8a34b615027bb4a062 Mon Sep 17 00:00:00 2001 From: x620 Date: Tue, 22 Mar 2016 18:11:27 +0500 Subject: [PATCH 07/13] Add mail to depends in __openerp__.py --- res_partner_mails_count/__openerp__.py | 2 +- res_partner_mails_count/templates.xml | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/res_partner_mails_count/__openerp__.py b/res_partner_mails_count/__openerp__.py index 8e67f6b..0af6711 100644 --- a/res_partner_mails_count/__openerp__.py +++ b/res_partner_mails_count/__openerp__.py @@ -20,7 +20,7 @@ 'version': '0.1', # any module necessary for this one to work correctly - 'depends': ['base', 'mail_sent'], + 'depends': ['base', 'mail'], # always loaded 'data': [ diff --git a/res_partner_mails_count/templates.xml b/res_partner_mails_count/templates.xml index 5adf411..1cae718 100644 --- a/res_partner_mails_count/templates.xml +++ b/res_partner_mails_count/templates.xml @@ -6,7 +6,6 @@ res.partner - diff --git a/res_partner_mails_count/views/res_partner_mails_count.xml b/res_partner_mails_count/views/res_partner_mails_count.xml index d9f01d0..8d048e6 100644 --- a/res_partner_mails_count/views/res_partner_mails_count.xml +++ b/res_partner_mails_count/views/res_partner_mails_count.xml @@ -1,29 +1,17 @@ - - Mails from + + Mails + mail.wall mail.message tree,form { - 'active_test': False, + 'ignore_search_model': True, }

- Mails from -

-
-
- - Mails to - mail.message - tree,form - { - 'active_test': False, - } - -

- Mails to + Mails not found. Probably, they exist, but you don't have access.

From f0678b2943568a7948067fb22446ead72b9d34d3 Mon Sep 17 00:00:00 2001 From: x620 Date: Wed, 23 Mar 2016 12:33:59 +0500 Subject: [PATCH 10/13] Removed additional filter by model --- res_partner_mails_count/static/src/js/main.js | 8 ++++++++ res_partner_mails_count/templates.xml | 10 +++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 res_partner_mails_count/static/src/js/main.js diff --git a/res_partner_mails_count/static/src/js/main.js b/res_partner_mails_count/static/src/js/main.js new file mode 100644 index 0000000..78fcd66 --- /dev/null +++ b/res_partner_mails_count/static/src/js/main.js @@ -0,0 +1,8 @@ +openerp.res_partner_mails_count = function(instance){ + instance.mail.Wall.include({ + init: function(){ + this._super.apply(this, arguments); + delete this.defaults.model; + } + }); +}; diff --git a/res_partner_mails_count/templates.xml b/res_partner_mails_count/templates.xml index 8339909..a429476 100644 --- a/res_partner_mails_count/templates.xml +++ b/res_partner_mails_count/templates.xml @@ -1,7 +1,15 @@ - + + res.partner.mails.count res.partner From c32e2c9aadf5c3a201bbad45c319dcf447751565 Mon Sep 17 00:00:00 2001 From: x620 Date: Wed, 23 Mar 2016 12:47:37 +0500 Subject: [PATCH 11/13] Removed from ir.actions.client view_mode field --- res_partner_mails_count/views/res_partner_mails_count.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/res_partner_mails_count/views/res_partner_mails_count.xml b/res_partner_mails_count/views/res_partner_mails_count.xml index 8d048e6..ce339a6 100644 --- a/res_partner_mails_count/views/res_partner_mails_count.xml +++ b/res_partner_mails_count/views/res_partner_mails_count.xml @@ -5,7 +5,6 @@ Mails mail.wall mail.message - tree,form { 'ignore_search_model': True, } From d626aa26b406bf65340f8a1e86968de273241ad1 Mon Sep 17 00:00:00 2001 From: Ilyas Date: Wed, 23 Mar 2016 17:27:28 +0500 Subject: [PATCH 12/13] [IMP] Add test --- res_partner_mails_count/tests/__init__.py | 22 ++++++++++++++ res_partner_mails_count/tests/test_mail.py | 35 ++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 res_partner_mails_count/tests/__init__.py create mode 100644 res_partner_mails_count/tests/test_mail.py diff --git a/res_partner_mails_count/tests/__init__.py b/res_partner_mails_count/tests/__init__.py new file mode 100644 index 0000000..0bc1d87 --- /dev/null +++ b/res_partner_mails_count/tests/__init__.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Business Applications +# Copyright (C) 2013-Today OpenERP SA () +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see +# +############################################################################## + +from . import test_mail diff --git a/res_partner_mails_count/tests/test_mail.py b/res_partner_mails_count/tests/test_mail.py new file mode 100644 index 0000000..4fc6383 --- /dev/null +++ b/res_partner_mails_count/tests/test_mail.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- + +from openerp.tests.common import TransactionCase + + +class test_message_count(TransactionCase): + post_install = True + def test_count(self): + new_partner1 = self.env['res.partner'].sudo().create({'name': 'rpmc Test Partner one', 'email': 'tt@tt', 'notify_email': 'always'}) + new_partner2 = self.env['res.partner'].sudo().create({'name': 'rpmc Test Partner two', 'email': 'rr@rr', 'notify_email': 'always'}) + self.assertEqual(new_partner1.mails_to, 0, 'rpmc: new partner have mails_to != 0') + mail_compose = self.env['mail.compose.message'] + compose = mail_compose.with_context( + { + 'default_composition_mode': 'comment', + 'default_model': 'res.partner', + 'default_res_id': new_partner1.id, + }).create( + { + 'subject': 'test subj', + 'body': 'test body', + 'partner_ids': [(4, new_partner2.id)], + 'email_from': 'tt@tt', + 'author_id': new_partner1.id + }) + compose.send_mail() + self.assertEqual(new_partner1.mails_to, 0) + self.assertEqual(new_partner1.mails_from, 1, 'rpmc: one message but mails_from != 1') + self.assertEqual(new_partner2.mails_to, 1, 'rpmc: one message but mails_to != 1') + self.assertEqual(new_partner2.mails_from, 0) + compose.send_mail() + self.assertEqual(new_partner1.mails_to, 0) + self.assertEqual(new_partner1.mails_from, 2, 'rpmc: one message but mails_from != 2') + self.assertEqual(new_partner2.mails_to, 2, 'rpmc: one message but mails_to != 2') + self.assertEqual(new_partner2.mails_from, 0) \ No newline at end of file From 58060c1cef36a1622e107594768bb6e78ff67808 Mon Sep 17 00:00:00 2001 From: Ilyas Date: Wed, 23 Mar 2016 17:32:19 +0500 Subject: [PATCH 13/13] [FIX] bad copypaste --- res_partner_mails_count/tests/__init__.py | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/res_partner_mails_count/tests/__init__.py b/res_partner_mails_count/tests/__init__.py index 0bc1d87..300df16 100644 --- a/res_partner_mails_count/tests/__init__.py +++ b/res_partner_mails_count/tests/__init__.py @@ -1,22 +1,3 @@ # -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Business Applications -# Copyright (C) 2013-Today OpenERP SA () -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see -# -############################################################################## from . import test_mail