Exemple #1
0
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']))
def entity(id, rev=None):
    review = get_review_or_404(id)
    # Not showing review if it isn't published yet and not viewed by author.
    if review["is_draft"] and not (current_user.is_authenticated and
                                   current_user == review["user"]):
        raise NotFound(gettext("Can't find a review with the specified ID."))
    if review["is_hidden"]:
        if not current_user.is_admin():
            raise Forbidden(gettext("Review has been hidden. "
                                    "You need to be an administrator to view it."))
        flash.warn(gettext("Review has been hidden."))

    spotify_mappings = None
    soundcloud_url = None
    if review["entity_type"] == 'release_group':
        spotify_mappings = mbspotify.mappings(str(review["entity_id"]))
        soundcloud_url = soundcloud.get_url(str(review["entity_id"]))
    count = db_revision.get_count(id)
    if not rev:
        rev = count
    if rev < count:
        flash.info(gettext('You are viewing an old revision, the review has been updated since then.'))
    elif rev > count:
        raise NotFound(gettext("The revision you are looking for does not exist."))

    revision = db_revision.get(id, offset=count - rev)[0]
    if not review["is_draft"] and current_user.is_authenticated:
        # if user is logged in, get their vote for this review
        try:
            vote = db_vote.get(user_id=current_user.id, revision_id=revision['id'])
        except db_exceptions.NoDataFoundException:
            vote = None
    else:  # otherwise set vote to None, its value will not be used
        vote = None
    if revision["text"] is None:
        review["text_html"] = None
    else:
        review["text_html"] = markdown(revision['text'], safe_mode="escape")

    review["rating"] = revision["rating"]

    user_all_reviews, _ = db_review.list_reviews(
        user_id=review["user_id"],
        sort="random",
        exclude=[review["id"]],
    )
    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,
                           comment_count=count, comments=comments, comment_form=comment_form)
Exemple #3
0
def create():
    # TODO (code-master5): comment limit, revision and drafts, edit functionality
    form = CommentEditForm()
    if form.validate_on_submit():
        get_review_or_404(form.review_id.data)
        if current_user.is_blocked:
            flash.error(gettext("You are not allowed to write new comments because your "
                                "account has been blocked by a moderator."))
            return redirect(url_for('review.entity', id=form.review_id.data))
        # should be able to comment only if review exists
        db_comment.create(
            review_id=form.review_id.data,
            user_id=current_user.id,
            text=form.text.data,
        )
        flash.success(gettext("Comment has been saved!"))
    elif not form.text.data:
        # comment must have some text
        flash.error(gettext("Comment must not be empty!"))
    return redirect(url_for('review.entity', id=form.review_id.data))
def create():
    # TODO (code-master5): comment limit, revision and drafts, edit functionality
    form = CommentEditForm()
    if form.validate_on_submit():
        get_review_or_404(form.review_id.data)
        if current_user.is_blocked:
            flash.error(
                gettext(
                    "You are not allowed to write new comments because your "
                    "account has been blocked by a moderator."))
            return redirect(url_for('review.entity', id=form.review_id.data))
        # should be able to comment only if review exists
        db_comment.create(
            review_id=form.review_id.data,
            user_id=current_user.id,
            text=form.text.data,
        )
        flash.success(gettext("Comment has been saved!"))
    elif not form.text.data:
        # comment must have some text
        flash.error(gettext("Comment must not be empty!"))
    return redirect(url_for('review.entity', id=form.review_id.data))
def edit(id):
    comment = get_comment_or_404(id)
    if comment["user"] != current_user:
        raise Unauthorized(gettext("Only the author can edit this comment."))
    if current_user.is_blocked:
        flash.error(
            gettext("You are not allowed to edit comments because your "
                    "account has been blocked by a moderator."))
        return redirect(url_for('review.entity', id=comment["review_id"]))
    form = CommentEditForm()
    if form.validate_on_submit():
        if form.text.data != comment["last_revision"]["text"]:
            db_comment.update(comment_id=comment["id"], text=form.text.data)
            flash.success(gettext("Comment has been updated."))
        else:
            flash.error(
                gettext(
                    "You must change some content of the comment to update it!"
                ))
    elif not form.text.data:
        # comment must have some text
        flash.error(gettext("Comment must not be empty!"))
    return redirect(url_for('review.entity', id=comment["review_id"]))