Просмотр исходного кода

extract update manage table data feature from home route to utils route

weichen 4 лет назад
Родитель
Сommit
4b9d1afdb6
2 измененных файлов с 22 добавлено и 17 удалено
  1. 3 17
      backstage/home/routes.py
  2. 19 0
      backstage/utils/routes.py

+ 3 - 17
backstage/home/routes.py

@@ -1,6 +1,6 @@
-from flask import render_template, Blueprint, request, redirect, url_for, flash
+from flask import render_template, Blueprint, request, redirect, url_for
 import requests
-from collections import defaultdict
+from backstage.utils.routes import update_manage_table
 
 home_app = Blueprint('home', __name__)
 
@@ -16,19 +16,5 @@ def home():
 
 @home_app.route('/backstage/home/update', methods=['POST'])
 def update():
-    update_data = defaultdict(list)
-    for key, value in request.form.items():
-        element_type = key.split('-')[0]
-        if element_type == 'img':
-            value = eval(value)
-        else:
-            value = str(value)
-        update_data[element_type].append(value)
-    response = requests.post(
-        'http://127.0.0.1:5000/api/gen/home?section={}'.format(
-            request.args.get('section_class')), json=update_data)
-    if response.status_code == 200:
-        flash('修改成功', 'success')
-    else:
-        flash('修改失敗', 'danger')
+    update_manage_table(request.form, request.args)
     return redirect(url_for('home.home'))

+ 19 - 0
backstage/utils/routes.py

@@ -2,6 +2,7 @@ from flask import flash, request, redirect, url_for
 import requests
 from time import sleep
 from backstage.utils import translate
+from collections import defaultdict
 
 TYPE_URL_FOR = {'collection': 'collections.collection_list',
                 'blog': 'blogs.blog_list'}
@@ -32,3 +33,21 @@ def remove_content():
 
 def get_trans_title_url_name(title):
     return translate(title).replace(' ', '_')
+
+
+def update_manage_table(forms, req_args):
+    update_data = defaultdict(list)
+    for key, value in forms.items():
+        element_type = key.split('-')[0]
+        if element_type == 'img':
+            value = eval(value)
+        else:
+            value = str(value)
+        update_data[element_type].append(value)
+    response = requests.post(
+        'http://127.0.0.1:5000/api/manages/data?page={}&section={}'.format(
+            req_args.get('page'), req_args.get('section_class')), json=update_data)
+    if response.status_code == 200:
+        flash('修改成功', 'success')
+    else:
+        flash('修改失敗', 'danger')