routes.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from flask import render_template, Blueprint, request, redirect, url_for, jsonify
  2. import requests
  3. from wtforms.compat import iteritems
  4. from backstage.collections.forms import CollectionCreateForm
  5. from backstage.utils import get_now_time
  6. from backstage.utils.routes import create_content, remove_content, get_trans_title_url_name
  7. from backstage.config import PORTAL_SERVER
  8. import os
  9. collections_app = Blueprint('collections', __name__)
  10. @collections_app.route('/backstage/collections')
  11. def collection_list():
  12. response = requests.get('{}contents?url=/collection'.format(PORTAL_SERVER))
  13. if response.status_code == 200:
  14. #sortedData = sorted(response.json(), key='date', reverse=True)
  15. #print(response.json())
  16. #aa = json.loads(response.text)
  17. #sortedData = sorted(response.json(), key=lambda x:x['date'], reverse=True)
  18. """ i=1
  19. for d in sortedData:
  20. if i == 1:
  21. print(d)
  22. for key, value in d.items():
  23. print(key+' '+value)
  24. i+=1 """
  25. #print(response.text)
  26. return render_template('collections.html',
  27. title='家具規劃作品',
  28. legend='家具規劃作品列表',
  29. collections=response.json(),
  30. length=len(response.json()),
  31. form=CollectionCreateForm())
  32. @collections_app.route('/backstage/collection/create', methods=['POST'])
  33. def create():
  34. form = CollectionCreateForm()
  35. form.image.data.filename = os.path.splitext(form.image.data.filename)[0].replace(".","") + os.path.splitext(form.image.data.filename)[1]
  36. eng_name = get_trans_title_url_name(form.title.data)
  37. front_matter = '''---
  38. title: "{}"\n\
  39. date: {}\n\
  40. draft: {}\n\
  41. type: "{}"\n\
  42. url: "{}"\n\
  43. image: "/img/collection/{}"\n\
  44. description: "{}"\n\
  45. ---'''.format(form.title.data,
  46. get_now_time(),
  47. 'true',
  48. 'collection',
  49. '/collection/{}'.format(eng_name),
  50. form.image.data.filename,
  51. form.description.data.replace('\r\n','<br>'))
  52. data = {'frontMatter': front_matter,
  53. 'name': eng_name,
  54. 'type': 'collection'}
  55. return create_content(data, form.image.data)
  56. @collections_app.route('/backstage/collection/remove', methods=['POST'])
  57. def remove():
  58. remove_content()
  59. return redirect(url_for('collections.collection_list'))