BBS Forum (23)

BBS Forum (23)

23. Add a section

(1) apps/models

class BoardModel(db.Model):
    __tablename__ ='board'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(20), nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.now)

(2) manage.py

from apps.models import BoardModel

Generate database tables

python manage.py db migrate

python manage.py db upgrade

(3) cms/forms.py

class AddBoardsForm(BaseForm):
    name=StringField(validators=[InputRequired(message='Please enter the section name'),Length(2,15,message='The length should be between 2-15 characters')])

class UpdateBoardForm(AddBoardsForm):
    board_id=IntegerField(validators=[InputRequired(message='Please enter the board name')])

(4) cms/views.py

@bp.route('/boards/')
@login_required
@permission_required(CMSPermission.BOARDER)
def boards():
    board_models=BoardModel.query.all()
    context={
        'boards':board_models
    }
    return render_template('cms/cms_boards.html',**context)

@bp.route('/aboards/',methods=['POST'])
@login_required
@permission_required(CMSPermission.BOARDER)
def aboards():
    form=AddBoardsForm(request.form)
    if form.validate():
        name=form.name.data
        board=BoardModel(name=name)
        db.session.add(board)
        db.session.commit()
        return restful.success()
    else:
        return restful.params_error(message=form.get_error())


@bp.route('/uboards/',methods=['POST'])
@login_required
@permission_required(CMSPermission.BOARDER)
def uboards():
    form=UpdateBoardForm(request.form)
    if form.validate():
        board_id=form.board_id.data
        name=form.name.data
        board=BoardModel.query.get(board_id)
        if board:
            board.name=name
            db.session.commit()
            return restful.success()
        else:
            return restful.params_error(message='There is no such section')
    else:
        return restful.params_error(message=form.get_error())


@bp.route('/dboards/',methods=['POST'])
@login_required
@permission_required(CMSPermission.BOARDER)
def dboards():
    board_id=request.form.get('board_id')
    if not board_id:
        return restful.params_error(message='Please pass in the section ID')
    board=BoardModel.query.get(board_id)
    if board:
        db.session.delete(board)
        db.session.commit()
        return restful.success()
    else:
        return restful.params_error(message='There is no such section')

(5) cms/js/boards.js

$(function () {
    $('#add_board_btn').on('click', function () {
        event.preventDefault();
        zlalert.alertOneInput({
            'title':'Add section',
            'text':'Please enter the section name',
            'placeholder':'section name',
            'confirmCallback': function (inputValue) {
                zlajax.post({
                    'url':'/cms/aboards/',
                    'data': {
                        'name': inputValue
                    },
                    'success': function (data) {
                        if (data['code'] == 200) {
                            win.loc.reload();
                        } else {
                            zlalert.alertInfo(data['message']);
                        }
                    }
                });

            }
        })
    });

});

(6) cms/cms_boards.html

{% extends'cms/cms_base.html' %}
{% from'common/_macros.html' import static %}
{% block title %}
    Sector management
{% endblock %}

{% block head %}
    <script src="{{ static('cms/js/boards.js') }}"></script>
{% endblock %}

{% block page_title %}
    {{ self.title() }}
{% endblock %}


{% block main_content %}
    <div class="top-box">
        <button class="btn btn-warning" id="add_board_btn" style="float: right">Add new board</button>
    </div>

    <table class="table table-bordered">
        <thead>
        <tr>
            <th>Section name</th>
            <th>Number of posts</th>
            <th>Created time</th>
            <th>Operation</th>
        </tr>
        </thead>

        <tbody>
        {% for board in boards %}
            <tr data-name="{{ board.name }}" data-id="{{ board.id }}">
                <td>{{ board.name }}</td>
                <td>0</td>
                <td>{{ board.create_time }}</td>
                <td>
                    <button class="btn btn-default edit-board-btn">Edit</button>
                    <button class="btn btn-danger delete-board-btn">Delete</button>
                </td>
            </tr>

        {% endfor %}
        </tbody>
    </table>

{% endblock %}
Reference: https://cloud.tencent.com/developer/article/1390490 BBS Forum (23)-Cloud + Community-Tencent Cloud