|
@@ -1,5 +1,9 @@
|
|
|
-from flask import render_template, Blueprint, request, redirect, url_for
|
|
|
+from flask import render_template, Blueprint, request, redirect, url_for, flash
|
|
|
import requests
|
|
|
+from backstage.collections.forms import CollectionCreateForm
|
|
|
+from datetime import datetime, timezone, timedelta
|
|
|
+from backstage.utils import translate
|
|
|
+from time import sleep
|
|
|
|
|
|
editor_app = Blueprint('editor', __name__)
|
|
|
|
|
@@ -15,3 +19,37 @@ def remove():
|
|
|
url = request.args.get('url', type=str)
|
|
|
response = requests.delete('http://127.0.0.1:5000/api/contents?url={}'.format(url))
|
|
|
return redirect(url_for('collections.collection_list'))
|
|
|
+
|
|
|
+
|
|
|
+@editor_app.route('/backstage/create/collection', methods=['POST'])
|
|
|
+def create_collection():
|
|
|
+ form = CollectionCreateForm()
|
|
|
+ url_name = translate(form.title.data).replace(' ', '_')
|
|
|
+ front_matter = '''---
|
|
|
+title: "{}"\n\
|
|
|
+date: {}\n\
|
|
|
+draft: {}\n\
|
|
|
+type: "{}"\n\
|
|
|
+url: "{}"\n\
|
|
|
+image: "/img/collection/{}"\n\
|
|
|
+description: "{}"\n\
|
|
|
+---'''.format(form.title.data,
|
|
|
+ datetime.now(timezone(timedelta(hours=+8))).isoformat(timespec="seconds"),
|
|
|
+ 'false',
|
|
|
+ 'collection',
|
|
|
+ '/collection/{}'.format(url_name),
|
|
|
+ form.image.data.filename,
|
|
|
+ form.description.data)
|
|
|
+ data = {'frontMatter': front_matter,
|
|
|
+ 'name': request.form['title'],
|
|
|
+ 'type': 'collection'}
|
|
|
+ new_response = requests.post('http://127.0.0.1:5000/api/new_content', json=data)
|
|
|
+ if new_response.status_code == 200:
|
|
|
+ requests.post(
|
|
|
+ 'http://127.0.0.1:5000/api/upload/static/img?type=collection&filename={}'.format(
|
|
|
+ form.image.data.filename), files={'image': form.image.data})
|
|
|
+ sleep(0.5)
|
|
|
+ return redirect(url_for('editor.editor', url='/collection/{}'.format(url_name)))
|
|
|
+ else:
|
|
|
+ flash('新增文章失敗', 'danger')
|
|
|
+ return redirect(url_for('collections.collection_list'))
|