Kaynağa Gözat

extract create collection from editor route to collection route

weichen 4 yıl önce
ebeveyn
işleme
6daac274d2

+ 37 - 1
backstage/collections/routes.py

@@ -1,6 +1,8 @@
-from flask import render_template, Blueprint
+from flask import render_template, Blueprint, flash, request, redirect, url_for
 import requests
 from backstage.collections.forms import CollectionCreateForm
+from backstage.utils import translate, get_now_time
+from time import sleep
 
 
 collections_app = Blueprint('collections', __name__)
@@ -16,3 +18,37 @@ def collection_list():
                                collections=response.json(),
                                length=len(response.json()),
                                form=CollectionCreateForm())
+
+
+@collections_app.route('/backstage/collection/create', methods=['POST'])
+def create():
+    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,
+              get_now_time(),
+              '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'))

+ 1 - 39
backstage/editor/routes.py

@@ -1,9 +1,5 @@
-from flask import render_template, Blueprint, request, redirect, url_for, flash
+from flask import render_template, Blueprint, request, redirect, url_for
 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__)
 
@@ -19,37 +15,3 @@ 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'))

+ 1 - 1
backstage/templates/collections.html

@@ -39,7 +39,7 @@
       <div class="modal-header">
         <h5 class="modal-title" id="createModalLabel">Create new content</h5>
       </div>
-      <form action="{{ url_for('editor.create_collection') }}" method="POST" enctype="multipart/form-data">
+      <form action="{{ url_for('collections.create') }}" method="POST" enctype="multipart/form-data">
         <div class="form-group">
           {{ form.title.label(class="form-control-label") }}
           {{ form.title(class="form-control form-control-lg") }}