routes.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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'), 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. tary = translate(title).replace('$','_').replace('&','_').replace('+','_').replace(',','_').replace('/','_').replace(':','_').replace(';','_').replace('=','_').replace('?','_').replace('@','_').replace(' ','_').split('_')
  34. while("" in tary) :
  35. tary.remove("")
  36. return ('_'.join(tary))
  37. #return translate(title).replace(' ', '_')
  38. def update_manage_table(data, elements, req_args, files, img_type):
  39. def allow_file(filename):
  40. return '.' in filename and \
  41. filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
  42. ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'webp'}
  43. update_data = defaultdict(list)
  44. for element, value in zip(elements, data):
  45. if element == 'img':
  46. value = eval(value)
  47. elif element == 'yt_video':
  48. value = {'videoid': str(value)}
  49. else:
  50. value = str(value)
  51. update_data[element].append(value)
  52. for idx, f_value in enumerate(files):
  53. if not f_value or f_value.filename == '':
  54. continue
  55. if not allow_file(f_value.filename):
  56. flash('上傳圖片失敗,副檔名必須為:{}'.format(ALLOWED_EXTENSIONS), 'danger')
  57. continue
  58. requests.post(
  59. '{}upload/static/img?type={}&filename={}'.format(
  60. PORTAL_SERVER, img_type, f_value.filename), files={'image': f_value})
  61. update_data['img'][idx]['src'] = path.join(
  62. path.split(update_data['img'][idx]['src'])[0], f_value.filename)
  63. response = requests.post(
  64. '{}manages/data?page={}&section={}'.format(
  65. PORTAL_SERVER, req_args.get('page'), req_args.get('section_class')), json=update_data)
  66. if response.status_code == 200:
  67. sleep(1) # waiting for API upload image successfully.
  68. flash('修改成功', 'success')
  69. else:
  70. flash('修改失敗', 'danger')