1 # -*- coding: utf-8 -*-
3 from datetime import datetime
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 _
15 class Blog(osv.Model):
17 _description = 'Blogs'
18 _inherit = ['mail.thread', 'website.seo.metadata']
21 'name': fields.char('Blog Name', required=True),
22 'subtitle': fields.char('Blog Subtitle'),
23 'description': fields.text('Description'),
26 def all_tags(self, cr, uid, ids, min_limit=1, context=None):
29 p.blog_id, count(*), r.blog_tag_id
31 blog_post_blog_tag_rel r
32 join blog_post p on r.blog_post_id=p.id
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():
45 tag_by_blog[blog_id].append(tag_id)
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)
53 class BlogTag(osv.Model):
55 _description = 'Blog Tag'
56 _inherit = ['website.seo.metadata']
59 'name': fields.char('Name', required=True),
60 'post_ids': fields.many2many(
61 'blog.post', string='Posts',
66 class BlogPost(osv.Model):
68 _description = "Blog Post"
69 _inherit = ['mail.thread', 'website.seo.metadata']
72 def _compute_ranking(self, cr, uid, ids, name, arg, context=None):
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)
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(
86 required=True, ondelete='cascade',
88 'tag_ids': fields.many2many(
89 'blog.tag', string='Tags',
91 'content': fields.html('Content', translate=True, sanitize=False),
93 'website_published': fields.boolean(
94 'Publish', help="Publish on the website", copy=False,
96 'website_message_ids': fields.one2many(
97 'mail.message', 'res_id',
99 '&', '&', ('model', '=', self._name), ('type', '=', 'comment'), ('path', '=', False)
101 string='Website Messages',
102 help="Website communication history",
104 # creation / update stuff
105 'create_date': fields.datetime(
107 select=True, readonly=True,
109 'create_uid': fields.many2one(
110 'res.users', 'Author',
111 select=True, readonly=True,
113 'write_date': fields.datetime(
115 select=True, readonly=True,
117 'write_uid': fields.many2one(
118 'res.users', 'Last Contributor',
119 select=True, readonly=True,
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'),
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,
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
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. """
145 if attribute is None:
146 attribute = 'data-unique-id'
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:
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:
160 ancestor_tags = [parent.tag for parent in node.iterancestors()]
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))
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>'):
176 def _postproces_content(self, cr, uid, id, content=None, context=None):
178 content = self.browse(cr, uid, id, context=context).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:
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)
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),
201 subtype='website_blog.mt_blog_blog_published',
206 def create(self, cr, uid, vals, context=None):
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)
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)