From 486aab5098fd757dfad06e63b9f3bde51a78cb0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Wed, 23 Sep 2015 16:39:36 +0200 Subject: [PATCH] [FIX][BACK FROM TRASH] mail: chatter: read more / read less Read more / less feature is back (and probably ugly). It has probably been removed when refactoring the old chatter. [FIX] tools: html shortening: encoding issue --- addons/mail/models/mail_message.py | 9 ++++----- .../mail/static/src/js/mail_chat_backend.js | 1 + addons/mail/static/src/js/mail_chatter.js | 1 + addons/mail/static/src/js/mail_thread.js | 10 ++++++++++ addons/mail/static/src/less/mail_thread.less | 3 +++ addons/mail/static/src/xml/mail_thread.xml | 14 +++++++++++++- openerp/addons/base/tests/test_mail.py | 2 +- openerp/tools/mail.py | 19 ++++++++++++------- 8 files changed, 45 insertions(+), 14 deletions(-) diff --git a/addons/mail/models/mail_message.py b/addons/mail/models/mail_message.py index 5380be4b072..bab78cd4b07 100644 --- a/addons/mail/models/mail_message.py +++ b/addons/mail/models/mail_message.py @@ -185,8 +185,6 @@ class Message(models.Model): :param list messages: list of message, as get_dict result :param dict message_tree: {[msg.id]: msg browse record} """ - pid = self.env.user.partner_id.id - # 1. Aggregate partners (author_id and partner_ids), attachments and tracking values partners = self.env['res.partner'] attachments = self.env['ir.attachment'] @@ -248,13 +246,13 @@ class Message(models.Model): tracking_value_ids.append(tracking_tree[tracking_value.id]) message_dict.update({ - #'is_author': pid == author[0], 'author_id': author, 'partner_ids': partner_ids, 'attachment_ids': attachment_ids, 'tracking_value_ids': tracking_value_ids, - #'user_pid': pid - }) + }) + body_short = tools.html_email_clean(message_dict['body'], remove=True) + message_dict['body_short'] = body_short != message_dict['body'] and body_short or False return True @@ -326,6 +324,7 @@ class Message(models.Model): - int: number of messages read (status 'unread' to 'read') - list: list of threads [[messages_of_thread1], [messages_of_thread2]] """ + # TDE note: seems deprecated, not used but however still kept - why ? assert thread_level in [0, 1], 'message_read() thread_level should be 0 (flat) or 1 (1 level of thread); given %s.' % thread_level domain = domain if domain is not None else [] diff --git a/addons/mail/static/src/js/mail_chat_backend.js b/addons/mail/static/src/js/mail_chat_backend.js index 6590e893973..d0966dd3088 100644 --- a/addons/mail/static/src/js/mail_chat_backend.js +++ b/addons/mail/static/src/js/mail_chat_backend.js @@ -378,6 +378,7 @@ var ChatMailThread = Widget.extend(mail_thread.MailThreadMixin, ControlPanelMixi "click .o_mail_thread_show_more > button": "message_load_history", // events from MailThreadMixin "click .o_mail_redirect": "on_click_redirect", + "click .o_mail_expand": "on_message_body_toggle", "click .o_mail_thread_message_star": "on_message_star", // events specific for ChatMailThread "click .o_mail_chat_channel_item": "on_click_channel", diff --git a/addons/mail/static/src/js/mail_chatter.js b/addons/mail/static/src/js/mail_chatter.js index 6d031be6404..8fbe8c1ea05 100644 --- a/addons/mail/static/src/js/mail_chatter.js +++ b/addons/mail/static/src/js/mail_chatter.js @@ -273,6 +273,7 @@ var ChatterMailThread = form_common.AbstractField.extend(mail_thread.MailThreadM "click .o_mail_thread_message_star": "on_message_star", "click .o_mail_thread_message_needaction": "on_message_needaction", "click .o_mail_thread_show_more": "message_load_history", + "click .o_mail_expand": "on_message_body_toggle", // toggle message composer (!! declaration order is important !!) "click .o_mail_chatter_button_new_message": "on_open_composer", "click .o_mail_chatter_button_log_note": "on_open_composer", diff --git a/addons/mail/static/src/js/mail_thread.js b/addons/mail/static/src/js/mail_thread.js index bcd9f47410d..6511e6a6583 100644 --- a/addons/mail/static/src/js/mail_thread.js +++ b/addons/mail/static/src/js/mail_thread.js @@ -617,6 +617,16 @@ var MailThreadMixin = { */ on_reverse_breadcrumb: function(){ + }, + /** + * Toggle short / long body when present (hide signature / crop long + messages) + */ + on_message_body_toggle: function (event) { + event.preventDefault(); + var $source = this.$(event.currentTarget); + $source.parents('.o_mail_thread_message_core').find('.o_mail_body_short').toggle(); + $source.parents('.o_mail_thread_message_core').find('.o_mail_body_long').toggle(); }, /** * Toggle a message as 'starred' (or not), by toggling the 'o_mail_message_starred' css class. diff --git a/addons/mail/static/src/less/mail_thread.less b/addons/mail/static/src/less/mail_thread.less index c502a528c8d..3f7b78201fe 100644 --- a/addons/mail/static/src/less/mail_thread.less +++ b/addons/mail/static/src/less/mail_thread.less @@ -44,6 +44,9 @@ > p { margin: 0 0 9px; // Required by the old design to override a general rule on p's } + .o_mail_body_long { + display: none; + } } .o_mail_thread_message_star, .o_mail_thread_message_needaction { cursor: pointer; diff --git a/addons/mail/static/src/xml/mail_thread.xml b/addons/mail/static/src/xml/mail_thread.xml index e650dd323fc..1323cb9c21e 100644 --- a/addons/mail/static/src/xml/mail_thread.xml +++ b/addons/mail/static/src/xml/mail_thread.xml @@ -141,7 +141,19 @@ - + + + + + + + diff --git a/openerp/addons/base/tests/test_mail.py b/openerp/addons/base/tests/test_mail.py index 0fa86ccc448..982a18f329e 100644 --- a/openerp/addons/base/tests/test_mail.py +++ b/openerp/addons/base/tests/test_mail.py @@ -316,7 +316,7 @@ class TestCleaner(unittest2.TestCase): for ext in test_mail_examples.BUG_1_IN: self.assertIn(ext, new_html, 'html_email_cleaner wrongly removed valid content') for ext in test_mail_examples.BUG_1_OUT: - self.assertNotIn(ext, new_html, 'html_email_cleaner did not removed invalid content') + self.assertNotIn(ext.decode('utf-8'), new_html, 'html_email_cleaner did not removed invalid content') new_html = html_email_clean(test_mail_examples.BUG2, remove=True, shorten=True, max_length=250) for ext in test_mail_examples.BUG_2_IN: diff --git a/openerp/tools/mail.py b/openerp/tools/mail.py index 4d17b1657bc..45b16dd811f 100644 --- a/openerp/tools/mail.py +++ b/openerp/tools/mail.py @@ -307,13 +307,13 @@ def html_email_clean(html, remove=False, shorten=False, max_length=300, expand_o # html: ClEditor seems to love using

