diff --git a/product_category_search/__init__.py b/product_category_search/__init__.py new file mode 100755 index 00000000..6ce6eab1 --- /dev/null +++ b/product_category_search/__init__.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +############################################################################## +# For copyright and license notices, see __openerp__.py file in module root +# directory +############################################################################## +from . import product diff --git a/product_category_search/__openerp__.py b/product_category_search/__openerp__.py new file mode 100644 index 00000000..cc4769a0 --- /dev/null +++ b/product_category_search/__openerp__.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Copyright (C) 2015 ADHOC SA (http://www.adhoc.com.ar) +# All Rights Reserved. +# +# 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 . +# +############################################################################## +{ + "name": "Product Public Category Search", + 'version': '8.0.0.0.0', + 'category': 'Sales & Purchases', + 'sequence': 14, + 'author': 'ADHOC SA', + 'website': 'www.adhoc.com.ar', + 'license': 'AGPL-3', + 'summary': '', + "description": """ +Product Public Category Search +============================== +This module adds to the product search function the possibility to search with "name/" + and all children of the category name are displayed. + + """, + "depends": [ + "product_website_categ_search", + ], + 'external_dependencies': { + }, + "data": [ + ], + 'demo': [ + ], + 'test': [ + ], + 'installable': True, + 'auto_install': False, + 'application': False, +} diff --git a/product_category_search/product.py b/product_category_search/product.py new file mode 100644 index 00000000..895aeea5 --- /dev/null +++ b/product_category_search/product.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- +############################################################################## +# For copyright and license notices, see __openerp__.py file in module root +# directory +############################################################################## +from openerp import models +from openerp.osv import expression + + +class product_public_category(models.Model): + _inherit = "product.public.category" + + def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100): + if not args: + args = [] + if not context: + context = {} + if name: + # Be sure name_search is symetric to name_get + categories = name.split('/') + parents = list(categories) + child = parents.pop() + domain = [('name', operator, child)] + if parents: + names_ids = self.name_search( + cr, uid, '/'.join(parents), args=args, operator='ilike', context=context, limit=limit) + category_ids = [name_id[0] for name_id in names_ids] + if operator in expression.NEGATIVE_TERM_OPERATORS: + category_ids = self.search( + cr, uid, [('id', 'not in', category_ids)]) + domain = expression.OR( + [[('parent_id', 'in', category_ids)], domain]) + else: + domain = expression.AND( + [[('parent_id', 'in', category_ids)], domain]) + for i in range(1, len(categories)): + domain = [ + [('name', operator, '/'.join(categories[-1 - i:]))], domain] + if operator in expression.NEGATIVE_TERM_OPERATORS: + domain = expression.AND(domain) + else: + domain = expression.OR(domain) + ids = self.search( + cr, uid, expression.AND([domain, args]), limit=limit, context=context) + else: + ids = self.search(cr, uid, args, limit=limit, context=context) + return self.name_get(cr, uid, ids, context) + + def name_get(self, cr, uid, ids, context=None): + res = [] + for cat in self.browse(cr, uid, ids, context=context): + names = [cat.name] + pcat = cat.parent_id + while pcat: + names.append(pcat.name) + pcat = pcat.parent_id + res.append((cat.id, '/'.join(reversed(names)))) + return res