12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- from flask import render_template, Blueprint, request, redirect, url_for, jsonify
- import requests
- from wtforms.compat import iteritems
- 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
- import os
- 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:
- #sortedData = sorted(response.json(), key='date', reverse=True)
- #print(response.json())
- #aa = json.loads(response.text)
- #sortedData = sorted(response.json(), key=lambda x:x['date'], reverse=True)
- """ i=1
- for d in sortedData:
- if i == 1:
- print(d)
- for key, value in d.items():
- print(key+' '+value)
- i+=1 """
- #print(response.text)
- 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()
- form.image.data.filename = os.path.splitext(form.image.data.filename)[0].replace(".","") + os.path.splitext(form.image.data.filename)[1]
- eng_name = get_trans_title_url_name(form.title.data)
- 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(),
- 'true',
- 'collection',
- '/collection/{}'.format(eng_name),
- form.image.data.filename,
- form.description.data.replace('\r\n','<br>'))
- data = {'frontMatter': front_matter,
- 'name': eng_name,
- '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'))
|