Skip to content

Commit

Permalink
Merge d45b986 into bafe691
Browse files Browse the repository at this point in the history
  • Loading branch information
simahawk committed May 21, 2018
2 parents bafe691 + d45b986 commit 847bbaf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
27 changes: 23 additions & 4 deletions cms_form/models/cms_search_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class CMSFormSearch(models.AbstractModel):
_form_results_orderby = ''
# declare fields that must be searched w/ multiple values
_form_search_fields_multi = ()
# declare custom domain computation rules
_form_search_domain_rules = {
# field name: (leaf field name, operator, format value),
# 'product_id': ('product_id.name', 'ilike', '{}'),
}

def form_check_permission(self):
"""Just searching, nothing to check here."""
Expand All @@ -36,6 +41,13 @@ def form_update_fields_attributes(self, _fields):
for fname, field in _fields.iteritems():
field['required'] = False

def form_get_widget_model(self, fname, field):
"""Search via related field needs a simple char widget."""
res = super(CMSFormSearch, self).form_get_widget_model(fname, field)
if fname in self._form_search_domain_rules:
res = 'cms.form.widget.char'
return res

__form_search_results = {}

@property
Expand Down Expand Up @@ -72,6 +84,9 @@ def form_search(self, render_values):
url = render_values.get('extra_args', {}).get('pager_url', '')
if self._form_model:
url = getattr(self.form_model, 'cms_search_url', url)
if not url:
# default to current path w/out paging
url = self.request.path.decode('utf-8').split('/page')[0]
pager = self._form_results_pager(count=count, page=page, url=url)
order = self._form_results_orderby or None
results = self.form_model.search(
Expand Down Expand Up @@ -114,9 +129,6 @@ def form_search_domain(self, search_values):
leaf = (fname, 'in', value)
domain.append(leaf)
continue
if field['type'] in ('many2one', ) and value < 1:
# we need an existing ID here ( > 0)
continue
# TODO: find the way to properly handle this.
# It would be nice to guess leafs in a clever way.
operator = '='
Expand All @@ -129,7 +141,7 @@ def form_search_domain(self, search_values):
if not value:
continue
operator = 'in'
elif field['type'] in ('many2one', ) and not value:
elif field['type'] in ('many2one', ) and not value or value < 1:
# we need an existing ID here ( > 0)
continue
elif field['type'] in ('boolean', ):
Expand All @@ -138,6 +150,13 @@ def form_search_domain(self, search_values):
if not value:
# searching for an empty string breaks search
continue
if fname in self._form_search_domain_rules:
fname, operator, fmt_value = \
self._form_search_domain_rules[fname]
if hasattr(fmt_value, '__call__'):
value = fmt_value(field, value, search_values)
else:
value = fmt_value.format(value) if fmt_value else value
leaf = (fname, operator, value)
domain.append(leaf)
return domain
14 changes: 14 additions & 0 deletions cms_form/tests/test_form_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,17 @@ def test_search_form_bypass_security_check(self):
form = self.get_search_form(
{}, sudo_uid=self.env.ref('base.public_user').id)
self.assertTrue(form.form_check_permission())

def test_search_custom_rules(self):
data = {'country_id': 'Italy', }
form = self.get_search_form(data)
form.form_process()
# we find them all since domain is mocked to include all test partners
self.assert_results(form, 5, self.expected_partners)
# apply custom rules
data = {'country_id': 'Italy', }
form = self.get_search_form(data, search_domain_rules={
'country_id': ('country_id.name', 'ilike', '')
})
form.form_process()
self.assert_results(form, 2, self.expected_partners[:2])

0 comments on commit 847bbaf

Please sign in to comment.