Skip to content

Commit

Permalink
Merge pull request #209 from paramsingh/comment-views
Browse files Browse the repository at this point in the history
CB-295: Show comments on reviews and add ability to add comments.
  • Loading branch information
paramsingh committed Jun 16, 2018
2 parents 0a6e01d + e57c8bc commit 2e10da1
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 3 deletions.
2 changes: 2 additions & 0 deletions critiquebrainz/frontend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def create_app(debug=None, config_path=None):
from critiquebrainz.frontend.views.reports import reports_bp
from critiquebrainz.frontend.views.moderators import moderators_bp
from critiquebrainz.frontend.views.log import log_bp
from critiquebrainz.frontend.views.comment import comment_bp

app.register_blueprint(frontend_bp)
app.register_blueprint(review_bp, url_prefix='/review')
Expand All @@ -141,6 +142,7 @@ def create_app(debug=None, config_path=None):
app.register_blueprint(reports_bp, url_prefix='/reports')
app.register_blueprint(log_bp, url_prefix='/log')
app.register_blueprint(moderators_bp, url_prefix='/moderators')
app.register_blueprint(comment_bp, url_prefix='/comments')

return app

Expand Down
14 changes: 14 additions & 0 deletions critiquebrainz/frontend/forms/comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from flask_babel import lazy_gettext
from flask_wtf import Form
from wtforms import TextAreaField, StringField
from wtforms.widgets import HiddenInput


class CommentEditForm(Form):
state = StringField(widget=HiddenInput(), default='publish')
text = TextAreaField(lazy_gettext("Add a comment..."))
review_id = StringField(widget=HiddenInput())

def __init__(self, review_id=None, **kwargs):
kwargs['review_id'] = review_id
Form.__init__(self, **kwargs)
6 changes: 6 additions & 0 deletions critiquebrainz/frontend/templates/macros.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,9 @@
<i class="glyphicon glyphicon-star"></i>
<span style="font-size: 16px;">{{ rating }}</span><em class="text-muted">/5 {{ _('based on %(number)s ratings', number=count) }}</em>
{% endmacro %}

{% macro comment_credit(comment, user_picture_size) %}
{% set user_string = '<img class="avatar" src="%s&s=%d" /> '|safe % (review.user.avatar, user_picture_size)
+ '<a href="%s">%s</a>'|safe % (url_for('user.reviews', user_id=review.user.id), review.user.display_name) %}
{{ _('%(user)s', user=user_string) }}
{% endmacro %}
46 changes: 44 additions & 2 deletions critiquebrainz/frontend/templates/review/entity/base.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends 'base.html' %}
{% from 'macros.html' import review_credit, cover_art, show_avg_rating with context %}
{% from 'macros.html' import review_credit, cover_art, show_avg_rating, comment_credit with context %}

{% block content %}
<div id="review-entity">
Expand Down Expand Up @@ -44,7 +44,7 @@ <h3>{{ review_credit(review, user_picture_size=24) }}</h3>
<p style="word-wrap: break-word; white-space: pre-wrap;">{{ review.text_html|safe }}</p>
{% endif %}
{% endif %}

<hr />

{% if not review.is_draft %}
Expand Down Expand Up @@ -141,6 +141,35 @@ <h3>
</div>
{% endif %}

{% if comments %}
<hr/>
<h3> Comments ({{ comment_count }})</h3>
<div id="comment-list">
{% for comment in comments %}
<div id="{{ comment.id }}">
<hr/>
<p>
{{ comment_credit(comment, user_picture_size=24) }}
<span style="float:right;">
<small><em class="text-muted"> {{comment.created | date}} </em></small>
</span>
</p>
<p> {{ comment.text_html|safe }} </p>
</div>
{% endfor %}
<hr/>
</div>
{% endif %}

{% if current_user.is_authenticated %}
<form id="comment-editor" method="POST" class="form-horizontal" role="form" action="{{ url_for('comment.create') }}">
{{ comment_form.hidden_tag() }}
{{ comment_form.text(id="comment-text") }}
</form>
<hr/>
<button id="btn-comment-publish" class="btn btn-primary">{{ _('Add comment') }}</button>
{% endif %}