-> replace with
br_div_tags = re.compile(r'(
\s*\s*<\/div>)', re.IGNORECASE) - html = _replace_matching_regex(br_div_tags, html, '
') + inner_html = _replace_matching_regex(br_div_tags, html, '
') # form a tree - root = lxml.html.fromstring(html) + root = lxml.html.fromstring(inner_html) if not len(root) and root.text is None and root.tail is None: - html = '
%s
' % html - root = lxml.html.fromstring(html) + inner_html = '
%s
' % inner_html + root = lxml.html.fromstring(inner_html) quote_tags = re.compile(r'(\n(>)+[^\n\r]*)') signature = re.compile(r'([-]{2,}[\s]?[\r\n]{1,2}[\s\S]+)') @@ -333,6 +333,7 @@ def html_email_clean(html, remove=False, shorten=False, max_length=300, expand_o # tree: tag nodes # signature_begin = False # try dynamic signature recognition + quoted = False quote_begin = False overlength = False overlength_section_id = None @@ -381,9 +382,11 @@ def html_email_clean(html, remove=False, shorten=False, max_length=300, expand_o if node.tag == 'blockquote' or node.get('text_quote') or node.get('text_signature'): # here no quote_begin because we want to be able to remove some quoted # text without removing all the remaining context + quoted = True node.set('in_quote', '1') if node.getparent() is not None and node.getparent().get('in_quote'): # inside a block of removed text but not in quote_begin (see above) + quoted = True node.set('in_quote', '1') # shorten: @@ -445,12 +448,14 @@ def html_email_clean(html, remove=False, shorten=False, max_length=300, expand_o node_class = node.get('class', '') + ' oe_mail_cleaned' node.set('class', node_class) + if not overlength and not quote_begin and not quoted: + return html + # html: \n that were tail of elements have been encapsulated into -> back to \n - html = etree.tostring(root, pretty_print=False) + html = etree.tostring(root, pretty_print=False, encoding='UTF-8') linebreaks = re.compile(r']*>([\s]*[\r\n]+[\s]*)<\/span>', re.IGNORECASE | re.DOTALL) html = _replace_matching_regex(linebreaks, html, '\n') - - return html + return ustr(html) #----------------------------------------------------------