Create Blog-Comments section (Admin)

Source: Internet
Author: User
Tags comments create blog

Previously we defined several role users, each with different permissions, one of which is permission.moderate_comments, and users with this permission can manage comments from other users

In order to facilitate the management of comments, we want to add a link in the navigation bar, the user has permission to see, this link in the base.html template using conditional statements added, as follows:

# app/templates/base.html

    #

    ... {if Current_user.can (permission.moderate_comments)%}
    <li><a href= "{{url_for (' Main.moderate ')}}" >moderate comments</a></li>
    {% endif%}

The Administration page displays comments for all articles in the same list, the latest published comments are displayed in front of each comment, and a button is displayed below each review to toggle the value of the Disabled property, and the/moderate route is defined as follows:

# app/main/views.py

#

... @main. Route ('/moderate ')
@login_required
@permission_required (permission.moderate_comments)
def Moderate ():
    page = request.args.get (' page ', 1, type=int)
    pagination = Comment.query.order_by ( Comment.timestamp.desc ()). Paginate (
        page, per_page=current_app.config[' Flasky_comments_per_page '),
        Error_out=false)
    comments = Pagination.items
    return render_template (' moderate.html ', comments=comments,
                            pagination=pagination, Page=page)

This is a simple function that reads a page of comments from the database, renders it into a template, and, in addition to the list of comments, passes the paging object and the current page to the template

Moderate.html template is also not difficult, because it relies on previously created sub-template _comments.html render comments

# app/templates/moderate.html

{% extends "base.html"%}
{% import "_macros.html" as macros%}

{% block title% }flasky-comment moderation{% endblock%}

{% block page_content%}
<div class= "Page-header" >

This template will render the work of commenting to the _comments.html template, but before handing control over to the subordinate template, a template variable moderate is defined with the set directive provided by JINJA2 and set to True, which is used in _ Comments.html, decide whether to render comment management function

In the _comments.html template, the part of the body of the comment is to be modified in two ways, for a normal user, (without setting the moderate variable), not to show comments marked as problematic, for wardens (moderate set to true), regardless of whether the comment is marked as problematic, to display , and a button to toggle the state is displayed below the text, with the following code:

# app/templates/_comments.html # ... <div class= "comment-body" > {% if comme Nt.disabled%} <p><i>this comment have been disabled by a moderator</i></p> {% endif%} {% if moderate or not comment.disbled%} {% if comment.body_html%} {{Comme nt.body_html |
            Safe}} {% Else%} {{Comment.body}} {% endif%} {% ENDIF%} </div> {% If moderate%} <br> {% if comment.disabled%} <a class= "Btn b Tn-default Btn-xs "href=" {{url_for ('. Moderate_enable ', Id=comment.id, Page=page)}} ">Enable</a> {% Else%
            } <a class= "btn btn-danger btn-xs" href= "{{url_for ('. moderate_disbled ', Id=comment.id, Page=page)}}" > disable</a> {% endif%} {% endif%} </div> 

After making the above changes, the user will see a short hint about the problematic comments, the wardens can see this prompt, but also can see the text of the comment, under each comment, the wardens can also see a button to switch the status of comments, click the button will trigger two new routes in one, But the specific trigger which depends on the wardens to set the comment to why state, two new routes are as follows:

# app/main/views.py

@main. Route ('/moderate/enable/<int:id> ')
@login_required
@permission_ Required (psermission.moderate_comments)
def moderate_enable (id):
    comment = Comment.query.get_or_404 (id)
    comment.disabled = False
    db.session.add (comment)
    return redirect (Url_for ('. Moderate ', poage= Request.args.get (' page ', 1, type=int))

@main. Route ('/moderate/disable/<int:id> ')
@login_required
@permission_required (permission.moderate_comments)
def moderate_disabled (id):
    comment = Comment.query.get_or_404 (ID)
    comment.disabled = True
    db.session.add (Comment)
    return Redirect (Url_ For ('. Moderate ', page=request.args.get (' page ', 1, type=int)))

The above-enabled routing and disabling routing first load the comment object, set the Disabled field to the correct value, then write the comment object to the database, and finally, redirect to the Comment management page, if the query string specified in the page parameter, it will be passed to the redirect operation, _ The button in the comments.html template specifies the page parameter, and redirects back to the previous pages

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.