[MERGE] forward port of branch 8.0 up to 591e329
[odoo/odoo.git] / addons / website_blog / models / website_blog.py
1 # -*- coding: utf-8 -*-
2
3 from datetime import datetime
4 import difflib
5 import lxml
6 import random
7
8 from openerp import tools
9 from openerp import SUPERUSER_ID
10 from openerp.addons.website.models.website import slug
11 from openerp.osv import osv, fields
12 from openerp.tools.translate import _
13
14
15 class Blog(osv.Model):
16     _name = 'blog.blog'
17     _description = 'Blogs'
18     _inherit = ['mail.thread', 'website.seo.metadata']
19     _order = 'name'
20     _columns = {
21         'name': fields.char('Blog Name', required=True),
22         'subtitle': fields.char('Blog Subtitle'),
23         'description': fields.text('Description'),
24     }
25
26     def all_tags(self, cr, uid, ids, min_limit=1, context=None):
27         req = """
28             SELECT
29                 p.blog_id, count(*), r.blog_tag_id
30             FROM
31                 blog_post_blog_tag_rel r
32                     join blog_post p on r.blog_post_id=p.id
33             WHERE
34                 p.blog_id in %s
35             GROUP BY
36                 p.blog_id,
37                 r.blog_tag_id
38             ORDER BY
39                 count(*) DESC
40         """
41         cr.execute(req, [tuple(ids)])
42         tag_by_blog = {i: [] for i in ids}
43         for blog_id, freq, tag_id in cr.fetchall():
44             if freq >= min_limit:
45                 tag_by_blog[blog_id].append(tag_id)
46
47         tag_obj = self.pool['blog.tag']
48         for blog_id in tag_by_blog:
49             tag_by_blog[blog_id] = tag_obj.browse(cr, uid, tag_by_blog[blog_id], context=context)
50         return tag_by_blog
51
52
53 class BlogTag(osv.Model):
54     _name = 'blog.tag'
55     _description = 'Blog Tag'
56     _inherit = ['website.seo.metadata']
57     _order = 'name'
58     _columns = {
59         'name': fields.char('Name', required=True),
60         'post_ids': fields.many2many(
61             'blog.post', string='Posts',
62         ),
63     }
64
65
66 class BlogPost(osv.Model):
67     _name = "blog.post"
68     _description = "Blog Post"
69     _inherit = ['mail.thread', 'website.seo.metadata']
70     _order = 'id DESC'
71
72     def _compute_ranking(self, cr, uid, ids, name, arg, context=None):
73         res = {}
74         for blog_post in self.browse(cr, uid, ids, context=context):
75             age = datetime.now() - datetime.strptime(blog_post.create_date, tools.DEFAULT_SERVER_DATETIME_FORMAT)
76             res[blog_post.id] = blog_post.visits * (0.5+random.random()) / max(3, age.days)
77         return res
78
79     _columns = {
80         'name': fields.char('Title', required=True, translate=True),
81         'subtitle': fields.char('Sub Title', translate=True),
82         'author_id': fields.many2one('res.partner', 'Author'),
83         'background_image': fields.binary('Background Image', oldname='content_image'),
84         'blog_id': fields.many2one(
85             'blog.blog', 'Blog',
86             required=True, ondelete='cascade',
87         ),
88         'tag_ids': fields.many2many(
89             'blog.tag', string='Tags',
90         ),
91         'content': fields.html('Content', translate=True, sanitize=False),
92         # website control
93         'website_published': fields.boolean(
94             'Publish', help="Publish on the website", copy=False,
95         ),
96         'website_message_ids': fields.one2many(
97             'mail.message', 'res_id',
98             domain=lambda self: [
99                 '&', '&', ('model', '=', self._name), ('type', '=', 'comment'), ('path', '=', False)
100             ],
101             string='Website Messages',
102             help="Website communication history",
103         ),
104         # creation / update stuff
105         'create_date': fields.datetime(
106             'Created on',
107             select=True, readonly=True,
108         ),
109         'create_uid': fields.many2one(
110             'res.users', 'Author',
111             select=True, readonly=True,
112         ),
113         'write_date': fields.datetime(
114             'Last Modified on',
115             select=True, readonly=True,
116         ),
117         'write_uid': fields.many2one(
118             'res.users', 'Last Contributor',
119             select=True, readonly=True,
120         ),
121         'author_avatar': fields.related(
122             'author_id', 'image_small',
123             string="Avatar", type="binary"),
124         'visits': fields.integer('No of Views'),
125         'ranking': fields.function(_compute_ranking, string='Ranking', type='float'),
126     }
127
128     _defaults = {
129         'name': _('Blog Post Title'),
130         'subtitle': _('Subtitle'),
131         'author_id': lambda self, cr, uid, ctx=None: self.pool['res.users'].browse(cr, uid, uid, context=ctx).partner_id.id,
132     }
133
134     def html_tag_nodes(self, html, attribute=None, tags=None, context=None):
135         """ Processing of html content to tag paragraphs and set them an unique
136         ID.
137         :return result: (html, mappin), where html is the updated html with ID
138                         and mapping is a list of (old_ID, new_ID), where old_ID
139                         is None is the paragraph is a new one. """
140         mapping = []
141         if not html:
142             return html, mapping
143         if tags is None:
144             tags = ['p']
145         if attribute is None:
146             attribute = 'data-unique-id'
147         counter = 0
148
149         # form a tree
150         root = lxml.html.fragment_fromstring(html, create_parent='div')
151         if not len(root) and root.text is None and root.tail is None:
152             return html, mapping
153
154         # check all nodes, replace :
155         # - img src -> check URL
156         # - a href -> check URL
157         for node in root.iter():
158             if not node.tag in tags:
159                 continue
160             ancestor_tags = [parent.tag for parent in node.iterancestors()]
161             if ancestor_tags:
162                 ancestor_tags.pop()
163             ancestor_tags.append('counter_%s' % counter)
164             new_attribute = '/'.join(reversed(ancestor_tags))
165             old_attribute = node.get(attribute)
166             node.set(attribute, new_attribute)
167             mapping.append((old_attribute, counter))
168             counter += 1
169
170         html = lxml.html.tostring(root, pretty_print=False, method='html')
171         # this is ugly, but lxml/etree tostring want to put everything in a 'div' that breaks the editor -> remove that
172         if html.startswith('<div>') and html.endswith('</div>'):
173             html = html[5:-6]
174         return html, mapping
175
176     def _postproces_content(self, cr, uid, id, content=None, context=None):
177         if content is None:
178             content = self.browse(cr, uid, id, context=context).content
179         if content is False:
180             return content
181         content, mapping = self.html_tag_nodes(content, attribute='data-chatter-id', tags=['p'], context=context)
182         for old_attribute, new_attribute in mapping:
183             if not old_attribute:
184                 continue
185             msg_ids = self.pool['mail.message'].search(cr, SUPERUSER_ID, [('path', '=', old_attribute)], context=context)
186             self.pool['mail.message'].write(cr, SUPERUSER_ID, msg_ids, {'path': new_attribute}, context=context)
187         return content
188
189     def _check_for_publication(self, cr, uid, ids, vals, context=None):
190         if vals.get('website_published'):
191             base_url = self.pool['ir.config_parameter'].get_param(cr, uid, 'web.base.url')
192             for post in self.browse(cr, uid, ids, context=context):
193                 post.blog_id.message_post(
194                     body='<p>%(post_publication)s <a href="%(base_url)s/blog/%(blog_slug)s/post/%(post_slug)s">%(post_link)s</a></p>' % {
195                         'post_publication': _('A new post %s has been published on the %s blog.') % (post.name, post.blog_id.name),
196                         'post_link': _('Click here to access the post.'),
197                         'base_url': base_url,
198                         'blog_slug': slug(post.blog_id),
199                         'post_slug': slug(post),
200                     },
201                     subtype='website_blog.mt_blog_blog_published',
202                     context=context)
203             return True
204         return False
205
206     def create(self, cr, uid, vals, context=None):
207         if context is None:
208             context = {}
209         if 'content' in vals:
210             vals['content'] = self._postproces_content(cr, uid, None, vals['content'], context=context)
211         create_context = dict(context, mail_create_nolog=True)
212         post_id = super(BlogPost, self).create(cr, uid, vals, context=create_context)
213         self._check_for_publication(cr, uid, [post_id], vals, context=context)
214         return post_id
215
216     def write(self, cr, uid, ids, vals, context=None):
217         if 'content' in vals:
218             vals['content'] = self._postproces_content(cr, uid, None, vals['content'], context=context)
219         result = super(BlogPost, self).write(cr, uid, ids, vals, context)
220         self._check_for_publication(cr, uid, ids, vals, context=context)
221         return result