[IMP] website_forum: better implementation of automatic nofollow attribute
on links in forum posts. This is now done when creating / updating posts instead of updating the display of the post. This means that old posts may contain nofollow attribute altough the user has sufficient karma; however it is acceptable compared to the computation we save by avoiding processing every post to display.
This commit is contained in:
@@ -290,13 +290,6 @@ class HTML(orm.AbstractModel):
|
||||
for child in element.iterchildren(tag=etree.Element))
|
||||
return '\n'.join(content)
|
||||
|
||||
def value_to_html(self, cr, uid, value, column, options=None, context=None):
|
||||
if options and options.get("nofollow") and value:
|
||||
base_url = self.pool['ir.config_parameter'].get_param(cr, uid, 'web.base.url', context=context)
|
||||
for match in re.findall(r'<a\s.*href="(?!%s).*?">' % base_url, value):
|
||||
value = re.sub(match, match[:3] + 'rel="nofollow" ' + match[3:], value)
|
||||
return super(HTML, self).value_to_html(cr, uid, value, column, options, context=context)
|
||||
|
||||
class Image(orm.AbstractModel):
|
||||
"""
|
||||
Widget options:
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
from datetime import datetime
|
||||
import logging
|
||||
import math
|
||||
import re
|
||||
import uuid
|
||||
from werkzeug.exceptions import Forbidden
|
||||
|
||||
@@ -278,8 +279,18 @@ class Post(models.Model):
|
||||
self.can_comment = user.karma >= self.karma_comment
|
||||
self.can_comment_convert = user.karma >= self.karma_comment_convert
|
||||
|
||||
def _update_content(self, content, forum_id):
|
||||
forum = self.env['forum.forum'].browse(forum_id)
|
||||
if content and self.env.user.karma < forum.karma_dofollow:
|
||||
for match in re.findall(r'<a\s.*href=".*?">', content):
|
||||
content = re.sub(match, match[:3] + 'rel="nofollow" ' + match[3:], content)
|
||||
return content
|
||||
|
||||
@api.model
|
||||
def create(self, vals):
|
||||
if 'content' in vals and vals.get('forum_id'):
|
||||
vals['content'] = self._update_content(vals['content'], vals['forum_id'])
|
||||
|
||||
post = super(Post, self.with_context(mail_create_nolog=True)).create(vals)
|
||||
# deleted or closed questions
|
||||
if post.parent_id and (post.parent_id.state == 'close' or post.parent_id.active is False):
|
||||
@@ -308,6 +319,8 @@ class Post(models.Model):
|
||||
|
||||
@api.multi
|
||||
def write(self, vals):
|
||||
if 'content' in vals:
|
||||
vals['content'] = self._update_content(vals['content'], self.forum_id.id)
|
||||
if 'state' in vals:
|
||||
if vals['state'] in ['active', 'close'] and any(not post.can_close for post in self):
|
||||
raise KarmaError('Not enough karma to close or reopen a post.')
|
||||
|
||||
@@ -637,8 +637,7 @@
|
||||
</t>
|
||||
</div>
|
||||
</div>
|
||||
<div t-if="question.post_type != 'link' and question.create_uid.karma >= forum.karma_dofollow"><span t-field="question.content" class="oe_no_empty"/></div>
|
||||
<div t-if="question.post_type != 'link' and question.create_uid.karma < forum.karma_dofollow"><span t-field="question.content" class="oe_no_empty" t-field-options='{"nofollow": 1}'/></div>
|
||||
<div t-if="question.post_type != 'link'"><span t-field="question.content" class="oe_no_empty"/></div>
|
||||
|
||||
<div class="clearfix">
|
||||
<ul class="pull-right list-inline mb0" id="options">
|
||||
@@ -738,8 +737,7 @@
|
||||
style="display: inline-block;"/>
|
||||
<div class="text-muted">On <span t-field="answer.create_date" t-field-options='{"format":"short"}'/></div>
|
||||
</div>
|
||||
<div class="clearfix" t-if="answer.create_uid.karma >= forum.karma_dofollow"><span t-field="answer.content" class="oe_no_empty"/></div>
|
||||
<div class="clearfix" t-if="answer.create_uid.karma < forum.karma_dofollow"><span t-field="answer.content" class="oe_no_empty" t-field-options='{"nofollow": 1}'/></div>
|
||||
<div class="clearfix"><span t-field="answer.content" class="oe_no_empty"/></div>
|
||||
<div class="clearfix">
|
||||
<ul class="list-inline pull-right mb0">
|
||||
<li>
|
||||
@@ -818,8 +816,7 @@
|
||||
<t t-set="classes" t-value="'close comment_delete fa-times'"/>
|
||||
</t>
|
||||
|
||||
<div t-if="message.create_uid.karma >= forum.karma_dofollow"><span t-field="message.body" class="oe_no_empty"/></div>
|
||||
<div t-if="message.create_uid.karma < forum.karma_dofollow"><span t-field="message.body" class="oe_no_empty" t-field-options='{"nofollow": 1}'/></div>
|
||||
<div><span t-field="message.body" class="oe_no_empty"/></div>
|
||||
<t t-set="required_karma" t-value="message.author_id.id == user.partner_id.id and object.forum_id.karma_comment_convert_own or object.forum_id.karma_comment_convert_all"/>
|
||||
<t t-if="(object.parent_id and object.parent_id.state != 'close' and object.parent_id.active != False) or (not object.parent_id and object.state != 'close' and object.active != False)">
|
||||
<t t-set="allow_post_comment" t-value="True" />
|
||||
@@ -1107,8 +1104,7 @@
|
||||
</tr>
|
||||
</table>
|
||||
<div class="well well-sm">
|
||||
<div t-if="user.karma >= forum.karma_dofollow"><span t-field="user.partner_id.website_description" class="oe_no_empty"/></div>
|
||||
<div t-if="user.karma < forum.karma_dofollow"><span t-field="user.partner_id.website_description" class="oe_no_empty" t-field-options='{"nofollow": 1}'/></div>
|
||||
<div><span t-field="user.partner_id.website_description" class="oe_no_empty"/></div>
|
||||
<t t-if="uid == user.id">
|
||||
<a class="fa fa-arrow-right" t-attf-href="/forum/#{slug(forum)}/user/#{slug(user)}/edit"> Edit Your Bio</a>
|
||||
</t>
|
||||
|
||||
Reference in New Issue
Block a user