routes.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from flask import render_template, Blueprint, request, redirect, url_for
  2. from flask.wrappers import Response
  3. import requests
  4. import os
  5. import fnmatch
  6. from backstage.blogs.forms import BlogCreateForm
  7. from backstage.utils import get_now_time, translate
  8. from backstage.utils.routes import create_content, remove_content, get_trans_title_url_name
  9. from backstage.config import PORTAL_SERVER, UPLOAD_PATH_MAP
  10. blogs_app = Blueprint('blogs', __name__)
  11. @blogs_app.route('/backstage/blogs', methods=['GET'])
  12. def blog_list():
  13. response = requests.get('{}contents?url=/blog'.format(PORTAL_SERVER))
  14. if response.status_code == 200:
  15. sortedData = sorted(response.json(), key=lambda x: x['date'], reverse=True)
  16. return render_template('blogs.html',
  17. title='設計專欄',
  18. legend='設計專欄列表',
  19. blogs=sortedData,
  20. length=len(response.json()),
  21. form=BlogCreateForm())
  22. @blogs_app.route('/backstage/blog/create/', methods=['POST'])
  23. def create():
  24. form = BlogCreateForm()
  25. if form.categories.data == "居家美學":
  26. caturl = "home_aesthetics"
  27. elif form.categories.data == "規劃師QA":
  28. caturl = "room_planner_expertise"
  29. elif form.categories.data == "驗屋知識":
  30. caturl = "home_inspection_knowledge"
  31. else:
  32. caturl = get_trans_title_url_name(form.categories.data)
  33. front_matter = '''---
  34. title: "{}"\n\
  35. date: {}\n\
  36. draft: {}\n\
  37. type: "{}"\n\
  38. url: "{}"\n\
  39. image: "/img/blog/{}"\n\
  40. categories: ["{}"]\n\
  41. caturl: "{}"\n\
  42. ---'''.format(form.title.data,
  43. get_now_time(),
  44. 'false',
  45. 'blog',
  46. '/blog/{}'.format(get_trans_title_url_name(form.title.data)),
  47. form.image.data.filename,
  48. form.categories.data,
  49. caturl)
  50. data = {'frontMatter': front_matter,
  51. 'name': get_trans_title_url_name(request.form['title']),
  52. 'type': 'blog',
  53. 'categories': form.categories.data,
  54. 'caturl': caturl
  55. }
  56. return create_content(data, form.image.data)
  57. @blogs_app.route('/backstage/blog/createCat/', methods=['GET'])
  58. def createCat():
  59. #title = ""
  60. front_matter = '''---
  61. title: "{}"\n\
  62. date: {}\n\
  63. draft: {}\n\
  64. type: "{}"\n\
  65. categories: ["{}"]\n\
  66. ---'''.format(request.args["title"],
  67. get_now_time(),
  68. 'false',
  69. 'blog',
  70. get_trans_title_url_name(request.args["title"]))
  71. CatPath = UPLOAD_PATH_MAP[0][0]+"../blog/" + get_trans_title_url_name(request.args["title"])
  72. print(CatPath)
  73. if not os.path.exists(CatPath):
  74. os.mkdir(CatPath)
  75. with open(os.path.join(CatPath, 'category.md'), 'w', encoding="utf-8") as md:
  76. md.write(front_matter)
  77. print("11")
  78. return Response("你好", 200)
  79. @blogs_app.route('/backstage/blog/remove', methods=['POST'])
  80. def remove():
  81. remove_content()
  82. return redirect(url_for('blogs.blog_list'))
  83. @blogs_app.route('/backstage/utils', methods=['GET'])
  84. def transService():
  85. #print(request.args["trantext"])
  86. return get_trans_title_url_name(request.args["trantext"])
  87. """ def GetCategories():
  88. GetCategories
  89. configfiles = [os.path.join(dirpath, f)
  90. for dirpath, dirnames, files in os.walk(UPLOAD_PATH_MAP+'../blog')
  91. for f in fnmatch.filter(files, 'category.md')]
  92. return configfiles """