routes.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from concurrent.futures import process
  2. from flask import render_template, Blueprint, request, redirect, url_for, jsonify
  3. import requests
  4. from wtforms.compat import iteritems
  5. from backstage.collections.forms import CollectionCreateForm
  6. from backstage.utils import get_now_time
  7. from backstage.utils.routes import create_collection_content, remove_content, get_trans_title_url_name
  8. from backstage.config import PORTAL_SERVER
  9. import os
  10. collections_app = Blueprint('collections', __name__)
  11. def processImgFile(filename):
  12. return os.path.splitext(filename)[0].replace(".","") + os.path.splitext(filename)[1]
  13. @collections_app.route('/backstage/collections')
  14. def collection_list():
  15. response = requests.get('{}contents?url=/collection'.format(PORTAL_SERVER))
  16. if response.status_code == 200:
  17. #sortedData = sorted(response.json(), key='date', reverse=True)
  18. #print(response.json())
  19. #aa = json.loads(response.text)
  20. #sortedData = sorted(response.json(), key=lambda x:x['date'], reverse=True)
  21. """ i=1
  22. for d in sortedData:
  23. if i == 1:
  24. print(d)
  25. for key, value in d.items():
  26. print(key+' '+value)
  27. i+=1 """
  28. #print(response.text)
  29. return render_template('collections.html',
  30. title='家具規劃作品',
  31. legend='家具規劃作品列表',
  32. collections=response.json(),
  33. length=len(response.json()),
  34. form=CollectionCreateForm())
  35. @collections_app.route('/backstage/collection/create', methods=['POST'])
  36. def create():
  37. form = CollectionCreateForm()
  38. csliderimg = []
  39. csliderimgfilename = []
  40. form.image.data.filename = processImgFile(form.image.data.filename)
  41. # for file in form.collectionslider.data:
  42. # file.filename = processImgFile(file.filename)
  43. # csliderimg.append(file)
  44. # csliderimgfilename.append(file.filename)
  45. eng_name = get_trans_title_url_name(form.title.data)
  46. front_matter = '''---
  47. meta_title: "{}"\nmeta_description: "{}"\ntitle: "{}"\ndate: {}\n\
  48. draft: {}\n\
  49. type: "{}"\n\
  50. url: "{}"\n\
  51. image: "/img/title/{}"\n\
  52. collection_name: "{}"\n\
  53. cover_img: "/img/title/{}"\n\
  54. description: "{}"\n\
  55. tags: ["{}"]\n\
  56. banner_img_text: "{}"\n\
  57. homeowner: "{}"\n\
  58. size: "{}"\n\
  59. bed_num: "{}"\n\
  60. house_cat: "{}"\n\
  61. designer: "{}"\n\
  62. space: "{}"\n\
  63. loc: "{}"\n\
  64. budget: "{}"\n\
  65. construction: "{}"\n\
  66. collection_description: "{}"\n\
  67. collection_slider: "" \n\
  68. comment: "{}"\n
  69. ---'''.format(form.title.data, form.description.data, form.title.data,
  70. get_now_time(), 'true', 'collection', '/collection/{}'.format(eng_name),
  71. form.image.data.filename, form.collectiontitle.data, form.coverimg.data.filename, form.description.data.replace('\r\n','<br>'), form.tags.data,
  72. form.bannerimgtext.data, form.homeowner.data, form.size.data, form.bednum.data,
  73. form.housetype.data, form.designer.data, form.space.data, form.loc.data,
  74. form.budget.data, form.construction.data, form.collectiondesc.data, form.comment.data.filename)
  75. print(front_matter)
  76. data = {'frontMatter': front_matter,
  77. 'name': eng_name,
  78. 'type': 'collection',
  79. 'dest': '/collection/{}'.format(eng_name)}
  80. return create_collection_content(data, form.image.data, form.coverimg.data, csliderimg, form.comment.data)
  81. @collections_app.route('/backstage/collection/remove', methods=['POST'])
  82. def remove():
  83. remove_content()
  84. return redirect(url_for('collections.collection_list'))