</div>
</div>
</div>
Expand All @@ -149,4 +178,17 @@ <h3>
{% block scripts %}
{{ super() }}
<script src="{{ get_static_path('rating.js') }}"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css"/>
<script src="{{ get_static_path('wysiwyg-editor.js') }}"></script>
<script>
$(document).ready(function() {
reviewContentEditor = new SimpleMDE({
element: $('#comment-text')[0],
spellChecker: false
});
});
$("#btn-comment-publish").click(function(){
$("#comment-editor").submit();
});
</script>
{% endblock %}
40 changes: 40 additions & 0 deletions critiquebrainz/frontend/views/comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# critiquebrainz - Repository for Creative Commons licensed reviews
#
# Copyright (C) 2018 MetaBrainz Foundation Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from flask import Blueprint, redirect, url_for
from flask_login import current_user, login_required
from critiquebrainz.frontend import flash
from critiquebrainz.frontend.forms.comment import CommentEditForm

import critiquebrainz.db.comment as db_comment

comment_bp = Blueprint('comment', __name__)


@comment_bp.route('/create', methods=['POST'])
@login_required
def create():
form = CommentEditForm()
if form.validate_on_submit():
comment = db_comment.create(
review_id=form.review_id.data,
user_id=current_user.id,
text=form.text.data,
)
flash.success('Comment has been saved!')
return redirect(url_for('review.entity', id=comment['review_id']))
10 changes: 9 additions & 1 deletion critiquebrainz/frontend/views/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
from critiquebrainz.frontend.external import mbspotify, soundcloud
from critiquebrainz.frontend.forms.log import AdminActionForm
from critiquebrainz.frontend.forms.review import ReviewCreateForm, ReviewEditForm, ReviewReportForm
from critiquebrainz.frontend.forms.comment import CommentEditForm
from critiquebrainz.frontend.login import admin_view
from critiquebrainz.frontend.views import get_avg_rating
from critiquebrainz.utils import side_by_side_diff
import critiquebrainz.db.spam_report as db_spam_report
import critiquebrainz.db.review as db_review
import critiquebrainz.db.moderation_log as db_moderation_log
import critiquebrainz.db.users as db_users
import critiquebrainz.db.comment as db_comment
from critiquebrainz.frontend.external.musicbrainz_db.entities import get_multiple_entities, get_entity_by_id


Expand Down Expand Up @@ -112,9 +114,15 @@ def entity(id, rev=None):
)
other_reviews = user_all_reviews[:3]
avg_rating = get_avg_rating(review["entity_id"], review["entity_type"])

comments, count = db_comment.list_comments(review_id=id)
for comment in comments:
comment["text_html"] = markdown(comment["last_revision"]["text"], safe_mode="escape")
comment_form = CommentEditForm(review_id=id)
return render_template('review/entity/%s.html' % review["entity_type"], review=review,
spotify_mappings=spotify_mappings, soundcloud_url=soundcloud_url,
vote=vote, other_reviews=other_reviews, avg_rating=avg_rating)
vote=vote, other_reviews=other_reviews, avg_rating=avg_rating,
comment_count=count, comments=comments, comment_form=comment_form)


@review_bp.route('/<uuid:review_id>/revision/<int:revision_id>')
Expand Down
63 changes: 63 additions & 0 deletions critiquebrainz/frontend/views/test/test_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# critiquebrainz - Repository for Creative Commons licensed reviews
#
# Copyright (C) 2018 MetaBrainz Foundation Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import critiquebrainz.db.comment as db_comment
import critiquebrainz.db.license as db_license
import critiquebrainz.db.review as db_review
import critiquebrainz.db.users as db_users

from critiquebrainz.db.user import User
from critiquebrainz.frontend.testing import FrontendTestCase


class CommentViewsTestCase(FrontendTestCase):

def setUp(self):
super(CommentViewsTestCase, self).setUp()
self.user = User(db_users.get_or_create("aef06569-098f-4218-a577-b413944d9493", new_user_data={
"display_name": u"Tester",
}))
self.license = db_license.create(
id="CC BY-SA 3.0",
full_name="Created so we can fill the form correctly.",
)
self.review = db_review.create(
user_id=self.user.id,
entity_id="e7aad618-fa86-3983-9e77-405e21796eca",
entity_type="release_group",
text="Testing",
rating=5,
is_draft=False,
license_id=self.license["id"],
)

def test_create(self):
self.temporary_login(self.user)
payload = {
'review_id': self.review['id'],
'text': 'Hello, this is a comment!',
'state': 'publish',
}
r = self.client.post(
'/comments/create',
data=payload,
)

self.assertRedirects(r, '/review/%s' % self.review['id'])
count = db_comment.count_comments(review_id=self.review['id'])
self.assertEqual(count, 1)

0 comments on commit 2e10da1

Please sign in to comment.