website_blog.py 11 KB

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