Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.0][MIG] base_search_mail_content #180

Merged
merged 5 commits into from
Sep 20, 2017

Conversation

MiquelRForgeFlow
Copy link
Contributor

Standard migration.

@MiquelRForgeFlow MiquelRForgeFlow force-pushed the 10.0-mig-base_search_mail_content branch 3 times, most recently from 20cb978 to c8eb14c Compare July 10, 2017 12:05
@yajo yajo added this to the 10.0 milestone Jul 10, 2017
@pedrobaeza pedrobaeza force-pushed the 10.0-mig-base_search_mail_content branch from c8eb14c to 256d275 Compare August 5, 2017 15:13
def _compute_message_content(self):
""" We don't really need to show any content. This field is to be
used only by searches"""
return ''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compute fields don't return, they just set self.field=something. Just pass please.

return res


models.BaseModel.fields_view_get = _custom_fields_view_get
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This monkey-patching should not get accepted now on v10. Override it as usual with _inherit = "base" instead (it is a new v10 feature).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

class Partner(models.Model):

_name = 'res.partner'
_inherit = ['res.partner', 'mail.thread']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this file? res.partner already inherits from mail.thread by default... Isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It already inherits or you wouldn't have chatter on partners, so this is not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Let's see if it works now without the file 👍

search='_search_message_content')


class BaseModel(object):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I bet this shouldn't inherit from object


class BaseModel(object):

_inherit = 'base'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be mail.thread instead? So it should be merged with the class above... 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain a bit more? You said before that we should override it with _inherit = "base". 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I know 😆

Sorry for confusing. I said that before because it's the new way to avoid monkey patches, but after seeing this now, I realized that you only need this code for addons that inherit from mail.thread, so possibly adding the method there should be better.

