routes.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. from backstage.config import PORTAL_SERVER
  7. collections_app = Blueprint('collections', __name__)
  8. @collections_app.route('/backstage/collections')
  9. def collection_list():
  10. response = requests.get('{}contents?url=/collection'.format(PORTAL_SERVER))
  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. 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(get_trans_title_url_name(form.title.data)),
  34. form.image.data.filename,
  35. form.description.data)
  36. data = {'frontMatter': front_matter,
  37. 'name': request.form['title'],
  38. 'type': 'collection'}
  39. return create_content(data, form.image.data)
  40. @collections_app.route('/backstage/collection/remove', methods=['POST'])
  41. def remove():
  42. remove_content()
  43. return redirect(url_for('collections.collection_list'))