from flask import render_template, Blueprint, request, redirect, url_for
import requests
from backstage.utils.routes import update_manage_table
from backstage.config import PORTAL_SERVER, BHOUSE_SERVER

home_app = Blueprint('home', __name__)


@home_app.route('/backstage/home')
def home():
    response = requests.get('{}manages/data?page=home'.format(PORTAL_SERVER))
    if response.status_code == 200:
        return render_template('home.html',
                               title='首頁',
                               form_url='home.update',
                               page='home',
                               contents=response.json(),
                               bhouse_server=BHOUSE_SERVER)


@home_app.route('/backstage/home/update', methods=['POST'])
def update():
    update_manage_table(request.form.getlist('data'),
                        request.form.getlist('element'),
                        request.args,
                        request.files.getlist('image'),
                        'home3')
    return redirect(url_for('home.home'))
