routes.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from flask import render_template, Blueprint, request, redirect, url_for
  2. import requests
  3. from backstage.collections.forms import CollectionCreateForm
  4. from backstage.utils import get_now_time
  5. from backstage.utils.routes import create_content, remove_content, get_trans_title_url_name
  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. front_matter = '''---
  21. title: "{}"\n\
  22. date: {}\n\
  23. draft: {}\n\
  24. type: "{}"\n\
  25. url: "{}"\n\
  26. image: "/img/collection/{}"\n\
  27. description: "{}"\n\
  28. ---'''.format(form.title.data,
  29. get_now_time(),
  30. 'false',
  31. 'collection',
  32. '/collection/{}'.format(get_trans_title_url_name(form.title.data)),
  33. form.image.data.filename,
  34. form.description.data)
  35. data = {'frontMatter': front_matter,
  36. 'name': request.form['title'],
  37. 'type': 'collection'}
  38. return create_content(data, form.image.data)
  39. @collections_app.route('/backstage/collection/remove', methods=['POST'])
  40. def remove():
  41. remove_content()
  42. return redirect(url_for('collections.collection_list'))