routes.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from flask import flash, request, redirect, url_for, Response,Blueprint
  2. from flask_restful import Resource, Api
  3. import requests
  4. from time import sleep
  5. from backstage.utils import translate
  6. from collections import defaultdict
  7. from os import path
  8. from backstage.config import PORTAL_SERVER
  9. contents_app = Blueprint('contents', __name__)
  10. api = Api(contents_app)
  11. TYPE_URL_FOR = {'collection': 'collections.collection_list',
  12. 'blog': 'blogs.blog_list'}
  13. def checktype(articletype):
  14. if articletype == 'blog':
  15. return 'blog'
  16. return articletype
  17. def create_content(data, image_data):
  18. new_response = requests.post('{}new_content'.format(PORTAL_SERVER), json=data)
  19. print("Request returned " + str(new_response.status_code))
  20. if new_response.status_code == 201:
  21. #flash('新增文章失敗', 'danger')
  22. return "<script>alert('已有重複的標題,請重新設定');history.go(-1);</script>"
  23. if new_response.status_code == 200:
  24. if not image_data == None:
  25. requests.post(
  26. '{}upload/static/img?type={}&filename={}'.format(
  27. PORTAL_SERVER, checktype(data.get('type')), image_data.filename), files={'image': image_data})
  28. sleep(1) # sleep for waiting for new_content API generating content successfully.
  29. return redirect(url_for('editor.editor', url='/{}/{}'.format(
  30. checktype(data.get('type')), get_trans_title_url_name(data.get('name')))))
  31. else:
  32. flash('新增文章失敗', 'danger')
  33. print(url_for(TYPE_URL_FOR.get(data.get('type'))))
  34. return redirect(url_for(TYPE_URL_FOR.get(data.get('type'))))
  35. def create_collection_content(data, img, coverimg, csliderimg, commentimg, ownerimg): #exclusive for collection
  36. new_response = requests.post('{}new_content'.format(PORTAL_SERVER), json=data)
  37. if new_response.status_code == 201:
  38. #flash('新增文章失敗', 'danger')
  39. return "<script>alert('已有重複的標題,請重新設定');history.go(-1);</script>"
  40. if new_response.status_code == 200:
  41. if not img == None:
  42. requests.post(
  43. '{}upload/static/img?type={}&filename={}'.format(
  44. PORTAL_SERVER, checktype(data.get('type')), img.filename), files={'image': img})
  45. requests.post(
  46. '{}upload/static/img?type={}&filename={}'.format(
  47. PORTAL_SERVER, checktype(data.get('type')), coverimg.filename), files={'image': coverimg})
  48. sleep(1) # sleep for waiting for new_content API generating content successfully.
  49. if not commentimg == None:
  50. requests.post(
  51. '{}upload/img_to_dir?url={}&filename={}'.format(
  52. PORTAL_SERVER, data.get('dest'), commentimg.filename), files={'image': commentimg})
  53. sleep(1)
  54. if not ownerimg == None:
  55. requests.post(
  56. '{}upload/img_to_dir?url={}&filename={}'.format(
  57. PORTAL_SERVER, data.get('dest'), ownerimg.filename), files={'image': ownerimg})
  58. sleep(1)
  59. # reserved for slider img wheel
  60. return redirect(url_for('editor.editor', url='/{}/{}'.format(
  61. checktype(data.get('type')), get_trans_title_url_name(data.get('name')))))
  62. else:
  63. flash('新增文章失敗', 'danger')
  64. return redirect(url_for(TYPE_URL_FOR.get(data.get('type'))))
  65. def upload_slider_img(data, image_data): #for uploading slider images, currently unused
  66. if (data.type == 'collection'):
  67. print("This is a collection article.") #debug use
  68. if not image_data == None: #just in case
  69. requests.post(
  70. '{}upload/static/img/?type={}&filename={}'.format(
  71. PORTAL_SERVER, checktype(data.get('type')), image_data.filename), files={'image': data.get('csliderimg')}, type='csliderimg', dest=data.get('dest'))
  72. sleep(1) # sleep for waiting for new_content API generating content successfully.
  73. def remove_content():
  74. url = request.args.get('url', type=str)
  75. response = requests.delete('{}contents?url={}'.format(PORTAL_SERVER, url))
  76. if response.status_code == 200:
  77. flash('刪除文章成功', 'success')
  78. else:
  79. flash('刪除文章失敗', 'danger')
  80. def get_trans_title_url_name(title):
  81. title = title.replace('「','').replace('」','').replace('%','_percent')
  82. tary = translate(title).replace('$','_').replace('&','_').replace('+','_').replace(',','_').replace('/','_').replace(':','_').replace(';','_').replace('=','_').replace('?','_').replace('@','_').replace(' ','_').replace("'",'_').replace('?','').replace('!','').split('_')
  83. while("" in tary) :
  84. tary.remove("")
  85. return ('_'.join(tary))
  86. #return translate(title).replace(' ', '_')
  87. def update_manage_table(data, elements, req_args, files, img_type):
  88. def allow_file(filename):
  89. return '.' in filename and \
  90. filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
  91. ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'webp'}
  92. update_data = defaultdict(list)
  93. for element, value in zip(elements, data):
  94. if element == 'img':
  95. value = eval(value)
  96. elif element == 'yt_video':
  97. value = {'videoid': str(value)}
  98. else:
  99. value = str(value)
  100. update_data[element].append(value)
  101. for idx, f_value in enumerate(files):
  102. if not f_value or f_value.filename == '':
  103. continue
  104. if not allow_file(f_value.filename):
  105. flash('上傳圖片失敗,副檔名必須為:{}'.format(ALLOWED_EXTENSIONS), 'danger')
  106. continue
  107. requests.post(
  108. '{}upload/static/img?type={}&filename={}'.format(
  109. PORTAL_SERVER, img_type, f_value.filename), files={'image': f_value})
  110. update_data['img'][idx]['src'] = path.join(
  111. path.split(update_data['img'][idx]['src'])[0], f_value.filename)
  112. response = requests.post(
  113. '{}manages/data?page={}&section={}'.format(
  114. PORTAL_SERVER, req_args.get('page'), req_args.get('section_class')), json=update_data)
  115. if response.status_code == 200:
  116. sleep(1) # waiting for API upload image successfully.
  117. flash('修改成功', 'success')
  118. else:
  119. flash('修改失敗', 'danger')