routes.py 2.2 KB

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