res = super(BaseModel, self)._fields_view_get(
view_id=view_id, view_type=view_type, toolbar=toolbar,
submenu=submenu)
if view_type == 'search' and self._fields.get('message_content'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you add the method in mail.thread, then you don't need to check and self._fields.get('message_content'), since it would always be true.

operators = {'!=': '=', 'not like': 'like',
'not ilike': 'ilike', 'not in': 'in'}
operator = operators[operator]
domain = [('model', '=', self._name), '|', '|', '|', '|',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that it would be much cleaner and more efficient to do something like:

domain = [('model', '=', self._name)]
if operator not in expression.NEGATIVE_TERM_OPERATORS:
    domain += ["|"] * 4
domain += [('record_name', operator, value),
           ('subject', operator, value), 
           ('body', operator, value),
           ('email_from', operator, value),
           ('reply_to', operator, value)]

message_content = fields.Text(
string='Message Content',
help='Message content, to be used only in searches',
compute="_compute_message_content",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can compute=lambda self: False instead. Just a suggestion.

@@ -22,42 +22,34 @@ def _search_message_content(self, operator, value):
operators = {'!=': '=', 'not like': 'like',
'not ilike': 'ilike', 'not in': 'in'}
operator = operators[operator]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete all above this in the method.

('subject', operator, value),
('body', operator, value),
('email_from', operator, value),
('reply_to', operator, value)]
recs = self.env['mail.message'].search(domain)
return [('id', main_operator, recs.mapped('res_id'))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete these 2 lines and return domain

"data": ["data/trgm_index_data.xml",
"views/trgm_index_view.xml"],
"depends": ["mail",
"base_search_fuzzy"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really a dependency?

Copy link
Contributor Author

@MiquelRForgeFlow MiquelRForgeFlow Aug 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

  • mail is because the inherited mail.thread class
  • base_search_fuzzy, a module in server-tools repository, is because the inherited trgm_index_view.xml view

Copy link
Member

@yajo yajo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good, but UI fails.

for node in doc.xpath("//field[1]"):
# Add message_content in search view
elem = etree.Element('field', {
'name': 'message_content',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not seem to be working, maybe yes it was required to inherit from base instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So... return again to:

class BaseModel(object):
    _inherit = 'base'

🤔?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it seems... Remember that reviewers can be wrong, you should test what you do and let it as it was if new patch does not work 😉


def _search_message_content(self, operator, value):

domain = [('model', '=', self._name)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to break the UI:

Odoo Server Error

Traceback (most recent call last):
  File "/.repo_requirements/odoo/odoo/http.py", line 640, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/.repo_requirements/odoo/odoo/http.py", line 677, in dispatch
    result = self._call_function(**self.params)
  File "/.repo_requirements/odoo/odoo/http.py", line 333, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/.repo_requirements/odoo/odoo/service/model.py", line 101, in wrapper
    return f(dbname, *args, **kwargs)
  File "/.repo_requirements/odoo/odoo/http.py", line 326, in checked_call
    result = self.endpoint(*a, **kw)
  File "/.repo_requirements/odoo/odoo/http.py", line 935, in __call__
    return self.method(*args, **kw)
  File "/.repo_requirements/odoo/odoo/http.py", line 506, in response_wrap
    response = f(*args, **kw)
  File "/home/odoo/odoo-10.0/addons/web/controllers/main.py", line 827, in search_read
    return self.do_search_read(model, fields, offset, limit, domain, sort)
  File "/home/odoo/odoo-10.0/addons/web/controllers/main.py", line 849, in do_search_read
    offset=offset or 0, limit=limit or False, order=sort or False)
  File "/.repo_requirements/odoo/odoo/models.py", line 4678, in search_read
    records = self.search(domain or [], offset=offset, limit=limit, order=order)
  File "/.repo_requirements/odoo/odoo/models.py", line 1518, in search
    res = self._search(args, offset=offset, limit=limit, order=order, count=count)
  File "/.repo_requirements/odoo/odoo/addons/base/res/res_partner.py", line 633, in _search
    count=count, access_rights_uid=access_rights_uid)
  File "/.repo_requirements/odoo/odoo/models.py", line 4220, in _search
    query = self._where_calc(args)
  File "/.repo_requirements/odoo/odoo/models.py", line 4019, in _where_calc
    e = expression.expression(domain, self)
  File "/.repo_requirements/odoo/odoo/osv/expression.py", line 643, in __init__
    self.parse()
  File "/.repo_requirements/odoo/odoo/osv/expression.py", line 821, in parse
    raise ValueError("Invalid field %r in leaf %r" % (left, str(leaf)))
ValueError: Invalid field 'model' in leaf "<osv.ExtendedLeaf: ('model', '=', 'res.partner') on res_partner (ctx: )>"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what do you propose? 😮

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly changing this to domain=[] fixes it.

@rafaelbn rafaelbn self-requested a review August 21, 2017 15:42
@MiquelRForgeFlow
Copy link
Contributor Author

@yajo , could you please update your review?

@yajo
Copy link
Member

yajo commented Sep 7, 2017

I feel current code will not work, have you tested it?

@MiquelRForgeFlow
Copy link
Contributor Author

MiquelRForgeFlow commented Sep 7, 2017

I have just tested, but it has some issues that should be fixed

@rafaelbn
Copy link
Member

rafaelbn commented Sep 7, 2017

Please @mreficent ping me when module is ready for a functional test! thanks

@MiquelRForgeFlow MiquelRForgeFlow force-pushed the 10.0-mig-base_search_mail_content branch 2 times, most recently from bfbcaf7 to 0d144d4 Compare September 7, 2017 17:31
@MiquelRForgeFlow
Copy link
Contributor Author

MiquelRForgeFlow commented Sep 7, 2017

@yajo @rafaelbn Issues solved! Now it's ready functional! 🍏

@rafaelbn
Copy link
Member

rafaelbn commented Sep 7, 2017

Hi @mreficent thank you!!! you should improve test as it has 42.85% of diff hit (target 98.81%)

I will wait until @yajo finish his review ;-) for testing

Copy link

@elicoidal elicoidal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non-blocking suggestions

This module depends on the module 'base_search_fuzzy' to ensure that
searches on emails are based on indexes. Please read carefully the install
instructions:
https://github.com/OCA/server-tools/blob/10.0/base_search_fuzzy/README.rst

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe better:

`install instructions <https://github.com/OCA/server-tools/blob/10.0/base_search_fuzzy/README.rst>`_.

* Lois Rilo Antelo <lois.rilo@eficent.com>
* Aaron Henriquez <ahenriquez@eficent.com>


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove line

search='_search_message_content')


class BaseModel(object):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't seem to do quick search of message contents. Most likely this should inherit from models.AbstractModel instead, although I think you already fixed this before... 🤔

Copy link
Contributor Author

@MiquelRForgeFlow MiquelRForgeFlow Sep 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Postgresql decides when is best to use trigram indexes. You will need to install this module into a DB with a rather large number of emails.

To check if the trigram indexes are being used, you must:

  1. start odoo with --log-level=debug_sql
  2. Do the search that you want to analyze - e.g. search for emails from the leads.
  3. Copy the query and run in command line EXPLAIN ANALYZE (my_query):
    EXPLAIN ANALYZE (SELECT "mail_message".id FROM "mail_message" WHERE (("mail_message"."model" = 'res.partner') AND ((((("mail_message"."record_name"::text ilike 'dear') OR ("mail_message"."subject"::text ilike 'dear')) OR ("mail_message"."body"::text ilike 'dear')) OR ("mail_message"."email_from"::text ilike 'dear')) OR ("mail_message"."reply_to"::text ilike 'dear'))) ORDER BY "mail_message"."id" DESC);

This will tell you if trigram indexes are being used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what?

@JordiBForgeFlow
Copy link
Sponsor Member

the field message_content was not being added to the search view. I have fixed that, and now the module is 🌟 🌟 🌟

@MiquelRForgeFlow
Copy link
Contributor Author

@yajo @rafaelbn update review please 🍏

Copy link
Member

@yajo yajo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excelent!

Please do the final squash

@MiquelRForgeFlow
Copy link
Contributor Author

Squash done

@JordiBForgeFlow
Copy link
Sponsor Member

@rafaelbn can you review?

@pedrobaeza
Copy link
Member

Is there the possibility of improving test coverage?

Copy link
Member

@rafaelbn rafaelbn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested functionaly 👍 but codecov/patch — 38.46% of diff hit (target 98.81%) must be increased please! This is a great funcionality

ping @mreficent @jbeficent

self.thread_obj = self.env["mail.thread"]

def test_base_search_mail_content(self):
res = self.thread_obj._search_message_content('not ilike', 'xxxyyyzzz')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the test should be self.thread_obj.search([("message_content", ...


def test_base_search_mail_content(self):
res = self.thread_obj._search_message_content('not ilike', 'xxxyyyzzz')
self.assertEqual(res[0][2], [], "You have a message with xxxyyyzzz :O")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and then here assertFalse

@MiquelRForgeFlow MiquelRForgeFlow force-pushed the 10.0-mig-base_search_mail_content branch 2 times, most recently from 3070674 to 110b28d Compare September 19, 2017 13:56
@MiquelRForgeFlow
Copy link
Contributor Author

There is not codecov/patch now 😂

Copy link
Member

@rafaelbn rafaelbn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested functionaly 👍

@rafaelbn
Copy link
Member

@mreficent please could answer @yajo 's comments ? I wish tomorrow we can merge this one! Great work!

@MiquelRForgeFlow
Copy link
Contributor Author

MiquelRForgeFlow commented Sep 19, 2017

Which comments? All @yajo's comments have been attended I think.

@yajo
Copy link
Member

yajo commented Sep 20, 2017

Cool! could you squash the migration commits to merge please? 😊

@MiquelRForgeFlow
Copy link
Contributor Author

Done! codecov/patch at 100%

@pedrobaeza pedrobaeza merged commit a2ecf4a into OCA:10.0 Sep 20, 2017
@pedrobaeza pedrobaeza mentioned this pull request Sep 20, 2017
21 tasks
@MiquelRForgeFlow MiquelRForgeFlow deleted the 10.0-mig-base_search_mail_content branch September 20, 2017 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants