from flask import render_template, Blueprint, request, redirect, url_for import requests from backstage.collections.forms import CollectionCreateForm from backstage.utils import get_now_time from backstage.utils.routes import create_content, remove_content, get_trans_title_url_name from backstage.config import PORTAL_SERVER collections_app = Blueprint('collections', __name__) @collections_app.route('/backstage/collections') def collection_list(): response = requests.get('{}contents?url=/collection'.format(PORTAL_SERVER)) if response.status_code == 200: return render_template('collections.html', title='家具規劃作品', legend='家具規劃作品列表', collections=response.json(), length=len(response.json()), form=CollectionCreateForm()) @collections_app.route('/backstage/collection/create', methods=['POST']) def create(): form = CollectionCreateForm() front_matter = '''--- title: "{}"\n\ date: {}\n\ draft: {}\n\ type: "{}"\n\ url: "{}"\n\ image: "/img/collection/{}"\n\ description: "{}"\n\ ---'''.format(form.title.data, get_now_time(), 'false', 'collection', '/collection/{}'.format(get_trans_title_url_name(form.title.data)), form.image.data.filename, form.description.data) data = {'frontMatter': front_matter, 'name': request.form['title'], 'type': 'collection'} return create_content(data, form.image.data) @collections_app.route('/backstage/collection/remove', methods=['POST']) def remove(): remove_content() return redirect(url_for('collections.collection_list'))