ab96cc5b7f85b5d1601c10ddd008330073bc9c63
[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.osv import osv, fields
11 from openerp.tools.translate import _
12
13
14 class Blog(osv.Model):
15     _name = 'blog.blog'
16     _description = 'Blogs'
17     _inherit = ['mail.thread', 'website.seo.metadata']
18     _order = 'name'
19
20     _columns = {
21         'name': fields.char('Blog Name', required=True),
22         'subtitle': fields.char('Blog Subtitle'),
23         'description': fields.text('Description'),
24     }
25
26
27 class BlogTag(osv.Model):
28     _name = 'blog.tag'
29     _description = 'Blog Tag'
30     _inherit = ['website.seo.metadata']
31     _order = 'name'
32
33     _columns = {
34         'name': fields.char('Name', required=True),
35     }
36
37
38 class BlogPost(osv.Model):
39     _name = "blog.post"
40     _description = "Blog Post"
41     _inherit = ['mail.thread', 'website.seo.metadata']
42     _order = 'id DESC'
43
44     def _compute_ranking(self, cr, uid, ids, name, arg, context=None):
45         res = {}
46         for blog_post in self.browse(cr, uid, ids, context=context):
47             age = datetime.now() - datetime.strptime(blog_post.create_date, tools.DEFAULT_SERVER_DATETIME_FORMAT)
48             res[blog_post.id] = blog_post.visits * (0.5+random.random()) / max(3, age.days)
49         return res
50
51     _columns = {
52         'name': fields.char('Title', required=True, translate=True),
53         'subtitle': fields.char('Sub Title', translate=True),
54         'author_id': fields.many2one('res.partner', 'Author'),
55         'background_image': fields.binary('Background Image', oldname='content_image'),
56         'blog_id': fields.many2one(
57             'blog.blog', 'Blog',
58             required=True, ondelete='cascade',
59         ),
60         'tag_ids': fields.many2many(
61             'blog.tag', string='Tags',
62         ),
63         'content': fields.html('Content', translate=True),
64         # website control
65         'website_published': fields.boolean(
66             'Publish', help="Publish on the website"
67         ),
68         'website_message_ids': fields.one2many(
69             'mail.message', 'res_id',
70             domain=lambda self: [
71                 '&', '&', ('model', '=', self._name), ('type', '=', 'comment'), ('path', '=', False)
72             ],
73             string='Website Messages',
74             help="Website communication history",
75         ),
76         'history_ids': fields.one2many(
77             'blog.post.history', 'post_id',
78             'History', help='Last post modifications',
79             deprecated='This field will be removed for OpenERP v9.'
80         ),
81         # creation / update stuff
82         'create_date': fields.datetime(
83             'Created on',
84             select=True, readonly=True,
85         ),
86         'create_uid': fields.many2one(
87             'res.users', 'Author',
88             select=True, readonly=True,
89         ),
90         'write_date': fields.datetime(
91             'Last Modified on',
92             select=True, readonly=True,
93         ),
94         'write_uid': fields.many2one(
95             'res.users', 'Last Contributor',
96             select=True, readonly=True,
97         ),
98         'visits': fields.integer('No of Views'),
99         'ranking': fields.function(_compute_ranking, string='Ranking', type='float'),
100     }
101
102     _defaults = {
103         'name': _('Blog Post Title'),
104         'subtitle': _('Subtitle'),
105         'author_id': lambda self, cr, uid, ctx=None: self.pool['res.users'].browse(cr, uid, uid, context=ctx).partner_id.id,
106     }
107
108     def html_tag_nodes(self, html, attribute=None, tags=None, context=None):
109         """ Processing of html content to tag paragraphs and set them an unique
110         ID.
111         :return result: (html, mappin), where html is the updated html with ID
112                         and mapping is a list of (old_ID, new_ID), where old_ID
113                         is None is the paragraph is a new one. """
114         mapping = []
115         if not html:
116             return html, mapping
117         if tags is None:
118             tags = ['p']
119         if attribute is None:
120             attribute = 'data-unique-id'
121         counter = 0
122
123         # form a tree
124         root = lxml.html.fragment_fromstring(html, create_parent='div')
125         if not len(root) and root.text is None and root.tail is None:
126             return html, mapping
127
128         # check all nodes, replace :
129         # - img src -> check URL
130         # - a href -> check URL
131         for node in root.iter():
132             if not node.tag in tags:
133                 continue
134             ancestor_tags = [parent.tag for parent in node.iterancestors()]
135             if ancestor_tags:
136                 ancestor_tags.pop()
137             ancestor_tags.append('counter_%s' % counter)
138             new_attribute = '/'.join(reversed(ancestor_tags))
139             old_attribute = node.get(attribute)
140             node.set(attribute, new_attribute)
141             mapping.append((old_attribute, counter))
142             counter += 1
143
144         html = lxml.html.tostring(root, pretty_print=False, method='html')
145         # this is ugly, but lxml/etree tostring want to put everything in a 'div' that breaks the editor -> remove that
146         if html.startswith('<div>') and html.endswith('</div>'):
147             html = html[5:-6]
148         return html, mapping
149
150     def _postproces_content(self, cr, uid, id, content=None, context=None):
151         if content is None:
152             content = self.browse(cr, uid, id, context=context).content
153         if content is False:
154             return content
155         content, mapping = self.html_tag_nodes(content, attribute='data-chatter-id', tags=['p'], context=context)
156         for old_attribute, new_attribute in mapping:
157             if not old_attribute:
158                 continue
159             msg_ids = self.pool['mail.message'].search(cr, SUPERUSER_ID, [('path', '=', old_attribute)], context=context)
160             self.pool['mail.message'].write(cr, SUPERUSER_ID, msg_ids, {'path': new_attribute}, context=context)
161         return content
162
163     def create_history(self, cr, uid, ids, vals, context=None):
164         for i in ids:
165             history = self.pool.get('blog.post.history')
166             if vals.get('content'):
167                 res = {
168                     'content': vals.get('content', ''),
169                     'post_id': i,
170                 }
171                 history.create(cr, uid, res)
172
173     def create(self, cr, uid, vals, context=None):
174         if context is None:
175             context = {}
176         if 'content' in vals:
177             vals['content'] = self._postproces_content(cr, uid, None, vals['content'], context=context)
178         create_context = dict(context, mail_create_nolog=True)
179         post_id = super(BlogPost, self).create(cr, uid, vals, context=create_context)
180         self.create_history(cr, uid, [post_id], vals, context)
181         return post_id
182
183     def write(self, cr, uid, ids, vals, context=None):
184         if 'content' in vals:
185             vals['content'] = self._postproces_content(cr, uid, None, vals['content'], context=context)
186         result = super(BlogPost, self).write(cr, uid, ids, vals, context)
187         self.create_history(cr, uid, ids, vals, context)
188         return result
189
190     def copy(self, cr, uid, id, default=None, context=None):
191         if default is None:
192             default = {}
193         default.update({
194             'website_message_ids': [],
195             'website_published': False,
196             'website_published_datetime': False,
197         })
198         return super(BlogPost, self).copy(cr, uid, id, default=default, context=context)
199
200
201 class BlogPostHistory(osv.Model):
202     _name = "blog.post.history"
203     _description = "Blog Post History"
204     _order = 'id DESC'
205     _rec_name = "create_date"
206
207     _columns = {
208         'post_id': fields.many2one('blog.post', 'Blog Post'),
209         'summary': fields.char('Summary', size=256, select=True),
210         'content': fields.text("Content"),
211         'create_date': fields.datetime("Date"),
212         'create_uid': fields.many2one('res.users', "Modified By"),
213     }
214
215     def getDiff(self, cr, uid, v1, v2, context=None):
216         history_pool = self.pool.get('blog.post.history')
217         text1 = history_pool.read(cr, uid, [v1], ['content'])[0]['content']
218         text2 = history_pool.read(cr, uid, [v2], ['content'])[0]['content']
219         line1 = line2 = ''
220         if text1:
221             line1 = text1.splitlines(1)
222         if text2:
223             line2 = text2.splitlines(1)
224         if (not line1 and not line2) or (line1 == line2):
225             raise osv.except_osv(_('Warning!'), _('There are no changes in revisions.'))
226         diff = difflib.HtmlDiff()
227         return diff.make_table(line1, line2, "Revision-%s" % (v1), "Revision-%s" % (v2), context=True)