routes.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 json
  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(len(response))
  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=sortedData,
  30. length=len(response.json()),
  31. form=CollectionCreateForm())
  32. @collections_app.route('/backstage/collection/create', methods=['POST'])
  33. def create():
  34. form = CollectionCreateForm()
  35. eng_name = get_trans_title_url_name(form.title.data)
  36. front_matter = '''---
  37. title: "{}"\n\
  38. date: {}\n\
  39. draft: {}\n\
  40. type: "{}"\n\
  41. url: "{}"\n\
  42. image: "/img/collection/{}"\n\
  43. description: "{}"\n\
  44. ---'''.format(form.title.data,
  45. get_now_time(),
  46. 'false',
  47. 'collection',
  48. '/collection/{}'.format(eng_name),
  49. form.image.data.filename,
  50. form.description.data.replace('\r\n','<br>'))
  51. data = {'frontMatter': front_matter,
  52. 'name': eng_name,
  53. 'type': 'collection'}
  54. return create_content(data, form.image.data)
  55. @collections_app.route('/backstage/collection/remove', methods=['POST'])
  56. def remove():
  57. remove_content()
  58. return redirect(url_for('collections.collection_list'))