123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- 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, translate
- 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', methods=['GET'])
- 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():
- transcat = ""
- form = BlogCreateForm()
- if form.categories.data == "居家美學":
- transcat = "home_aesthetics"
- elif form.categories.data == "規劃師QA":
- transcat = "room_planner_expertise"
- elif form.categories.data == "驗屋知識":
- transcat = "home_inspection_knowledge"
- else:
- transcat = get_trans_title_url_name(form.categories.data)
- transtitle = get_trans_title_url_name(form.title.data)
- front_matter = '''---
- title: "{}"\n\
- date: {}\n\
- draft: {}\n\
- type: "{}"\n\
- url: "{}"\n\
- image: "/img/blog/{}"\n\
- categories: ["{}"]\n\
- col1: "{}"\n\
- col2: "{}"\n\
- ---'''.format(form.title.data,
- get_now_time(),
- 'false',
- 'blog',
- '/blog/{}'.format(transtitle),
- form.image.data.filename,
- form.categories.data,
- transcat,"")
- data = {'frontMatter': front_matter,
- 'name': transtitle,
- 'type': 'blog',
- 'categories': form.categories.data,
- #'caturl': caturl
- }
- 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'))
- @blogs_app.route('/backstage/utils', methods=['GET'])
- def transService():
- #print(request.args["trantext"])
- return get_trans_title_url_name(request.args["trantext"])
- """ 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 """
|