1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- 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'}
- def create_content(data, image_data):
- 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={}&filename={}'.format(
- data.get('type'), image_data.filename), files={'image': image_data})
- sleep(1) # sleep for waiting for new_content API generating content successfully.
- return redirect(url_for('editor.editor', url='/{}/{}'.format(
- data.get('type'), get_trans_title_url_name(data.get('name')))))
- else:
- flash('新增文章失敗', 'danger')
- return redirect(url_for(TYPE_URL_FOR.get(data.get('type'))))
- def remove_content():
- url = request.args.get('url', type=str)
- response = requests.delete('http://127.0.0.1:5000/api/contents?url={}'.format(url))
- if response.status_code == 200:
- flash('刪除文章成功', 'success')
- else:
- flash('刪除文章失敗', 'danger')
- 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={}§ion={}'.format(
- req_args.get('page'), req_args.get('section_class')), json=update_data)
- if response.status_code == 200:
- flash('修改成功', 'success')
- else:
- flash('修改失敗', 'danger')
|