diff --git a/mozaik_coordinate/abstract_coordinate.py b/mozaik_coordinate/abstract_coordinate.py index eb51fca4b..1f77031bb 100644 --- a/mozaik_coordinate/abstract_coordinate.py +++ b/mozaik_coordinate/abstract_coordinate.py @@ -462,19 +462,24 @@ def get_fields_to_update(self, cr, uid, mode, context=None): }) return res - def action_invalidate(self, cr, uid, ids, context=None, vals=None): - context = context.copy() if context else {} + def ensure_one_main_coordinate(self, cr, uid, ids, invalidate=False, + vals=False, context=None): + ''' + This method ensure that a main coordinate will remain after + an action + ''' + context = context.copy() or {} + limit = 1 if not invalidate else False rejected_ids = [] for coordinate in self.browse(cr, uid, ids, context=context): coord_ids = self.search( cr, uid, [('partner_id', '=', coordinate.partner_id.id), ('coordinate_type', '=', coordinate.coordinate_type), - ('id', '!=', coordinate.id)], context=context) + ('id', '!=', coordinate.id)], limit=limit, + context=context) if coordinate.is_main and len(coord_ids) == 1: - # only one coordinate will remain after invalidate -> set it - # automatically as new main new_main = self.browse(cr, uid, coord_ids[0], context=context) - context['invalidate'] = True + context['invalidate'] = invalidate coordinate_field = self._discriminant_field coordinate_value = self._is_discriminant_m2o() and \ new_main[coordinate_field].id or \ @@ -486,5 +491,11 @@ def action_invalidate(self, cr, uid, ids, context=None, vals=None): self.write(cr, uid, coordinate.id, vals, context=context) else: rejected_ids.append(coordinate.id) + return rejected_ids + + def action_invalidate(self, cr, uid, ids, context=None, vals=None): + context = context.copy() if context else {} + rejected_ids = self.ensure_one_main_coordinate( + cr, uid, ids, invalidate=True, context=context) return super(abstract_coordinate, self). action_invalidate( cr, uid, rejected_ids, context=context, vals=vals) diff --git a/mozaik_phone/__openerp__.py b/mozaik_phone/__openerp__.py index c67dcefb4..a2de29f37 100644 --- a/mozaik_phone/__openerp__.py +++ b/mozaik_phone/__openerp__.py @@ -52,6 +52,7 @@ 'wizard/change_main_phone.xml', 'wizard/allow_duplicate_view.xml', 'wizard/bounce_editor_view.xml', + 'wizard/change_phone_type.xml', ], 'qweb': [ ], diff --git a/mozaik_phone/i18n/fr.po b/mozaik_phone/i18n/fr.po index 1f9badc30..247cd1e6d 100644 --- a/mozaik_phone/i18n/fr.po +++ b/mozaik_phone/i18n/fr.po @@ -1,6 +1,6 @@ # Translation of Odoo Server. # This file contains the translation of the following modules: -# * mozaik_phone +# * mozaik_phone # msgid "" msgstr "" @@ -49,6 +49,16 @@ msgstr "Autoriser les doublons" msgid "Also for Fax" msgstr "Également pour fax" +#. module: mozaik_phone +#: view:change.phone.type:mozaik_phone.change_phone_type_form +msgid "Cancel" +msgstr "Annuler" + +#. module: mozaik_phone +#: view:change.phone.type:mozaik_phone.change_phone_type_form +msgid "Change" +msgstr "Modifier" + #. module: mozaik_phone #: field:change.main.phone,change_allowed:0 msgid "Change Allowed" @@ -64,6 +74,17 @@ msgstr "Changer le téléphone principal" msgid "Change Main Phone Wizard" msgstr "Assistant de changement de téléphone principal" +#. module: mozaik_phone +#: view:change.phone.type:mozaik_phone.change_phone_type_form +#: model:ir.actions.act_window,name:mozaik_phone.change_phone_type_action +msgid "Change Phone Type" +msgstr "Changement du type de téléphone" + +#. module: mozaik_phone +#: model:ir.model,name:mozaik_phone.model_change_phone_type +msgid "Change Phone Type Wizard" +msgstr "Assistant de changement de type de téléphone" + #. module: mozaik_phone #: field:phone.coordinate,partner_id:0 msgid "Contact" @@ -81,6 +102,7 @@ msgstr "Coordonnées" #. module: mozaik_phone #: field:change.main.phone,create_uid:0 +#: field:change.phone.type,create_uid:0 #: field:phone.coordinate,create_uid:0 #: field:phone.phone,create_uid:0 msgid "Created by" @@ -88,6 +110,7 @@ msgstr "Créé par" #. module: mozaik_phone #: field:change.main.phone,create_date:0 +#: field:change.phone.type,create_date:0 msgid "Created on" msgstr "Date de création" @@ -127,6 +150,7 @@ msgid "Failures Counter" msgstr "Compteur d'échecs" #. module: mozaik_phone +#: selection:change.phone.type,type:0 #: selection:phone.coordinate,coordinate_type:0 #: selection:phone.phone,type:0 #: field:res.partner,fax_coordinate_id:0 @@ -139,6 +163,7 @@ msgid "Faxes" msgstr "Fax" #. module: mozaik_phone +#: selection:change.phone.type,type:0 #: selection:phone.coordinate,coordinate_type:0 #: selection:phone.phone,type:0 msgid "Fix" @@ -169,6 +194,7 @@ msgstr "Contient le résumé de la discussion (nombre de messages, ...). Ce rés #. module: mozaik_phone #: field:change.main.phone,id:0 +#: field:change.phone.type,id:0 #: field:phone.coordinate,id:0 #: field:phone.phone,id:0 msgid "ID" @@ -236,6 +262,7 @@ msgstr "Date du dernier message" #. module: mozaik_phone #: field:change.main.phone,write_uid:0 +#: field:change.phone.type,write_uid:0 #: field:phone.coordinate,write_uid:0 #: field:phone.phone,write_uid:0 msgid "Last Updated by" @@ -243,6 +270,7 @@ msgstr "Dernière mise à jour par" #. module: mozaik_phone #: field:change.main.phone,write_date:0 +#: field:change.phone.type,write_date:0 #: field:phone.coordinate,write_date:0 #: field:phone.phone,write_date:0 msgid "Last Updated on" @@ -267,6 +295,7 @@ msgid "Messages and communication history" msgstr "Historique des messages et communications" #. module: mozaik_phone +#: selection:change.phone.type,type:0 #: selection:phone.coordinate,coordinate_type:0 #: selection:phone.phone,type:0 #: field:res.partner,mobile_coordinate_id:0 @@ -283,6 +312,12 @@ msgstr "Plus d'info" msgid "New Main Phone" msgstr "Nouveau téléphone principal" +#. module: mozaik_phone +#: code:addons/mozaik_phone/wizard/change_phone_type.py:58 +#, python-format +msgid "New phone type should be different than current one!" +msgstr "Le nouveau type de téléphone doit être différent de l'actuel!" + #. module: mozaik_phone #: field:phone.phone,name:0 msgid "Number" @@ -295,6 +330,7 @@ msgid "Partner" msgstr "Partenaire" #. module: mozaik_phone +#: field:change.phone.type,phone_id:0 #: view:phone.coordinate:mozaik_phone.phone_coordinate_search_view #: field:phone.coordinate,phone_id:0 #: view:phone.phone:mozaik_phone.phone_phone_form_view @@ -346,6 +382,7 @@ msgid "Phones" msgstr "Téléphones" #. module: mozaik_phone +#: field:change.phone.type,is_main:0 #: model:ir.actions.act_window,name:mozaik_phone.set_as_main_action msgid "Set as main" msgstr "Positionner comme principale" @@ -362,6 +399,7 @@ msgid "This Phone Coordinate seems to be down" msgstr "Cette coordonnée téléphonique semble obsolète" #. module: mozaik_phone +#: field:change.phone.type,type:0 #: view:phone.phone:mozaik_phone.phone_phone_search_view #: field:phone.phone,type:0 msgid "Type" @@ -407,10 +445,14 @@ msgstr "Messages de site web" msgid "Website communication history" msgstr "Historique de communication de site web" +#. module: mozaik_phone +#: view:change.phone.type:mozaik_phone.change_phone_type_form +msgid "or" +msgstr "ou" + #. module: mozaik_phone #: view:phone.coordinate:mozaik_phone.phone_coordinate_form_view msgid "{'search_default_duplicate': True,\n" " 'search_default_phone_id': phone_id,\n" " 'default_phone_id': phone_id}" msgstr "" - diff --git a/mozaik_phone/tests/__init__.py b/mozaik_phone/tests/__init__.py index f21220fa0..f1d3e29b9 100644 --- a/mozaik_phone/tests/__init__.py +++ b/mozaik_phone/tests/__init__.py @@ -26,3 +26,4 @@ from . import test_phone_coordinate from . import test_phone_coordinate_wizard from . import test_res_partner +from . import test_change_phone_type diff --git a/mozaik_phone/tests/data/phone_data.xml b/mozaik_phone/tests/data/phone_data.xml index c8367275e..28de09705 100644 --- a/mozaik_phone/tests/data/phone_data.xml +++ b/mozaik_phone/tests/data/phone_data.xml @@ -24,11 +24,31 @@ mobile + + +32 473 78 10 79 + mobile + + + + +32 473 78 10 80 + mobile + + 061548799 fax + + 063458978 + fix + + + + 063458688 + fix + + 061458978 fix @@ -96,5 +116,25 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/mozaik_phone/tests/test_change_phone_type.py b/mozaik_phone/tests/test_change_phone_type.py new file mode 100644 index 000000000..0d489ffe1 --- /dev/null +++ b/mozaik_phone/tests/test_change_phone_type.py @@ -0,0 +1,141 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# This file is part of mozaik_phone, an Odoo module. +# +# Copyright (c) 2015 ACSONE SA/NV () +# +# mozaik_phone 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. +# +# mozaik_phone 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 mozaik_phone. +# If not, see . +# +############################################################################## +from anybox.testing.openerp import SharedSetupTransactionCase +import logging +_logger = logging.getLogger(__name__) + + +class testChangePhoneType(SharedSetupTransactionCase): + + _data_files = ( + '../../mozaik_base/tests/data/res_partner_data.xml', + 'data/phone_data.xml', + ) + + _module_ns = 'mozaik_phone' + + def setUp(self): + super(testChangePhoneType, self).setUp() + self.env.invalidate_all() + self.mobile_four = self.browse_ref('%s.mobile_four' + % self._module_ns) + self.mobile_five = self.browse_ref('%s.mobile_five' + % self._module_ns) + self.fix_one = self.browse_ref('%s.fix_one' + % self._module_ns) + self.fix_two = self.browse_ref('%s.fix_two' + % self._module_ns) + self.coord_mobile_1 = self.browse_ref( + '%s.mobile_coordinate_for_jacques_1' % self._module_ns) + self.coord_mobile_2 = self.browse_ref( + '%s.mobile_coordinate_for_jacques_2' % self._module_ns) + self.coord_fix_1 = self.browse_ref('%s.fix_coordinate_for_jacques_1' + % self._module_ns) + self.coord_fix_2 = self.browse_ref('%s.fix_coordinate_for_jacques_2' + % self._module_ns) + self.wiz_obj = self.env['change.phone.type'] + + def test_change_main_mobile_to_fix_main(self): + ''' + Trying to change mobile_four (main) from mobile to fix and + set is as main in the new category. + Expected results are: + - mobile_four type should be FIX + - mobile_coordinate_for_jacques_1 type should be FIX and MAIN + - mobile_coordinate_for_jacques_2 should become main + - fix_coordinate_for_jacques_1 should not be main anymore + ''' + wiz = self.wiz_obj.create({'phone_id': self.mobile_four.id, + 'type': 'fix'}) + wiz.change_phone_type() + self.env.invalidate_all() + self.assertEqual(self.mobile_four.type, 'fix') + self.assertEqual(self.coord_mobile_1.coordinate_type, 'fix') + self.assertTrue(self.coord_mobile_2.is_main) + self.assertTrue(self.coord_mobile_1.is_main) + self.assertFalse(self.coord_fix_1.is_main) + + def test_change_main_mobile_to_fix_no_main(self): + ''' + Trying to change mobile_four (main) from mobile to fix but + do not set it as main in the new category. + Expected results are: + - mobile_four type should be FIX + - mobile_coordinate_for_jacques_1 type should be FIX and not MAIN + - mobile_coordinate_for_jacques_2 should become main + - fix_coordinate_for_jacques_1 should be main anymore + ''' + wiz = self.wiz_obj.create({'phone_id': self.mobile_four.id, + 'type': 'fix', + 'is_main': False}) + wiz.change_phone_type() + self.env.invalidate_all() + self.assertEqual(self.mobile_four.type, 'fix') + self.assertEqual(self.coord_mobile_1.coordinate_type, 'fix') + self.assertTrue(self.coord_mobile_2.is_main) + self.assertFalse(self.coord_mobile_1.is_main) + self.assertTrue(self.coord_fix_1.is_main) + + def test_change_not_main_mobile_to_fix_main(self): + ''' + Trying to change mobile_five (not main) from mobile to fix and + set is as main in the new category. + Expected results are: + - mobile_five type should be FIX + - mobile_coordinate_for_jacques_2 type should be FIX and MAIN + - mobile_coordinate_for_jacques_1 should remain main + - fix_coordinate_for_jacques_1 should not be main anymore + ''' + wiz = self.wiz_obj.create({'phone_id': self.mobile_five.id, + 'type': 'fix'}) + wiz.change_phone_type() + self.env.invalidate_all() + self.assertEqual(self.mobile_five.type, 'fix') + self.assertEqual(self.coord_mobile_2.coordinate_type, 'fix') + self.assertTrue(self.coord_mobile_1.is_main) + self.assertTrue(self.coord_mobile_2.is_main) + self.assertFalse(self.coord_fix_1.is_main) + + def test_change_not_main_mobile_to_fix_no_main(self): + ''' + Trying to change mobile_five (not main) from mobile to fix and + do not set is as main in the new category. + Expected results are: + - mobile_five type should be FIX + - mobile_coordinate_for_jacques_2 type should be FIX and should + remain not main + - mobile_coordinate_for_jacques_1 should remain main + - fix_coordinate_for_jacques_1 should remain main + ''' + wiz = self.wiz_obj.create({'phone_id': self.mobile_five.id, + 'type': 'fix', + 'is_main': False}) + wiz.change_phone_type() + self.env.invalidate_all() + self.assertEqual(self.mobile_five.type, 'fix') + self.assertEqual(self.coord_mobile_2.coordinate_type, 'fix') + self.assertTrue(self.coord_mobile_1.is_main) + self.assertFalse(self.coord_mobile_2.is_main) + self.assertTrue(self.coord_fix_1.is_main) diff --git a/mozaik_phone/wizard/__init__.py b/mozaik_phone/wizard/__init__.py index 78dccd6e4..0735ef4ed 100644 --- a/mozaik_phone/wizard/__init__.py +++ b/mozaik_phone/wizard/__init__.py @@ -24,3 +24,4 @@ ############################################################################## from . import change_main_phone +from . import change_phone_type diff --git a/mozaik_phone/wizard/change_phone_type.py b/mozaik_phone/wizard/change_phone_type.py new file mode 100644 index 000000000..5d2e75106 --- /dev/null +++ b/mozaik_phone/wizard/change_phone_type.py @@ -0,0 +1,78 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# This file is part of mozaik_phone, an Odoo module. +# +# Copyright (c) 2015 ACSONE SA/NV () +# +# mozaik_phone 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. +# +# mozaik_phone 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 mozaik_phone. +# If not, see . +# +############################################################################## +from openerp import models, fields, api, exceptions +from openerp.addons.mozaik_phone import phone_phone +from openerp.tools.translate import _ + + +class ChangePhoneType(models.TransientModel): + _name = 'change.phone.type' + _description = "Change Phone Type Wizard" + + phone_id = fields.Many2one(string='Phone', + comodel_name="phone.phone") + is_main = fields.Boolean('Set as main', default=True) + type = fields.Selection(string='Type', + selection=phone_phone.PHONE_AVAILABLE_TYPES) + + @api.model + def default_get(self, fields_lists): + res = super(ChangePhoneType, self).default_get(fields_lists) + model = self.env.context.get('active_model', False) + if not model: + return res + + ids = self.env.context.get('active_ids') \ + or (self.env.context.get('active_id') + and [self.env.context.get('active_id')]) \ + or [] + phone = self.env[model].browse(ids[0]) + res['phone_id'] = phone.id + return res + + @api.multi + def change_phone_type(self): + if self.type == self.phone_id.type: + raise exceptions.Warning(_('New phone type should be different' + ' than current one!')) + + self.phone_id.phone_coordinate_ids.ensure_one_main_coordinate( + invalidate=False) + self.phone_id.write({'type': self.type}) + if not self.is_main: + self.phone_id.phone_coordinate_ids.write({'is_main': False}) + + for coordinate in self.phone_id.phone_coordinate_ids: + if self.is_main: + wiz_obj = self.env['change.main.phone'] + context = dict(active_model='phone.coordinate', + target_model='phone.coordinate', + mode='switch', + active_id=coordinate.id) + wiz_id = wiz_obj._model.create( + self.env.cr, self.env.uid, {}, context=context) + wiz_obj._model.button_change_main_coordinate( + self.env.cr, self.env.uid, wiz_id, context=context) + return True diff --git a/mozaik_phone/wizard/change_phone_type.xml b/mozaik_phone/wizard/change_phone_type.xml new file mode 100644 index 000000000..757821d99 --- /dev/null +++ b/mozaik_phone/wizard/change_phone_type.xml @@ -0,0 +1,48 @@ + + + + + + + + change.phone.type.form + change.phone.type + +
+ + + + + +
+ +
+
+
+
+ + + Change Phone Type + form + form + phone.phone + change.phone.type + new + + + + + + Change Phone Type + phone.phone + action + client_action_relate + + +
+
\ No newline at end of file