12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- from flask import render_template, Blueprint, request, redirect, url_for
- from flask.wrappers import Response
- import requests
- import os
- import fnmatch
- from backstage.blogs.forms import BlogCreateForm
- from backstage.utils import get_now_time
- from backstage.utils.routes import create_content, remove_content, get_trans_title_url_name
- from backstage.config import PORTAL_SERVER, UPLOAD_PATH_MAP
- blogs_app = Blueprint('blogs', __name__)
- @blogs_app.route('/backstage/blogs')
- def blog_list():
- response = requests.get('{}contents?url=/blog'.format(PORTAL_SERVER))
- if response.status_code == 200:
- sortedData = sorted(response.json(), key=lambda x: x['date'], reverse=True)
- return render_template('blogs.html',
- title='設計專欄',
- legend='設計專欄列表',
- blogs=sortedData,
- length=len(response.json()),
- form=BlogCreateForm())
- @blogs_app.route('/backstage/blog/create/', methods=['POST'])
- def create():
- form = BlogCreateForm()
- front_matter = '''---
- title: "{}"\n\
- date: {}\n\
- draft: {}\n\
- type: "{}"\n\
- url: "{}"\n\
- image: "/img/blog/{}"\n\
- categories: ["{}"]\n\
- ---'''.format(form.title.data,
- get_now_time(),
- 'false',
- 'blog',
- '/blog/{}'.format(get_trans_title_url_name(form.title.data)),
- form.image.data.filename,
- form.categories.data)
- data = {'frontMatter': front_matter,
- 'name': request.form['title'],
- 'type': 'blog',
- 'categories': form.categories.data}
- return create_content(data, form.image.data)
- @blogs_app.route('/backstage/blog/createCat/', methods=['GET'])
- def createCat():
- #title = ""
- front_matter = '''---
- title: "{}"\n\
- date: {}\n\
- draft: {}\n\
- type: "{}"\n\
- categories: ["{}"]\n\
- ---'''.format(request.args["title"],
- get_now_time(),
- 'false',
- 'blog',
- get_trans_title_url_name(request.args["title"]))
- CatPath = UPLOAD_PATH_MAP[0][0]+"../blog/" + get_trans_title_url_name(request.args["title"])
- print(CatPath)
- if not os.path.exists(CatPath):
- os.mkdir(CatPath)
- with open(os.path.join(CatPath, 'category.md'), 'w', encoding="utf-8") as md:
- md.write(front_matter)
- print("11")
- return Response("你好", 200)
- @blogs_app.route('/backstage/blog/remove', methods=['POST'])
- def remove():
- remove_content()
- return redirect(url_for('blogs.blog_list'))
- """ def GetCategories():
- GetCategories
- configfiles = [os.path.join(dirpath, f)
- for dirpath, dirnames, files in os.walk(UPLOAD_PATH_MAP+'../blog')
- for f in fnmatch.filter(files, 'category.md')]
- return configfiles """
|