routes.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. import uuid
  11. collections_app = Blueprint('collections', __name__)
  12. def processImgFile(filename):
  13. return os.path.splitext(filename)[0].replace(".","") + os.path.splitext(filename)[1]
  14. @collections_app.route('/backstage/collections')
  15. def collection_list():
  16. response = requests.get('{}contents?url=/collection'.format(PORTAL_SERVER))
  17. if response.status_code == 200:
  18. #sortedData = sorted(response.json(), key='date', reverse=True)
  19. #print(response.json())
  20. #aa = json.loads(response.text)
  21. #sortedData = sorted(response.json(), key=lambda x:x['date'], reverse=True)
  22. """ i=1
  23. for d in sortedData:
  24. if i == 1:
  25. print(d)
  26. for key, value in d.items():
  27. print(key+' '+value)
  28. i+=1 """
  29. #print(response.text)
  30. return render_template('collections.html',
  31. title='家具規劃作品',
  32. legend='家具規劃作品列表',
  33. collections=response.json(),
  34. length=len(response.json()),
  35. form=CollectionCreateForm())
  36. @collections_app.route('/backstage/collection/create', methods=['POST'])
  37. def create():
  38. form = CollectionCreateForm()
  39. csliderimg = []
  40. csliderimgfilename = []
  41. ownerimgfile = ""
  42. commentimgfile = ""
  43. form.image.data.filename = processImgFile(form.image.data.filename)
  44. # for file in form.collectionslider.data:
  45. # file.filename = processImgFile(file.filename)
  46. # csliderimg.append(file)
  47. # csliderimgfilename.append(file.filename)
  48. eng_name = get_trans_title_url_name(form.title.data)
  49. form.image.data.filename = str(uuid.uuid4()).replace('-','') + ".webp"
  50. form.coverimg.data.filename = str(uuid.uuid4()).replace('-','') + ".webp"
  51. print(form.ownerimg.data)
  52. if form.ownerimg.data:
  53. form.ownerimg.data.filename = str(uuid.uuid4()).replace('-','') + ".webp"
  54. ownerimgfile = "img/" + form.ownerimg.data.filename
  55. if form.comment.data:
  56. form.comment.data.filename = str(uuid.uuid4()).replace('-','') + ".webp"
  57. commentimgfile = "img/" + form.comment.data.filename
  58. front_matter = '''---
  59. meta_title: "{}"\nmeta_description: "{}"\ntitle: "{}"\ndate: {}\n\
  60. draft: {}\n\
  61. type: "{}"\n\
  62. url: "{}"\n\
  63. image: "/img/title/{}"\n\
  64. collection_name: "{}"\n\
  65. cover_img: "/img/title/{}"\n\
  66. description: "{}"\n\
  67. tags: ["{}"]\n\
  68. banner_img_text: "{}"\n\
  69. homeowner: "{}"\n\
  70. owner_img: "{}"\n\
  71. size: "{}"\n\
  72. house_size: "{}"\n\
  73. bed_num: "{}"\n\
  74. room_count: "{}"\n\
  75. house_cat: "{}"\n\
  76. designer: "{}"\n\
  77. space: "{}"\n\
  78. loc: "{}"\n\
  79. budget_range: "{}"\n\
  80. budget: "{}"\n\
  81. construction: "{}"\n\
  82. collection_description: "{}"\n\
  83. collection_slider: [] \n\
  84. comment: "{}"\n\
  85. ---'''.format(form.title.data, form.description.data.replace('\r\n','<br>'), form.title.data,
  86. get_now_time(), 'true', 'collection', '/collection/{}'.format(eng_name),
  87. form.image.data.filename, form.collectiontitle.data, form.coverimg.data.filename, form.description.data.replace('\r\n','<br>'), form.tags.data,
  88. form.bannerimgtext.data, form.homeowner.data, ownerimgfile, form.size.data, form.housesize.data, form.bednum.data,
  89. form.roomcount.data, form.housetype.data, form.designer.data, form.space.data, form.loc.data, form.budgetr.data,
  90. form.budget.data, form.construction.data, form.collectiondesc.data.replace('\r\n','<br>'), commentimgfile)
  91. print(front_matter)
  92. data = {'frontMatter': front_matter,
  93. 'name': eng_name,
  94. 'type': 'collection',
  95. 'dest': '/collection/{}'.format(eng_name)}
  96. return create_collection_content(data, form.image.data, form.coverimg.data, csliderimg, form.comment.data, form.ownerimg.data)
  97. @collections_app.route('/backstage/collection/remove', methods=['POST'])
  98. def remove():
  99. remove_content()
  100. return redirect(url_for('collections.collection_list'))