routes.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. transcat = ""
  25. form = BlogCreateForm()
  26. if form.categories.data == "居家美學":
  27. transcat = "home_aesthetics"
  28. elif form.categories.data == "規劃師QA":
  29. transcat = "room_planner_expertise"
  30. elif form.categories.data == "驗屋知識":
  31. transcat = "home_inspection_knowledge"
  32. else:
  33. transcat = get_trans_title_url_name(form.categories.data)
  34. transtitle = get_trans_title_url_name(form.title.data)
  35. front_matter = '''---
  36. title: "{}"\n\
  37. date: {}\n\
  38. draft: {}\n\
  39. type: "{}"\n\
  40. url: "{}"\n\
  41. image: "/img/blog/{}"\n\
  42. categories: ["{}"]\n\
  43. col1: "{}"\n\
  44. col2: "{}"\n\
  45. ---'''.format(form.title.data,
  46. get_now_time(),
  47. 'false',
  48. 'blog',
  49. '/blog/{}'.format(transtitle),
  50. form.image.data.filename,
  51. form.categories.data,
  52. transcat,"")
  53. data = {'frontMatter': front_matter,
  54. 'name': transtitle,
  55. 'type': 'blog',
  56. 'categories': form.categories.data,
  57. #'caturl': caturl
  58. }
  59. return create_content(data, form.image.data)
  60. @blogs_app.route('/backstage/blog/createCat/', methods=['GET'])
  61. def createCat():
  62. #title = ""
  63. front_matter = '''---
  64. title: "{}"\n\
  65. date: {}\n\
  66. draft: {}\n\
  67. type: "{}"\n\
  68. categories: ["{}"]\n\
  69. ---'''.format(request.args["title"],
  70. get_now_time(),
  71. 'false',
  72. 'blog',
  73. get_trans_title_url_name(request.args["title"]))
  74. CatPath = UPLOAD_PATH_MAP[0][0]+"../blog/" + get_trans_title_url_name(request.args["title"])
  75. print(CatPath)
  76. if not os.path.exists(CatPath):
  77. os.mkdir(CatPath)
  78. with open(os.path.join(CatPath, 'category.md'), 'w', encoding="utf-8") as md:
  79. md.write(front_matter)
  80. print("11")
  81. return Response("你好", 200)
  82. @blogs_app.route('/backstage/blog/remove', methods=['POST'])
  83. def remove():
  84. remove_content()
  85. return redirect(url_for('blogs.blog_list'))
  86. @blogs_app.route('/backstage/utils', methods=['GET'])
  87. def transService():
  88. #print(request.args["trantext"])
  89. return get_trans_title_url_name(request.args["trantext"])
  90. """ def GetCategories():
  91. GetCategories
  92. configfiles = [os.path.join(dirpath, f)
  93. for dirpath, dirnames, files in os.walk(UPLOAD_PATH_MAP+'../blog')
  94. for f in fnmatch.filter(files, 'category.md')]
  95. return configfiles """