# -*- coding: utf-8 -*-
from flask import (Blueprint,
                   redirect, render_template,
                   url_for, flash)
from fudcon.app import is_fudcon_admin, app
from fudcon.database import db
from fudcon.modules.contents.forms import AddPage
from fudcon.modules.contents.models import Content

bp = Blueprint('admin', __name__, url_prefix='/admin')

items_per_page = app.config['ITEMS_PER_PAGE']


@bp.route('/', methods=['GET', 'POST'])
@is_fudcon_admin
def index():
    """ Admin blueprint for this application
    """
    return render_template('backend/index.html',
                           title='Administration')


@bp.route('/pages', methods=['GET', 'POST'])
@bp.route('pages/<int:page>', methods=['GET', 'POST'])
@is_fudcon_admin
def pages(page=1):
    paginate_params = (page, items_per_page, False)
    queryset = Content.query.paginate(*paginate_params)
    return render_template('backend/pages.html',
                           title='List pages',
                           pages=queryset)


@bp.route('/pages/add', methods=['GET', 'POST'])
def add_page():
    """ Add page to the application
    """
    form = AddPage()
    action = url_for('admin.add_page')
    if form.validate_on_submit():
        content = Content(title=form.title.data,
                          description=form.description.data,
                          content_type=form.content_type.data,
                          is_on_user_menu=form.is_on_user_menu.data,
                          tag=form.tag.data,
                          active=form.active.data)
        db.session.add(content)
        db.session.commit()
        flash('Page created')
        return redirect(url_for('admin.page'))
    return render_template('backend/pages_actions.html',
                           form=form,
                           title='Add page',
                           action=action)


@bp.route('/pages/edit/<int:page_id>', methods=['GET', 'POST'])
@is_fudcon_admin
def edit_page(page_id):
    query_edit_page = Content.query.filter(Content.id ==
                                           page_id).first_or_404()
    form = AddPage(obj=query_edit_page)
    action = url_for('admin.edit_page', page_id=page_id)
    if form.validate_on_submit():
        form.populate_obj(query_edit_page)
        db.session.commit()
        flash('Page edited')
        return redirect(url_for('admin.page'))
    return render_template('backend/pages_actions',
                           form=form,
                           action=action)

@@ -126,16 +126,25 @@
 r, flash
+, request
 )%0Afrom f
@@ -1077,16 +1077,33 @@
 POST'%5D)%0A
+@is_fudcon_admin%0A
 def add_
@@ -2526,28 +2526,362 @@
              action=action)%0A
+%0A%0A@bp.route('/pages/delete/%3Cint:page_id%3E', methods=%5B'GET', 'POST'%5D)%0A@is_fudcon_admin%0Adef delete_page(page_id):%0A    query_delete_page = Content.query.filter(%0A        Content.id == page_id).first_or_404()%0A    db.session.delete(query_delete_page)%0A    db.session.commit()%0A    flash('Record deleted')%0A    return redirect(request.referrer)%0A

delete method for pages