|
@@ -3,6 +3,7 @@ import requests
|
|
|
from time import sleep
|
|
|
from backstage.utils import translate
|
|
|
from collections import defaultdict
|
|
|
+from os import path
|
|
|
|
|
|
TYPE_URL_FOR = {'collection': 'collections.collection_list',
|
|
|
'blog': 'blogs.blog_list'}
|
|
@@ -35,7 +36,11 @@ def get_trans_title_url_name(title):
|
|
|
return translate(title).replace(' ', '_')
|
|
|
|
|
|
|
|
|
-def update_manage_table(forms, req_args):
|
|
|
+def update_manage_table(forms, req_args, files, img_type):
|
|
|
+ def allow_file(filename):
|
|
|
+ return '.' in filename and \
|
|
|
+ filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
|
|
+ ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'webp'}
|
|
|
update_data = defaultdict(list)
|
|
|
for key, value in forms.items():
|
|
|
element_type = key.split('-')[0]
|
|
@@ -44,6 +49,18 @@ def update_manage_table(forms, req_args):
|
|
|
else:
|
|
|
value = str(value)
|
|
|
update_data[element_type].append(value)
|
|
|
+ for f_key, f_value in files.items():
|
|
|
+ if not f_value or f_value.filename == '':
|
|
|
+ continue
|
|
|
+ if not allow_file(f_value.filename):
|
|
|
+ flash('上傳圖片失敗,副檔名必須為:{}'.format(ALLOWED_EXTENSIONS), 'danger')
|
|
|
+ continue
|
|
|
+ requests.post(
|
|
|
+ 'http://127.0.0.1:5000/api/upload/static/img?type={}&filename={}'.format(
|
|
|
+ img_type, f_value.filename), files={'image': f_value})
|
|
|
+ img_index = int(f_key.split('-')[-1])-1
|
|
|
+ update_data['img'][img_index]['src'] = path.join(
|
|
|
+ path.split(update_data['img'][img_index]['src'])[0], f_value.filename)
|
|
|
response = requests.post(
|
|
|
'http://127.0.0.1:5000/api/manages/data?page={}§ion={}'.format(
|
|
|
req_args.get('page'), req_args.get('section_class')), json=update_data)
|