routes.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from flask import flash, request, redirect, url_for, Response
  2. import requests
  3. from time import sleep
  4. from backstage.utils import translate
  5. from collections import defaultdict
  6. from os import path
  7. from backstage.config import PORTAL_SERVER
  8. TYPE_URL_FOR = {'collection': 'collections.collection_list',
  9. 'blog': 'blogs.blog_list'}
  10. def create_content(data, image_data):
  11. new_response = requests.post('{}new_content'.format(PORTAL_SERVER), json=data)
  12. if new_response.status_code == 201:
  13. flash('新增文章失敗', 'danger')
  14. return "<script>alert('已有重複的標題,請重新設定');history.go(-1);</script>"
  15. if new_response.status_code == 200:
  16. requests.post(
  17. '{}upload/static/img?type={}&filename={}'.format(
  18. PORTAL_SERVER, data.get('type'), get_trans_title_url_name(image_data.filename)), files={'image': image_data})
  19. sleep(1) # sleep for waiting for new_content API generating content successfully.
  20. return redirect(url_for('editor.editor', url='/{}/{}'.format(
  21. data.get('type'), get_trans_title_url_name(data.get('name')))))
  22. else:
  23. flash('新增文章失敗', 'danger')
  24. return redirect(url_for(TYPE_URL_FOR.get(data.get('type'))))
  25. def remove_content():
  26. url = request.args.get('url', type=str)
  27. response = requests.delete('{}contents?url={}'.format(PORTAL_SERVER, url))
  28. if response.status_code == 200:
  29. flash('刪除文章成功', 'success')
  30. else:
  31. flash('刪除文章失敗', 'danger')
  32. def get_trans_title_url_name(title):
  33. return translate(title).replace(' ', '_')
  34. #return translate(title).replace(' ', '_')
  35. def update_manage_table(data, elements, req_args, files, img_type):
  36. def allow_file(filename):
  37. return '.' in filename and \
  38. filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
  39. ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'webp'}
  40. update_data = defaultdict(list)
  41. for element, value in zip(elements, data):
  42. if element == 'img':
  43. value = eval(value)
  44. elif element == 'yt_video':
  45. value = {'videoid': str(value)}
  46. else:
  47. value = str(value)
  48. update_data[element].append(value)
  49. for idx, f_value in enumerate(files):
  50. if not f_value or f_value.filename == '':
  51. continue
  52. if not allow_file(f_value.filename):
  53. flash('上傳圖片失敗,副檔名必須為:{}'.format(ALLOWED_EXTENSIONS), 'danger')
  54. continue
  55. requests.post(
  56. '{}upload/static/img?type={}&filename={}'.format(
  57. PORTAL_SERVER, img_type, f_value.filename), files={'image': f_value})
  58. update_data['img'][idx]['src'] = path.join(
  59. path.split(update_data['img'][idx]['src'])[0], f_value.filename)
  60. response = requests.post(
  61. '{}manages/data?page={}&section={}'.format(
  62. PORTAL_SERVER, req_args.get('page'), req_args.get('section_class')), json=update_data)
  63. if response.status_code == 200:
  64. sleep(1) # waiting for API upload image successfully.
  65. flash('修改成功', 'success')
  66. else:
  67. flash('修改失敗', 'danger')