[IMP] base_name_search_improved: run pre-commit
parent
d962ad0c5f
commit
adaf70ed5e
|
@ -25,7 +25,11 @@ def _get_rec_names(self):
|
||||||
|
|
||||||
@tools.ormcache(skiparg=0)
|
@tools.ormcache(skiparg=0)
|
||||||
def _get_use_smart_name_search(self):
|
def _get_use_smart_name_search(self):
|
||||||
return self.env['ir.model'].search([('model', '=', str(self._name))]).use_smart_name_search
|
return (
|
||||||
|
self.env["ir.model"]
|
||||||
|
.search([("model", "=", str(self._name))])
|
||||||
|
.use_smart_name_search
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@tools.ormcache(skiparg=0)
|
@tools.ormcache(skiparg=0)
|
||||||
|
@ -55,17 +59,28 @@ def _extend_name_results(self, domain, results, limit, name_get_uid):
|
||||||
result_count = len(results)
|
result_count = len(results)
|
||||||
if result_count < limit:
|
if result_count < limit:
|
||||||
domain += [("id", "not in", [x[0] for x in results])]
|
domain += [("id", "not in", [x[0] for x in results])]
|
||||||
rec_ids = self._search(domain, limit=limit - result_count, access_rights_uid=name_get_uid)
|
rec_ids = self._search(
|
||||||
results.extend(models.lazy_name_get(self.browse(rec_ids).with_user(name_get_uid)))
|
domain, limit=limit - result_count, access_rights_uid=name_get_uid
|
||||||
|
)
|
||||||
|
results.extend(
|
||||||
|
models.lazy_name_get(self.browse(rec_ids).with_user(name_get_uid))
|
||||||
|
)
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
def patch_name_search():
|
def patch_name_search():
|
||||||
@api.model
|
@api.model
|
||||||
def _name_search(self, name='', args=None, operator='ilike', limit=100, name_get_uid=None):
|
def _name_search(
|
||||||
|
self, name="", args=None, operator="ilike", limit=100, name_get_uid=None
|
||||||
|
):
|
||||||
# Perform standard name search
|
# Perform standard name search
|
||||||
res = _name_search.origin(
|
res = _name_search.origin(
|
||||||
self, name=name, args=args, operator=operator, limit=limit, name_get_uid=name_get_uid
|
self,
|
||||||
|
name=name,
|
||||||
|
args=args,
|
||||||
|
operator=operator,
|
||||||
|
limit=limit,
|
||||||
|
name_get_uid=name_get_uid,
|
||||||
)
|
)
|
||||||
if name and _get_use_smart_name_search(self.sudo()) and operator in ALLOWED_OPS:
|
if name and _get_use_smart_name_search(self.sudo()) and operator in ALLOWED_OPS:
|
||||||
limit = limit or 0
|
limit = limit or 0
|
||||||
|
@ -79,11 +94,15 @@ def patch_name_search():
|
||||||
# Try regular search on each additional search field
|
# Try regular search on each additional search field
|
||||||
for rec_name in all_names[1:]:
|
for rec_name in all_names[1:]:
|
||||||
domain = [(rec_name, operator, name)]
|
domain = [(rec_name, operator, name)]
|
||||||
res = _extend_name_results(self, base_domain + domain, res, limit, name_get_uid)
|
res = _extend_name_results(
|
||||||
|
self, base_domain + domain, res, limit, name_get_uid
|
||||||
|
)
|
||||||
# Try ordered word search on each of the search fields
|
# Try ordered word search on each of the search fields
|
||||||
for rec_name in all_names:
|
for rec_name in all_names:
|
||||||
domain = [(rec_name, operator, name.replace(" ", "%"))]
|
domain = [(rec_name, operator, name.replace(" ", "%"))]
|
||||||
res = _extend_name_results(self, base_domain + domain, res, limit, name_get_uid)
|
res = _extend_name_results(
|
||||||
|
self, base_domain + domain, res, limit, name_get_uid
|
||||||
|
)
|
||||||
# Try unordered word search on each of the search fields
|
# Try unordered word search on each of the search fields
|
||||||
# we only perform this search if we have at least one
|
# we only perform this search if we have at least one
|
||||||
# separator character
|
# separator character
|
||||||
|
@ -97,7 +116,9 @@ def patch_name_search():
|
||||||
word_domain and ["|"] + word_domain or word_domain
|
word_domain and ["|"] + word_domain or word_domain
|
||||||
) + [(rec_name, operator, word)]
|
) + [(rec_name, operator, word)]
|
||||||
domain = (domain and ["&"] + domain or domain) + word_domain
|
domain = (domain and ["&"] + domain or domain) + word_domain
|
||||||
res = _extend_name_results(self, base_domain + domain, res, limit, name_get_uid)
|
res = _extend_name_results(
|
||||||
|
self, base_domain + domain, res, limit, name_get_uid
|
||||||
|
)
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
@ -176,29 +197,37 @@ class IrModel(models.Model):
|
||||||
|
|
||||||
add_smart_search = fields.Boolean(help="Add Smart Search on search views",)
|
add_smart_search = fields.Boolean(help="Add Smart Search on search views",)
|
||||||
use_smart_name_search = fields.Boolean(
|
use_smart_name_search = fields.Boolean(
|
||||||
string='Smart Name Search Enabled?', help="Use Smart Search for 'name_search', this will affect when searching"
|
string="Smart Name Search Enabled?",
|
||||||
"from other records (for eg. from m2o fields")
|
help="Use Smart Search for 'name_search', this will affect when "
|
||||||
|
"searching from other records (for eg. from m2o fields",
|
||||||
|
)
|
||||||
name_search_ids = fields.Many2many("ir.model.fields", string="Smart Search Fields")
|
name_search_ids = fields.Many2many("ir.model.fields", string="Smart Search Fields")
|
||||||
name_search_domain = fields.Char(string='Smart Search Domain')
|
name_search_domain = fields.Char(string="Smart Search Domain")
|
||||||
smart_search_warning = fields.Html(compute='_smart_search_warning')
|
smart_search_warning = fields.Html(compute="_compute_smart_search_warning")
|
||||||
|
|
||||||
@api.depends('name_search_ids')
|
@api.depends("name_search_ids")
|
||||||
def _smart_search_warning(self):
|
def _compute_smart_search_warning(self):
|
||||||
msgs = []
|
msgs = []
|
||||||
for rec in self:
|
for rec in self:
|
||||||
if len(rec.name_search_ids) > 4:
|
if len(rec.name_search_ids) > 4:
|
||||||
msgs.append(
|
msgs.append(
|
||||||
'Ha seleccionado más de 4 campos para smart search, recomendamos que intente utilizar menos '
|
"Ha seleccionado más de 4 campos para smart search, "
|
||||||
'campos')
|
"recomendamos que intente utilizar menos "
|
||||||
|
"campos"
|
||||||
|
)
|
||||||
if any(x.translate for x in rec.name_search_ids):
|
if any(x.translate for x in rec.name_search_ids):
|
||||||
msgs.append(
|
msgs.append(
|
||||||
'Ha seleccionado campos traducibles en la búsqueda inteligente, de ser posible intente '
|
"Ha seleccionado campos traducibles en la búsqueda "
|
||||||
'evitar los mismos')
|
"inteligente, de ser posible intente "
|
||||||
|
"evitar los mismos"
|
||||||
|
)
|
||||||
# rec.smart_search_warning = msg
|
# rec.smart_search_warning = msg
|
||||||
if msgs:
|
if msgs:
|
||||||
rec.smart_search_warning = (
|
rec.smart_search_warning = (
|
||||||
'<p>Si tiene problemas de performance en las búsquedas le recomendamos revisar estas '
|
"<p>Si tiene problemas de performance en las búsquedas le "
|
||||||
'sugerencias: <ul>%s</ul></p>') % "".join(["<li>%s</li>" % x for x in msgs])
|
"recomendamos revisar estas "
|
||||||
|
"sugerencias: <ul>%s</ul></p>"
|
||||||
|
) % "".join(["<li>%s</li>" % x for x in msgs])
|
||||||
else:
|
else:
|
||||||
rec.smart_search_warning = False
|
rec.smart_search_warning = False
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
<!-- © 2016 Daniel Reis
|
<!-- © 2016 Daniel Reis
|
||||||
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
|
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
|
||||||
<odoo>
|
<odoo>
|
||||||
|
|
||||||
<record id="view_model_form_new" model="ir.ui.view">
|
<record id="view_model_form_new" model="ir.ui.view">
|
||||||
<field name="name">view.model.form</field>
|
<field name="name">view.model.form</field>
|
||||||
<field name="model">ir.model</field>
|
<field name="model">ir.model</field>
|
||||||
|
@ -18,37 +17,64 @@
|
||||||
<p class="alert alert-info" role="alert" style="margin-bottom:0px;">
|
<p class="alert alert-info" role="alert" style="margin-bottom:0px;">
|
||||||
Smart Search se puede activar de dos formas que pueden ser complementarias:
|
Smart Search se puede activar de dos formas que pueden ser complementarias:
|
||||||
<ul>
|
<ul>
|
||||||
<li><b>Vista de búsqueda</b>: a la hora de buscar en vistas listas, kanban y demás, la primer opción será buscar mediante "Smart Search"
|
<li><b
|
||||||
|
>Vista de búsqueda</b>: a la hora de buscar en vistas listas, kanban y demás, la primer opción será buscar mediante "Smart Search"
|
||||||
</li>
|
</li>
|
||||||
<li><b>Name Search</b>: es para cuando estemos queriendo buscar un registro desde otro registro a través de los widgets m2o/m2m, por ejemplo al agregar un producto en una línea de venta o al elegir un contacto en una factura.
|
<li><b
|
||||||
|
>Name Search</b>: es para cuando estemos queriendo buscar un registro desde otro registro a través de los widgets m2o/m2m, por ejemplo al agregar un producto en una línea de venta o al elegir un contacto en una factura.
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
En donde se haya activado "Smart Search" las búsquedas tendrán un comportmiento más "relajado". Nativamente Odoo busca por registors que incluyan las palabras ingresadas tal cual las ha ingresado, al activar esta opción se buscará por registros que contegan dichas palabras y sin importar el orden en el que se ingresen. Por ej. si buscamos un Contacto con "Pedro Picapiedra", naticamente Odoo solo nos retornaría aquellos registros que contengan exactamente ese texto, con smart search activo se devolverá cualquier registro que contenga las palabras "Pedro" y "Picapiedra".<br/><br/>
|
En donde se haya activado "Smart Search" las búsquedas tendrán un comportmiento más "relajado". Nativamente Odoo busca por registors que incluyan las palabras ingresadas tal cual las ha ingresado, al activar esta opción se buscará por registros que contegan dichas palabras y sin importar el orden en el que se ingresen. Por ej. si buscamos un Contacto con "Pedro Picapiedra", naticamente Odoo solo nos retornaría aquellos registros que contengan exactamente ese texto, con smart search activo se devolverá cualquier registro que contenga las palabras "Pedro" y "Picapiedra".<br
|
||||||
|
/><br />
|
||||||
|
|
||||||
Al activar Smart Search podrá además agregar configurar estos dos comportamientos opcionales:
|
Al activar Smart Search podrá además agregar configurar estos dos comportamientos opcionales:
|
||||||
<ul>
|
<ul>
|
||||||
<li>Definir un <b>dominio personalizado</b> (por ejemplo solo mostrar usuarios internos y no los portal)</li>
|
<li>Definir un <b
|
||||||
<li><b>Búsquedas por otros campos</b>. Podremos definir un conjunto de campos por el cual querramos buscar</li>
|
>dominio personalizado</b> (por ejemplo solo mostrar usuarios internos y no los portal)</li>
|
||||||
|
<li><b
|
||||||
|
>Búsquedas por otros campos</b>. Podremos definir un conjunto de campos por el cual querramos buscar</li>
|
||||||
</ul>
|
</ul>
|
||||||
</p>
|
</p>
|
||||||
<p class="alert alert-warning" role="alert" style="margin-bottom:0px;">
|
<p
|
||||||
<b>IMPORTANTE:</b> tenga en cuenta que activar smart search puede afectar la performance de las búsquedas y del sistema en general.
|
class="alert alert-warning"
|
||||||
|
role="alert"
|
||||||
|
style="margin-bottom:0px;"
|
||||||
|
>
|
||||||
|
<b
|
||||||
|
>IMPORTANTE:</b> tenga en cuenta que activar smart search puede afectar la performance de las búsquedas y del sistema en general.
|
||||||
<field name="smart_search_warning" />
|
<field name="smart_search_warning" />
|
||||||
</p>
|
</p>
|
||||||
<group>
|
<group>
|
||||||
<group>
|
<group>
|
||||||
<field name="id" invisible="1" />
|
<field name="id" invisible="1" />
|
||||||
<field name="model" readonly="1" />
|
<field name="model" readonly="1" />
|
||||||
<field name="add_smart_search" string="Smart Search (search view)" widget="boolean_toggle"/>
|
<field
|
||||||
<field name="use_smart_name_search" string="Smart Name Search" widget="boolean_toggle"/>
|
name="add_smart_search"
|
||||||
|
string="Smart Search (search view)"
|
||||||
|
widget="boolean_toggle"
|
||||||
|
/>
|
||||||
|
<field
|
||||||
|
name="use_smart_name_search"
|
||||||
|
string="Smart Name Search"
|
||||||
|
widget="boolean_toggle"
|
||||||
|
/>
|
||||||
<!-- TODO use new odoo domain widget -->
|
<!-- TODO use new odoo domain widget -->
|
||||||
<field name="name_search_domain" attrs="{'invisible': [('use_smart_name_search', '=', False), ('add_smart_search', '=', False)]}"/>
|
<field
|
||||||
|
name="name_search_domain"
|
||||||
|
attrs="{'invisible': [('use_smart_name_search', '=', False), ('add_smart_search', '=', False)]}"
|
||||||
|
/>
|
||||||
</group>
|
</group>
|
||||||
</group>
|
</group>
|
||||||
<notebook colspan="4" attrs="{'invisible': [('use_smart_name_search', '=', False), ('add_smart_search', '=', False)]}">
|
<notebook
|
||||||
|
colspan="4"
|
||||||
|
attrs="{'invisible': [('use_smart_name_search', '=', False), ('add_smart_search', '=', False)]}"
|
||||||
|
>
|
||||||
<page string="Smart Search Fields">
|
<page string="Smart Search Fields">
|
||||||
<!-- we use default m2m widget and not tags widget so that is clearer the technical name of the field added -->
|
<!-- we use default m2m widget and not tags widget so that is clearer the technical name of the field added -->
|
||||||
<field name="name_search_ids" domain="[('model_id', '=', id), ('selectable', '=', True)]">
|
<field
|
||||||
|
name="name_search_ids"
|
||||||
|
domain="[('model_id', '=', id), ('selectable', '=', True)]"
|
||||||
|
>
|
||||||
<tree>
|
<tree>
|
||||||
<field name="name" />
|
<field name="name" />
|
||||||
<field name="field_description" />
|
<field name="field_description" />
|
||||||
|
@ -122,7 +148,9 @@
|
||||||
(0, 0, {'view_mode': 'form', 'view_id': ref('view_model_form_new')}
|
(0, 0, {'view_mode': 'form', 'view_id': ref('view_model_form_new')}
|
||||||
)]"
|
)]"
|
||||||
/>
|
/>
|
||||||
<field name="context">{'search_default_smart_search': 1, 'search_default_smart_name_search': 1}</field>
|
<field
|
||||||
|
name="context"
|
||||||
|
>{'search_default_smart_search': 1, 'search_default_smart_name_search': 1}</field>
|
||||||
<field name="domain">[('transient', '=', False)]</field>
|
<field name="domain">[('transient', '=', False)]</field>
|
||||||
</record>
|
</record>
|
||||||
<menuitem
|
<menuitem
|
||||||
|
|
Loading…
Reference in New Issue