routes.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from flask import render_template, Blueprint, flash, request, redirect, url_for
  2. import requests
  3. from backstage.collections.forms import CollectionCreateForm
  4. from backstage.utils import translate, get_now_time
  5. from time import sleep
  6. collections_app = Blueprint('collections', __name__)
  7. @collections_app.route('/backstage/collections')
  8. def collection_list():
  9. response = requests.get('http://127.0.0.1:5000/api/contents?url=/collection')
  10. if response.status_code == 200:
  11. return render_template('collections.html',
  12. title='家具規劃作品',
  13. legend='家具規劃作品列表',
  14. collections=response.json(),
  15. length=len(response.json()),
  16. form=CollectionCreateForm())
  17. @collections_app.route('/backstage/collection/create', methods=['POST'])
  18. def create():
  19. form = CollectionCreateForm()
  20. url_name = translate(form.title.data).replace(' ', '_')
  21. front_matter = '''---
  22. title: "{}"\n\
  23. date: {}\n\
  24. draft: {}\n\
  25. type: "{}"\n\
  26. url: "{}"\n\
  27. image: "/img/collection/{}"\n\
  28. description: "{}"\n\
  29. ---'''.format(form.title.data,
  30. get_now_time(),
  31. 'false',
  32. 'collection',
  33. '/collection/{}'.format(url_name),
  34. form.image.data.filename,
  35. form.description.data)
  36. data = {'frontMatter': front_matter,
  37. 'name': request.form['title'],
  38. 'type': 'collection'}
  39. new_response = requests.post('http://127.0.0.1:5000/api/new_content', json=data)
  40. if new_response.status_code == 200:
  41. requests.post(
  42. 'http://127.0.0.1:5000/api/upload/static/img?type=collection&filename={}'.format(
  43. form.image.data.filename), files={'image': form.image.data})
  44. sleep(0.5)
  45. return redirect(url_for('editor.editor', url='/collection/{}'.format(url_name)))
  46. else:
  47. flash('新增文章失敗', 'danger')
  48. return redirect(url_for('collections.collection_list'))