1 # -*- coding: utf-8 -*-
3 from datetime import datetime
8 from openerp import tools
9 from openerp import SUPERUSER_ID
10 from openerp.osv import osv, fields
11 from openerp.tools.translate import _
14 class Blog(osv.Model):
16 _description = 'Blogs'
17 _inherit = ['mail.thread', 'website.seo.metadata']
20 'name': fields.char('Blog Name', required=True),
21 'subtitle': fields.char('Blog Subtitle'),
22 'description': fields.text('Description'),
26 class BlogTag(osv.Model):
28 _description = 'Blog Tag'
29 _inherit = ['website.seo.metadata']
32 'name': fields.char('Name', required=True),
36 class BlogPost(osv.Model):
38 _description = "Blog Post"
39 _inherit = ['mail.thread', 'website.seo.metadata']
42 def _compute_ranking(self, cr, uid, ids, name, arg, context=None):
44 for blog_post in self.browse(cr, uid, ids, context=context):
45 age = datetime.now() - datetime.strptime(blog_post.create_date, tools.DEFAULT_SERVER_DATETIME_FORMAT)
46 res[blog_post.id] = blog_post.visits * (0.5+random.random()) / max(3, age.days)
50 'name': fields.char('Title', required=True, translate=True),
51 'subtitle': fields.char('Sub Title', translate=True),
52 'author_id': fields.many2one('res.partner', 'Author'),
53 'background_image': fields.binary('Background Image', oldname='content_image'),
54 'blog_id': fields.many2one(
56 required=True, ondelete='cascade',
58 'tag_ids': fields.many2many(
59 'blog.tag', string='Tags',
61 'content': fields.html('Content', translate=True),
63 'website_published': fields.boolean(
64 'Publish', help="Publish on the website"
66 'website_message_ids': fields.one2many(
67 'mail.message', 'res_id',
69 '&', '&', ('model', '=', self._name), ('type', '=', 'comment'), ('path', '=', False)
71 string='Website Messages',
72 help="Website communication history",
74 'history_ids': fields.one2many(
75 'blog.post.history', 'post_id',
76 'History', help='Last post modifications',
78 # creation / update stuff
79 'create_date': fields.datetime(
81 select=True, readonly=True,
83 'create_uid': fields.many2one(
84 'res.users', 'Author',
85 select=True, readonly=True,
87 'write_date': fields.datetime(
89 select=True, readonly=True,
91 'write_uid': fields.many2one(
92 'res.users', 'Last Contributor',
93 select=True, readonly=True,
95 'visits': fields.integer('No of Views'),
96 'ranking': fields.function(_compute_ranking, string='Ranking', type='float'),
100 'name': _('Blog Post Title'),
101 'subtitle': _('Subtitle'),
102 'author_id': lambda self, cr, uid, ctx=None: self.pool['res.users'].browse(cr, uid, uid, context=ctx).partner_id.id,
105 def html_tag_nodes(self, html, attribute=None, tags=None, context=None):
106 """ Processing of html content to tag paragraphs and set them an unique
108 :return result: (html, mappin), where html is the updated html with ID
109 and mapping is a list of (old_ID, new_ID), where old_ID
110 is None is the paragraph is a new one. """
116 if attribute is None:
117 attribute = 'data-unique-id'
121 root = lxml.html.fragment_fromstring(html, create_parent='div')
122 if not len(root) and root.text is None and root.tail is None:
125 # check all nodes, replace :
126 # - img src -> check URL
127 # - a href -> check URL
128 for node in root.iter():
129 if not node.tag in tags:
131 ancestor_tags = [parent.tag for parent in node.iterancestors()]
134 ancestor_tags.append('counter_%s' % counter)
135 new_attribute = '/'.join(reversed(ancestor_tags))
136 old_attribute = node.get(attribute)
137 node.set(attribute, new_attribute)
138 mapping.append((old_attribute, counter))
141 html = lxml.html.tostring(root, pretty_print=False, method='html')
142 # this is ugly, but lxml/etree tostring want to put everything in a 'div' that breaks the editor -> remove that
143 if html.startswith('<div>') and html.endswith('</div>'):
147 def _postproces_content(self, cr, uid, id, content=None, context=None):
149 content = self.browse(cr, uid, id, context=context).content
152 content, mapping = self.html_tag_nodes(content, attribute='data-chatter-id', tags=['p'], context=context)
153 for old_attribute, new_attribute in mapping:
154 if not old_attribute:
156 msg_ids = self.pool['mail.message'].search(cr, SUPERUSER_ID, [('path', '=', old_attribute)], context=context)
157 self.pool['mail.message'].write(cr, SUPERUSER_ID, msg_ids, {'path': new_attribute}, context=context)
160 def create_history(self, cr, uid, ids, vals, context=None):
162 history = self.pool.get('blog.post.history')
163 if vals.get('content'):
165 'content': vals.get('content', ''),
168 history.create(cr, uid, res)
170 def create(self, cr, uid, vals, context=None):
173 if 'content' in vals:
174 vals['content'] = self._postproces_content(cr, uid, None, vals['content'], context=context)
175 create_context = dict(context, mail_create_nolog=True)
176 post_id = super(BlogPost, self).create(cr, uid, vals, context=create_context)
177 self.create_history(cr, uid, [post_id], vals, context)
180 def write(self, cr, uid, ids, vals, context=None):
181 if 'content' in vals:
182 vals['content'] = self._postproces_content(cr, uid, None, vals['content'], context=context)
183 result = super(BlogPost, self).write(cr, uid, ids, vals, context)
184 self.create_history(cr, uid, ids, vals, context)
187 def copy(self, cr, uid, id, default=None, context=None):
191 'website_message_ids': [],
192 'website_published': False,
193 'website_published_datetime': False,
195 return super(BlogPost, self).copy(cr, uid, id, default=default, context=context)
198 class BlogPostHistory(osv.Model):
199 _name = "blog.post.history"
200 _description = "Blog Post History"
202 _rec_name = "create_date"
205 'post_id': fields.many2one('blog.post', 'Blog Post'),
206 'summary': fields.char('Summary', select=True),
207 'content': fields.text("Content"),
208 'create_date': fields.datetime("Date"),
209 'create_uid': fields.many2one('res.users', "Modified By"),
212 def getDiff(self, cr, uid, v1, v2, context=None):
213 history_pool = self.pool.get('blog.post.history')
214 text1 = history_pool.read(cr, uid, [v1], ['content'])[0]['content']
215 text2 = history_pool.read(cr, uid, [v2], ['content'])[0]['content']
218 line1 = text1.splitlines(1)
220 line2 = text2.splitlines(1)
221 if (not line1 and not line2) or (line1 == line2):
222 raise osv.except_osv(_('Warning!'), _('There are no changes in revisions.'))
223 diff = difflib.HtmlDiff()
224 return diff.make_table(line1, line2, "Revision-%s" % (v1), "Revision-%s" % (v2), context=True)