From 2cc68c75ed7971d34c0b56b1131e1679af16f3de Mon Sep 17 00:00:00 2001 From: oihane Date: Thu, 24 Sep 2015 12:34:24 +0200 Subject: [PATCH 1/4] [IMP] added button to fix #255 --- .../models/report_xml.py | 18 ++++++++++++++++++ .../views/report_xml_view.xml | 8 +++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/base_report_auto_create_qweb/models/report_xml.py b/base_report_auto_create_qweb/models/report_xml.py index 6c719f34d..02c12114d 100644 --- a/base_report_auto_create_qweb/models/report_xml.py +++ b/base_report_auto_create_qweb/models/report_xml.py @@ -9,6 +9,12 @@ from openerp import models, api, exceptions, _ class IrActionsReport(models.Model): _inherit = 'ir.actions.report.xml' + def _format_template_name(self, text): + from unidecode import unidecode + text = unidecode(unicode(text)) + text.lower() + return text.encode('iso-8859-1') + def _prepare_qweb_view_data(self, qweb_name, arch): return { 'name': qweb_name, @@ -45,6 +51,8 @@ class IrActionsReport(models.Model): @api.model def create(self, values): + values['report_name'] = self._format_template_name( + values.get('report_name', '')) if not self.env.context.get('enable_duplication', False): return super(IrActionsReport, self).create(values) if (values.get('report_type') in ['qweb-pdf', 'qweb-html'] and @@ -97,3 +105,13 @@ class IrActionsReport(models.Model): self.with_context( report_views=report_views.ids, suffix=suffix.lower())).copy(default=default) + + @api.multi + def button_create_qweb(self): + self.ensure_one() + module = self.report_name.split('.')[0] + report_name = self.report_name.split('.')[1] + arch = ('\n' + '\n' % report_name) + self._create_qweb(self.name, report_name, module, self.model, arch) + self.associated_view() diff --git a/base_report_auto_create_qweb/views/report_xml_view.xml b/base_report_auto_create_qweb/views/report_xml_view.xml index e558770af..865b85b19 100644 --- a/base_report_auto_create_qweb/views/report_xml_view.xml +++ b/base_report_auto_create_qweb/views/report_xml_view.xml @@ -5,7 +5,7 @@ ir.actions.report.xml.form ir.actions.report.xml - +
@@ -13,7 +13,13 @@ string="Duplicate Report" class="oe_highlight" type="action"/>
+
+ From 495db6261a2fb4e5a6a7124c933a6fcddf51e5df Mon Sep 17 00:00:00 2001 From: oihane Date: Thu, 24 Sep 2015 14:48:09 +0200 Subject: [PATCH 2/4] [IMP] added test and some fixes --- .../models/report_xml.py | 4 +- .../tests/__init__.py | 5 ++ .../test_base_report_auto_create_qweb.py | 56 +++++++++++++++++++ .../views/report_xml_view.xml | 2 +- 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 base_report_auto_create_qweb/tests/__init__.py create mode 100644 base_report_auto_create_qweb/tests/test_base_report_auto_create_qweb.py diff --git a/base_report_auto_create_qweb/models/report_xml.py b/base_report_auto_create_qweb/models/report_xml.py index 02c12114d..768be451f 100644 --- a/base_report_auto_create_qweb/models/report_xml.py +++ b/base_report_auto_create_qweb/models/report_xml.py @@ -63,7 +63,7 @@ class IrActionsReport(models.Model): report_xml = super(IrActionsReport, self).create(values) if values.get('report_type') in ['qweb-pdf', 'qweb-html']: report_view_ids = self.env.context.get('report_views', False) - suffix = self.env.context.get('suffix', 'copy') + suffix = self.env.context.get('suffix') or 'copy' name = values['name'] model = values['model'] report = values['report_name'] @@ -92,7 +92,7 @@ class IrActionsReport(models.Model): return super(IrActionsReport, self).copy(default=default) if default is None: default = {} - suffix = self.env.context.get('suffix', 'copy') + suffix = self.env.context.get('suffix') or 'copy' default['name'] = '%s (%s)' % (self.name, suffix) module = '%s_%s' % ( self.report_name.split('.')[0], suffix.lower()) diff --git a/base_report_auto_create_qweb/tests/__init__.py b/base_report_auto_create_qweb/tests/__init__.py new file mode 100644 index 000000000..509aabdd0 --- /dev/null +++ b/base_report_auto_create_qweb/tests/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# (c) 2015 Oihane Crucelaegui - AvanzOSC +# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html + +from . import test_base_report_auto_create_qweb diff --git a/base_report_auto_create_qweb/tests/test_base_report_auto_create_qweb.py b/base_report_auto_create_qweb/tests/test_base_report_auto_create_qweb.py new file mode 100644 index 000000000..176131870 --- /dev/null +++ b/base_report_auto_create_qweb/tests/test_base_report_auto_create_qweb.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# (c) 2015 Oihane Crucelaegui - AvanzOSC +# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html + +import openerp.tests.common as common + + +class TestBaseReportAutoQwebCreate(common.TransactionCase): + + def setUp(self): + super(TestBaseReportAutoQwebCreate, self).setUp() + self.report_model = self.env['ir.actions.report.xml'] + self.duplicate_model = self.env['ir.actions.report.xml.duplicate'] + self.view_model = self.env['ir.ui.view'] + + def test_creation_html(self): + report_html = self.report_model.create({ + 'name': 'Test 1', + 'model': 'res.partner', + 'report_type': 'qweb-html', + 'report_name': 'test1.report_test', + }) + report_html.button_create_qweb() + view_num = self.view_model.search_count( + [('name', 'ilike', report_html.report_name.split('.')[1]), + ('type', '=', 'qweb')]) + self.assertEqual(view_num, 1, 'Only one view must be created.') + self.assertNotEqual(view_num, 0, 'There are not related views') + + def test_creation_duplicate_pdf(self): + report_pdf = self.report_model.create({ + 'name': 'Test 2', + 'model': 'res.partner', + 'report_type': 'qweb-pdf', + 'report_name': 'test2.report_test', + }) + report_pdf.button_create_qweb() + view_num = self.view_model.search_count( + [('name', 'ilike', report_pdf.report_name.split('.')[1]), + ('type', '=', 'qweb')]) + self.assertEqual(view_num, 1, 'One view must be created.') + self.assertNotEqual(view_num, 0, 'There are not related views.') + wizard = self.duplicate_model.with_context( + active_id=report_pdf.id, model=report_pdf.model).create({ + 'suffix': 'copytest', + }) + wizard.duplicate_report() + report_pdf_copies = self.report_model.search( + [('report_name', 'ilike', 'test2_copytest.report_test_copytest')]) + for report_pdf_copy in report_pdf_copies: + view_num2 = self.view_model.search_count( + [('name', 'ilike', report_pdf_copy.report_name.split('.')[1]), + ('type', '=', 'qweb')]) + self.assertEqual(view_num2, view_num, + 'Same view numbers must have been created.') + self.assertNotEqual(view_num, 0, 'There are not related views.') diff --git a/base_report_auto_create_qweb/views/report_xml_view.xml b/base_report_auto_create_qweb/views/report_xml_view.xml index 865b85b19..bfccb5b9d 100644 --- a/base_report_auto_create_qweb/views/report_xml_view.xml +++ b/base_report_auto_create_qweb/views/report_xml_view.xml @@ -15,7 +15,7 @@
From 6d834572a15c93c56cdef493cfe17ecbede13981 Mon Sep 17 00:00:00 2001 From: oihane Date: Thu, 24 Sep 2015 14:59:27 +0200 Subject: [PATCH 3/4] [IMP] added unidecode to travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index eadb0b61d..4ea16838e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,6 +26,7 @@ install: - export PATH=${HOME}/maintainer-quality-tools/travis:${PATH} - travis_install_nightly - pip install python-ldap + - pip install unidecode - printf '[options]\n\nrunning_env = dev' > ${HOME}/.openerp_serverrc - ln -s server_environment_files_sample ./server_environment_files script: From 2e31945d478922985df9b9c838a7f32824a70683 Mon Sep 17 00:00:00 2001 From: oihane Date: Mon, 28 Sep 2015 11:25:55 +0200 Subject: [PATCH 4/4] [IMP] Fixed reported problems * Warning raised when template name does not contain '.' * Template name was not same as template ID * Dependecies added to __openerp__.py file --- base_report_auto_create_qweb/__openerp__.py | 7 ++++++- .../models/report_xml.py | 14 ++++++++++---- .../tests/test_base_report_auto_create_qweb.py | 16 +++++++++++++--- .../views/report_xml_view.xml | 1 - 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/base_report_auto_create_qweb/__openerp__.py b/base_report_auto_create_qweb/__openerp__.py index 92f73e2ae..0e19e549a 100644 --- a/base_report_auto_create_qweb/__openerp__.py +++ b/base_report_auto_create_qweb/__openerp__.py @@ -22,12 +22,17 @@ "depends": [ "report", ], + "external_dependencies": { + "python": [ + "unidecode", + ], + }, "author": "OdooMRP team, " "AvanzOSC, " "Serv. Tecnol. Avanzados - Pedro M. Baeza, " "Odoo Community Association (OCA), ", "website": "http://www.odoomrp.com", - 'license': 'AGPL-3', + "license": "AGPL-3", "contributors": [ "Oihane Crucelaegui ", "Pedro M. Baeza ", diff --git a/base_report_auto_create_qweb/models/report_xml.py b/base_report_auto_create_qweb/models/report_xml.py index 768be451f..7ef02178a 100644 --- a/base_report_auto_create_qweb/models/report_xml.py +++ b/base_report_auto_create_qweb/models/report_xml.py @@ -4,13 +4,19 @@ ############################################################################## from openerp import models, api, exceptions, _ +import logging + +_logger = logging.getLogger(__name__) class IrActionsReport(models.Model): _inherit = 'ir.actions.report.xml' def _format_template_name(self, text): - from unidecode import unidecode + try: + from unidecode import unidecode + except ImportError: + _logger.debug('Can not `import unidecode`.') text = unidecode(unicode(text)) text.lower() return text.encode('iso-8859-1') @@ -53,13 +59,13 @@ class IrActionsReport(models.Model): def create(self, values): values['report_name'] = self._format_template_name( values.get('report_name', '')) - if not self.env.context.get('enable_duplication', False): - return super(IrActionsReport, self).create(values) if (values.get('report_type') in ['qweb-pdf', 'qweb-html'] and values.get('report_name') and values['report_name'].find('.') == -1): raise exceptions.Warning( _("Template Name must contain at least a dot in it's name")) + if not self.env.context.get('enable_duplication', False): + return super(IrActionsReport, self).create(values) report_xml = super(IrActionsReport, self).create(values) if values.get('report_type') in ['qweb-pdf', 'qweb-html']: report_view_ids = self.env.context.get('report_views', False) @@ -112,6 +118,6 @@ class IrActionsReport(models.Model): module = self.report_name.split('.')[0] report_name = self.report_name.split('.')[1] arch = ('\n' - '\n' % report_name) + '\n' % self.report_name) self._create_qweb(self.name, report_name, module, self.model, arch) self.associated_view() diff --git a/base_report_auto_create_qweb/tests/test_base_report_auto_create_qweb.py b/base_report_auto_create_qweb/tests/test_base_report_auto_create_qweb.py index 176131870..84e247f7f 100644 --- a/base_report_auto_create_qweb/tests/test_base_report_auto_create_qweb.py +++ b/base_report_auto_create_qweb/tests/test_base_report_auto_create_qweb.py @@ -3,6 +3,7 @@ # License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html import openerp.tests.common as common +from openerp import exceptions class TestBaseReportAutoQwebCreate(common.TransactionCase): @@ -24,8 +25,8 @@ class TestBaseReportAutoQwebCreate(common.TransactionCase): view_num = self.view_model.search_count( [('name', 'ilike', report_html.report_name.split('.')[1]), ('type', '=', 'qweb')]) - self.assertEqual(view_num, 1, 'Only one view must be created.') self.assertNotEqual(view_num, 0, 'There are not related views') + self.assertEqual(view_num, 1, 'Only one view must be created.') def test_creation_duplicate_pdf(self): report_pdf = self.report_model.create({ @@ -38,8 +39,8 @@ class TestBaseReportAutoQwebCreate(common.TransactionCase): view_num = self.view_model.search_count( [('name', 'ilike', report_pdf.report_name.split('.')[1]), ('type', '=', 'qweb')]) - self.assertEqual(view_num, 1, 'One view must be created.') self.assertNotEqual(view_num, 0, 'There are not related views.') + self.assertEqual(view_num, 1, 'One view must be created.') wizard = self.duplicate_model.with_context( active_id=report_pdf.id, model=report_pdf.model).create({ 'suffix': 'copytest', @@ -51,6 +52,15 @@ class TestBaseReportAutoQwebCreate(common.TransactionCase): view_num2 = self.view_model.search_count( [('name', 'ilike', report_pdf_copy.report_name.split('.')[1]), ('type', '=', 'qweb')]) + self.assertNotEqual(view_num2, 0, 'There are not related views.') self.assertEqual(view_num2, view_num, 'Same view numbers must have been created.') - self.assertNotEqual(view_num, 0, 'There are not related views.') + + def test_wrong_template_name(self): + with self.assertRaises(exceptions.Warning): + self.report_model.create({ + 'name': 'Test', + 'model': 'res.partner', + 'report_type': 'qweb-pdf', + 'report_name': 'report_test', + }) diff --git a/base_report_auto_create_qweb/views/report_xml_view.xml b/base_report_auto_create_qweb/views/report_xml_view.xml index bfccb5b9d..70ed53585 100644 --- a/base_report_auto_create_qweb/views/report_xml_view.xml +++ b/base_report_auto_create_qweb/views/report_xml_view.xml @@ -20,6 +20,5 @@
-