From ec31f52bb3330eab7e00568b5b1670f33e22fc4e Mon Sep 17 00:00:00 2001 From: Ronald Portier Date: Fri, 11 Nov 2016 12:10:44 +0100 Subject: [PATCH] [FIX] Improvements after review. - Only load indexer when module is installed; - Protect non standard imports; - Standardized README.rst. --- .gitattributes | 1 + .travis.yml | 1 + document_rtf_index/README.rst | 29 +++++++-- document_rtf_index/__init__.py | 2 +- document_rtf_index/models/__init__.py | 4 ++ document_rtf_index/models/ir_attachment.py | 74 ++++++++++++++++++++++ document_rtf_index/std_index.py | 61 ------------------ document_rtf_index/tests/test_rtf_index.py | 6 +- 8 files changed, 110 insertions(+), 68 deletions(-) create mode 100644 .gitattributes create mode 100644 document_rtf_index/models/__init__.py create mode 100644 document_rtf_index/models/ir_attachment.py delete mode 100644 document_rtf_index/std_index.py diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..016151e2 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.rtf -diff diff --git a/.travis.yml b/.travis.yml index 74003680..853f814d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,6 +35,7 @@ install: - git clone --depth=1 https://github.com/OCA/maintainer-quality-tools.git ${HOME}/maintainer-quality-tools - export PATH=${HOME}/maintainer-quality-tools/travis:${PATH} - pip install --upgrade paramiko + - pip install --upgrade pyth - travis_install_nightly script: diff --git a/document_rtf_index/README.rst b/document_rtf_index/README.rst index d6c8d1f7..ab4929fe 100644 --- a/document_rtf_index/README.rst +++ b/document_rtf_index/README.rst @@ -1,3 +1,7 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 + Index rtf documents =================== @@ -11,6 +15,23 @@ Usage Just installing the module will register the rtf indexer. No further user action or configuration required. + +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/knowledge/8.0 + +.. repo_id is available in https://github.com/OCA/maintainer-tools/blob/master/tools/repos_with_ids.txt +.. branch is "8.0" for example + +Bug Tracker +=========== + +Bugs are tracked on +`GitHub Issues `_. +In case of trouble, please check there if your issue has already been +reported. If you spotted it first, help us smashing it by providing a +detailed and welcomed feedback. + Credits ======= @@ -18,15 +39,15 @@ Contributors ------------ * Ronald Portier -* Icon courtesy of http://www.picol.org (refresh.svg) and +* Icon courtesy of https://www.picol.org (refresh.svg) and https://github.com/odoo/odoo/blob/8.0/addons/knowledge/static/description/icon.png Maintainer ---------- -.. image:: http://odoo-community.org/logo.png +.. image:: https://odoo-community.org/logo.png :alt: Odoo Community Association - :target: http://odoo-community.org + :target: https://odoo-community.org This module is maintained by the OCA. @@ -34,4 +55,4 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use. -To contribute to this module, please visit http://odoo-community.org. +To contribute to this module, please visit https://odoo-community.org. diff --git a/document_rtf_index/__init__.py b/document_rtf_index/__init__.py index ab5a9805..7eda98a2 100644 --- a/document_rtf_index/__init__.py +++ b/document_rtf_index/__init__.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- # © 2016 Therp BV # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from . import std_index +from . import models diff --git a/document_rtf_index/models/__init__.py b/document_rtf_index/models/__init__.py new file mode 100644 index 00000000..a15f1b21 --- /dev/null +++ b/document_rtf_index/models/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# © 2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from . import ir_attachment diff --git a/document_rtf_index/models/ir_attachment.py b/document_rtf_index/models/ir_attachment.py new file mode 100644 index 00000000..d17b2cf0 --- /dev/null +++ b/document_rtf_index/models/ir_attachment.py @@ -0,0 +1,74 @@ +# -*- coding: utf-8 -*- +# © 2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +import logging + +from openerp import api, models + + +_logger = logging.getLogger(__name__) + + +class IrAttachment(models.Model): + _inherit = 'ir.attachment' + + @api.noguess + def _register_hook(self, cr): + """Only register our indexer if module is installed.""" + import StringIO + try: + from pyth import document + from pyth.plugins.rtf15.reader import Rtf15Reader + from pyth.plugins.plaintext.writer import PlaintextWriter + except ImportError: + _logger.warn("pyth not found, RTF indexing disabled.") + return + from openerp.addons.document.content_index import indexer, cntIndex + from openerp.addons.document.std_index import _to_unicode + + def improved_paragraph(self, paragraph, prefix=""): + """Override method to insert image ignoring code.""" + content = [] + for text in paragraph.content: + # Begin patch =========\ + if text.__class__ is document.Image: + continue + # End patch ===========/ + content.append(u"".join(text.content)) + content = u"".join(content).encode("utf-8") + for line in content.split("\n"): + self.target.write(" " * self.indent) + self.target.write(prefix) + self.target.write(line) + self.target.write("\n") + if prefix: + prefix = " " + + PlaintextWriter.paragraph = improved_paragraph + + class RtfDoc(indexer): + """Index Rich Text Format (RTF) files.""" + + def _getMimeTypes(self): + return [ + 'application/rtf', + 'application/x-rtf', + 'text/rtf', + 'text/richtext', + ] + + def _getExtensions(self): + return [ + '.rtf', + ] + + def _doIndexContent(self, content): + """Just get text contents of rtf file.""" + s = StringIO.StringIO(content) + r = Rtf15Reader.read(s) # r will be pyth.document.Document + s.close() + w = PlaintextWriter.write(r) # w will be cStringIO.StringO + result = _to_unicode(w.getvalue()) + return result + + cntIndex.register(RtfDoc()) diff --git a/document_rtf_index/std_index.py b/document_rtf_index/std_index.py deleted file mode 100644 index 84fc44bd..00000000 --- a/document_rtf_index/std_index.py +++ /dev/null @@ -1,61 +0,0 @@ -# -*- coding: utf-8 -*- -# © 2016 Therp BV -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -import StringIO - -from pyth import document -from pyth.plugins.rtf15.reader import Rtf15Reader -from pyth.plugins.plaintext.writer import PlaintextWriter - -from openerp.addons.document.content_index import indexer, cntIndex -from openerp.addons.document.std_index import _to_unicode - - -def improved_paragraph(self, paragraph, prefix=""): - """Override method to insert image ignoring code.""" - content = [] - for text in paragraph.content: - # Begin patch =========\ - if text.__class__ is document.Image: - continue - # End patch ===========/ - content.append(u"".join(text.content)) - content = u"".join(content).encode("utf-8") - - for line in content.split("\n"): - self.target.write(" " * self.indent) - self.target.write(prefix) - self.target.write(line) - self.target.write("\n") - if prefix: prefix = " " - - -PlaintextWriter.paragraph = improved_paragraph - - -class RtfDoc(indexer): - """Index Rich Text Format (RTF) files.""" - - def _getMimeTypes(self): - return [ - 'application/rtf', - 'application/x-rtf', - 'text/rtf', - 'text/richtext', - ] - - def _getExtensions(self): - return [ - '.rtf', - ] - - def _doIndexContent(self, content): - """Just get text contents of rtf file.""" - s = StringIO.StringIO(content) - r = Rtf15Reader.read(s) # r will be pyth.document.Document - s.close() - w = PlaintextWriter.write(r) # w will be cStringIO.StringO - result = _to_unicode(w.getvalue()) - return result - -cntIndex.register(RtfDoc()) diff --git a/document_rtf_index/tests/test_rtf_index.py b/document_rtf_index/tests/test_rtf_index.py index 231a364e..43253ead 100644 --- a/document_rtf_index/tests/test_rtf_index.py +++ b/document_rtf_index/tests/test_rtf_index.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- # © 2016 Therp BV # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -import base64 from openerp.tests.common import TransactionCase from openerp.modules.module import get_module_resource @@ -10,8 +9,11 @@ class TestIndexRtf(TransactionCase): def test_index_rtf(self): """Test if the indexer indexes just the text in rtf documents.""" + attachment_model = self.env['ir.attachment'] + # Force loading of indexer (normally _register_hooks runs after tests) + attachment_model._register_hook(self.env.cr) # we do this to avoid error messages about word files in demo data - self.env['ir.attachment'].search([]).unlink() + attachment_model.search([]).unlink() # Now take rather large rtf test file, with only few actual words: rtf_path = get_module_resource( 'document_rtf_index',