[MERGE] from master
[odoo/odoo.git] / addons / website_blog / models / website_blog.py
index 8a52caa..70cd18b 100644 (file)
@@ -7,6 +7,7 @@ import random
 
 from openerp import tools
 from openerp import SUPERUSER_ID
+from openerp.addons.website.models.website import slug
 from openerp.osv import osv, fields
 from openerp.tools.translate import _
 
@@ -22,6 +23,32 @@ class Blog(osv.Model):
         'description': fields.text('Description'),
     }
 
+    def all_tags(self, cr, uid, ids, min_limit=1, context=None):
+        req = """
+            SELECT
+                p.blog_id, count(*), r.blog_tag_id
+            FROM
+                blog_post_blog_tag_rel r
+                    join blog_post p on r.blog_post_id=p.id
+            WHERE
+                p.blog_id in %s
+            GROUP BY
+                p.blog_id,
+                r.blog_tag_id
+            ORDER BY
+                count(*) DESC
+        """
+        cr.execute(req, [tuple(ids)])
+        tag_by_blog = {i: [] for i in ids}
+        for blog_id, freq, tag_id in cr.fetchall():
+            if freq >= min_limit:
+                tag_by_blog[blog_id].append(tag_id)
+
+        tag_obj = self.pool['blog.tag']
+        for blog_id in tag_by_blog:
+            tag_by_blog[blog_id] = tag_obj.browse(cr, uid, tag_by_blog[blog_id], context=context)
+        return tag_by_blog
+
 
 class BlogTag(osv.Model):
     _name = 'blog.tag'
@@ -30,6 +57,9 @@ class BlogTag(osv.Model):
     _order = 'name'
     _columns = {
         'name': fields.char('Name', required=True),
+        'post_ids': fields.many2many(
+            'blog.post', string='Posts',
+        ),
     }
 
 
@@ -156,6 +186,23 @@ class BlogPost(osv.Model):
             self.pool['mail.message'].write(cr, SUPERUSER_ID, msg_ids, {'path': new_attribute}, context=context)
         return content
 
+    def _check_for_publication(self, cr, uid, ids, vals, context=None):
+        if vals.get('website_published'):
+            base_url = self.pool['ir.config_parameter'].get_param(cr, uid, 'web.base.url')
+            for post in self.browse(cr, uid, ids, context=context):
+                post.blog_id.message_post(
+                    body='<p>%(post_publication)s <a href="%(base_url)s/blog/%(blog_slug)s/post/%(post_slug)s">%(post_link)s</a></p>' % {
+                        'post_publication': _('A new post %s has been published on the %s blog.') % (post.name, post.blog_id.name),
+                        'post_link': _('Click here to access the post.'),
+                        'base_url': base_url,
+                        'blog_slug': slug(post.blog_id),
+                        'post_slug': slug(post),
+                    },
+                    subtype='website_blog.mt_blog_blog_published',
+                    context=context)
+            return True
+        return False
+
     def create(self, cr, uid, vals, context=None):
         if context is None:
             context = {}
@@ -163,10 +210,12 @@ class BlogPost(osv.Model):
             vals['content'] = self._postproces_content(cr, uid, None, vals['content'], context=context)
         create_context = dict(context, mail_create_nolog=True)
         post_id = super(BlogPost, self).create(cr, uid, vals, context=create_context)
+        self._check_for_publication(cr, uid, [post_id], vals, context=context)
         return post_id
 
     def write(self, cr, uid, ids, vals, context=None):
         if 'content' in vals:
             vals['content'] = self._postproces_content(cr, uid, None, vals['content'], context=context)
         result = super(BlogPost, self).write(cr, uid, ids, vals, context)
+        self._check_for_publication(cr, uid, ids, vals, context=context)
         return result