From d4db21a9cc89bc09acb64f8743bf12df910abc96 Mon Sep 17 00:00:00 2001 From: David Beguin Date: Fri, 8 Mar 2019 14:41:55 +0000 Subject: [PATCH] [IMP] website_slides : allow review, comment and vote only if enough karma To avoid eLearning to be spammed, the comment, review and vote behaviours are now allowed only if the user has enough karma to do it. Here is the new behaviour on courses and slides rating / comment / vote -If allow_comment is checked on Course : - Review (rating) is allowed on Course only if enough karma - Comment is allowed on slides within the course only if enough karma and course type is 'training' - Vote is allowed on slides within the course only if enough karma and course type is 'documentation' -If allow_comment is not checked on Course : - Review (rating) is not allowed on Course - Comment is not allowed on slides within the course - Vote is not allowed on slides within the course - Rating is not allowed on slides within the course anymore Task ID : 1943788 PR #31321 --- addons/website_forum/models/forum.py | 4 +-- .../static/src/js/website_mail.js | 2 +- .../views/website_rating_templates.xml | 3 +- addons/website_slides/controllers/main.py | 11 +++++-- addons/website_slides/models/slide_channel.py | 16 ++++++++-- addons/website_slides/models/slide_slide.py | 18 ++++++++++++ .../static/src/js/slides_slide_like.js | 4 +++ .../views/slide_channel_views.xml | 14 +++++++++ .../views/website_slides_templates_course.xml | 3 +- .../views/website_slides_templates_lesson.xml | 29 ++++++++++--------- 10 files changed, 81 insertions(+), 23 deletions(-) diff --git a/addons/website_forum/models/forum.py b/addons/website_forum/models/forum.py index 4dc172a90a0..03b45d6bf17 100644 --- a/addons/website_forum/models/forum.py +++ b/addons/website_forum/models/forum.py @@ -580,7 +580,7 @@ class Post(models.Model): @api.one def refuse(self): if not self.can_moderate: - raise KarmaError('Not enough karma to refuse a post') + raise KarmaError(_('Not enough karma to refuse a post')) self.moderator_id = self.env.user return True @@ -799,7 +799,7 @@ class Post(models.Model): self.ensure_one() if not self.can_comment: - raise KarmaError('Not enough karma to comment') + raise KarmaError(_('Not enough karma to comment')) if not kwargs.get('record_name') and self.parent_id: kwargs['record_name'] = self.parent_id.name return super(Post, self).message_post(message_type=message_type, **kwargs) diff --git a/addons/website_rating/static/src/js/website_mail.js b/addons/website_rating/static/src/js/website_mail.js index 6fd9a305e95..832a2d60e00 100644 --- a/addons/website_rating/static/src/js/website_mail.js +++ b/addons/website_rating/static/src/js/website_mail.js @@ -301,7 +301,7 @@ odoo.define('website_rating.thread', function(require) { 'res_model': false, 'res_id': false, 'pid': 0, - 'display_composer': !session.is_website_user, + 'display_composer': options['disable_composer'] ? false : !session.is_website_user, 'display_rating': true, 'csrf_token': odoo.csrf_token, 'user_id': session.user_id, diff --git a/addons/website_rating/views/website_rating_templates.xml b/addons/website_rating/views/website_rating_templates.xml index 8ce20495b32..c5893a7944e 100644 --- a/addons/website_rating/views/website_rating_templates.xml +++ b/addons/website_rating/views/website_rating_templates.xml @@ -61,7 +61,8 @@ t-att-data-default_message="default_message" t-att-data-default_message_id="default_message_id" t-att-data-default_rating_value="default_rating_value" - t-att-data-force_submit_url="force_submit_url"> + t-att-data-force_submit_url="force_submit_url" + t-att-data-disable_composer="disable_composer"> diff --git a/addons/website_slides/controllers/main.py b/addons/website_slides/controllers/main.py index c876881497b..83934343402 100644 --- a/addons/website_slides/controllers/main.py +++ b/addons/website_slides/controllers/main.py @@ -382,12 +382,15 @@ class WebsiteSlides(WebsiteProfile): ], order='write_date DESC', limit=1).read(['body', 'rating_value']) last_message_data = last_message_values[0] if last_message_values else {} values.update({ - 'message_post_hash': channel._sign_token(request.env.user.partner_id.id), - 'message_post_pid': request.env.user.partner_id.id, 'last_message_id': last_message_data.get('id'), 'last_message': tools.html2plaintext(last_message_data.get('body', '')), 'last_rating_value': last_message_data.get('rating_value'), }) + if channel.can_review: + values.update({ + 'message_post_hash': channel._sign_token(request.env.user.partner_id.id), + 'message_post_pid': request.env.user.partner_id.id, + }) # fetch slides and handle uncategorized slides; done as sudo because we want to display all # of them but unreachable ones won't be clickable (+ slide controller will crash anyway) @@ -574,6 +577,10 @@ class WebsiteSlides(WebsiteProfile): if (upvote and slide_partners.vote == 1) or (not upvote and slide_partners.vote == -1): return {'error': 'vote_done'} slide = request.env['slide.slide'].browse(int(slide_id)) + if not slide.channel_id.allow_comment: + return {'error': 'comment_disabled'} + if not slide.can_vote: + return {'error': 'missing_karma'} if upvote: slide.action_like() else: diff --git a/addons/website_slides/models/slide_channel.py b/addons/website_slides/models/slide_channel.py index 941477e69b2..cd7565bd41a 100644 --- a/addons/website_slides/models/slide_channel.py +++ b/addons/website_slides/models/slide_channel.py @@ -6,6 +6,7 @@ import uuid from odoo import api, fields, models, tools, _ from odoo.addons.http_routing.models.ir_http import slug +from odoo.addons.gamification.models.gamification_karma_rank import KarmaError from odoo.exceptions import UserError from odoo.osv import expression @@ -114,7 +115,7 @@ class Channel(models.Model): total_time = fields.Float('# Hours', compute='_compute_slides_statistics', digits=(10, 4), store=True) # configuration allow_comment = fields.Boolean( - "Allow comment on Content", default=False, + "Allow rating on Course", default=False, help="If checked it allows members to either:\n" " * like content and post comments on documentation course;\n" " * post comment and review on training course;") @@ -155,7 +156,11 @@ class Channel(models.Model): karma_gen_slide_vote = fields.Integer(string='Lesson voted', default=1) karma_gen_channel_rank = fields.Integer(string='Course ranked', default=5) karma_gen_channel_finish = fields.Integer(string='Course finished', default=10) - # TODO DBE : Add karma based action rules (like in forum) + # Karma based actions + karma_review = fields.Integer('Add a review', default=10, help="Karma needed to add a review on the course") + karma_slide_comment = fields.Integer('Add a comment', default=3, help="Karma needed to add a comment on a slide of this course") + karma_slide_vote = fields.Integer('Vote on slide', default=3, help="Karma needed to like/dislike a slide of this course.") + can_review = fields.Boolean('Can Review', compute='_compute_karma_rights') @api.depends('slide_ids.is_published') def _compute_slide_last_update(self): @@ -263,6 +268,11 @@ class Channel(models.Model): if channel.id: # avoid to perform a slug on a not yet saved record in case of an onchange. channel.website_url = '%s/slides/%s' % (base_url, slug(channel)) + @api.multi + def _compute_karma_rights(self): + for channel in self: + channel.can_review = self.env.user.karma >= channel.karma_review + # --------------------------------------------------------- # ORM Overrides # --------------------------------------------------------- @@ -318,6 +328,8 @@ class Channel(models.Model): through the 'Presentation Published' email, it should be considered as a note as we don't want all channel followers to be notified of this answer. """ self.ensure_one() + if kwargs.get('message_type') == 'comment' and not self.can_review: + raise KarmaError(_('Not enough karma to review')) if parent_id: parent_message = self.env['mail.message'].sudo().browse(parent_id) if parent_message.subtype_id and parent_message.subtype_id == self.env.ref('website_slides.mt_channel_slide_published'): diff --git a/addons/website_slides/models/slide_slide.py b/addons/website_slides/models/slide_slide.py index 2b4a13eefc1..a4a96001673 100644 --- a/addons/website_slides/models/slide_slide.py +++ b/addons/website_slides/models/slide_slide.py @@ -13,6 +13,7 @@ from werkzeug import urls from odoo import api, fields, models, _ from odoo.addons.http_routing.models.ir_http import slug +from odoo.addons.gamification.models.gamification_karma_rank import KarmaError from odoo.exceptions import Warning, UserError from odoo.http import request from odoo.addons.http_routing.models.ir_http import url_for @@ -155,6 +156,9 @@ class Slide(models.Model): slide_views = fields.Integer('# of Website Views', store=True, compute="_compute_slide_views") public_views = fields.Integer('# of Public Views') total_views = fields.Integer("Total # Views", default="0", compute='_compute_total', store=True) + # Karma Based action + can_comment = fields.Boolean('Can Comment', compute='_compute_karma_rights') + can_vote = fields.Boolean('Can Vote', compute='_compute_karma_rights') _sql_constraints = [ ('exclusion_html_content_and_url', "CHECK(html_content IS NULL OR url IS NULL)", "A slide is either filled with a document url or HTML content. Not both.") @@ -267,6 +271,12 @@ class Slide(models.Model): def _get_can_publish_error_message(self): return _("Publishing is restricted to the responsible of training courses or members of the publisher group for documentation courses") + @api.multi + def _compute_karma_rights(self): + for slide in self: + slide.can_comment = self.env.user.karma >= slide.channel_id.karma_slide_comment + slide.can_vote = self.env.user.karma >= slide.channel_id.karma_slide_vote + # --------------------------------------------------------- # ORM Overrides # --------------------------------------------------------- @@ -313,6 +323,14 @@ class Slide(models.Model): # Mail/Rating # --------------------------------------------------------- + @api.multi + @api.returns('mail.message', lambda value: value.id) + def message_post(self, message_type='notification', **kwargs): + self.ensure_one() + if message_type == 'comment' and not self.can_comment: # user comments have a restriction on karma + raise KarmaError(_('Not enough karma to comment')) + return super(Slide, self).message_post(message_type=message_type, **kwargs) + @api.multi def get_access_action(self, access_uid=None): """ Instead of the classic form view, redirect to website if it is published. """ diff --git a/addons/website_slides/static/src/js/slides_slide_like.js b/addons/website_slides/static/src/js/slides_slide_like.js index d5afc5c4409..bb0542a5f7e 100644 --- a/addons/website_slides/static/src/js/slides_slide_like.js +++ b/addons/website_slides/static/src/js/slides_slide_like.js @@ -59,6 +59,10 @@ var SlideLikeWidget = Widget.extend({ self._popoverAlert(self.$el, _.str.sprintf(_t('Please login to vote this slide'), (document.URL))); } else if (data.error === 'vote_done') { self._popoverAlert(self.$el, _t('You have already voted for this slide')); + } else if (data.error === 'comment_disabled') { + self._popoverAlert(self.$el, _t('Votes and comments are disabled for this channel')); + } else if (data.error === 'missing_karma') { + self._popoverAlert(self.$el, _t('You don\'t have enough karma to vote')); } else { self._popoverAlert(self.$el, _t('Unknown error')); } diff --git a/addons/website_slides/views/slide_channel_views.xml b/addons/website_slides/views/slide_channel_views.xml index e85097bdd8b..541492f5ff3 100644 --- a/addons/website_slides/views/slide_channel_views.xml +++ b/addons/website_slides/views/slide_channel_views.xml @@ -111,6 +111,20 @@ + + + + + + + + + + + + + + diff --git a/addons/website_slides/views/website_slides_templates_course.xml b/addons/website_slides/views/website_slides_templates_course.xml index ccc215ebf4f..e3a6583ca8b 100644 --- a/addons/website_slides/views/website_slides_templates_course.xml +++ b/addons/website_slides/views/website_slides_templates_course.xml @@ -104,6 +104,7 @@ + @@ -115,7 +116,7 @@ Course - diff --git a/addons/website_slides/views/website_slides_templates_lesson.xml b/addons/website_slides/views/website_slides_templates_lesson.xml index 225d3ecdc48..c589092aaa4 100644 --- a/addons/website_slides/views/website_slides_templates_lesson.xml +++ b/addons/website_slides/views/website_slides_templates_lesson.xml @@ -237,19 +237,21 @@ - Rating -
-
- - - - - - - - + + Rating +
+
+ + + + + + + + +
-
+
@@ -287,8 +289,7 @@
- - +