routes.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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
  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')
  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. front_matter = '''---
  26. title: "{}"\n\
  27. date: {}\n\
  28. draft: {}\n\
  29. type: "{}"\n\
  30. url: "{}"\n\
  31. image: "/img/blog/{}"\n\
  32. categories: ["{}"]\n\
  33. ---'''.format(form.title.data,
  34. get_now_time(),
  35. 'false',
  36. 'blog',
  37. '/blog/{}'.format(get_trans_title_url_name(form.title.data)),
  38. form.image.data.filename,
  39. form.categories.data)
  40. data = {'frontMatter': front_matter,
  41. 'name': request.form['title'],
  42. 'type': 'blog',
  43. 'categories': form.categories.data}
  44. return create_content(data, form.image.data)
  45. @blogs_app.route('/backstage/blog/createCat/', methods=['GET'])
  46. def createCat():
  47. #title = ""
  48. front_matter = '''---
  49. title: "{}"\n\
  50. date: {}\n\
  51. draft: {}\n\
  52. type: "{}"\n\
  53. categories: ["{}"]\n\
  54. ---'''.format(request.args["title"],
  55. get_now_time(),
  56. 'false',
  57. 'blog',
  58. get_trans_title_url_name(request.args["title"]))
  59. CatPath = UPLOAD_PATH_MAP[0][0]+"../blog/" + get_trans_title_url_name(request.args["title"])
  60. print(CatPath)
  61. if not os.path.exists(CatPath):
  62. os.mkdir(CatPath)
  63. with open(os.path.join(CatPath, 'category.md'), 'w', encoding="utf-8") as md:
  64. md.write(front_matter)
  65. print("11")
  66. return Response("你好", 200)
  67. @blogs_app.route('/backstage/blog/remove', methods=['POST'])
  68. def remove():
  69. remove_content()
  70. return redirect(url_for('blogs.blog_list'))
  71. """ def GetCategories():
  72. GetCategories
  73. configfiles = [os.path.join(dirpath, f)
  74. for dirpath, dirnames, files in os.walk(UPLOAD_PATH_MAP+'../blog')
  75. for f in fnmatch.filter(files, 'category.md')]
  76. return configfiles """