from concurrent.futures import process
from flask import render_template, Blueprint, request, redirect, url_for, jsonify
import requests
from wtforms.compat import iteritems
from backstage.collections.forms import CollectionCreateForm
from backstage.utils import get_now_time
from backstage.utils.routes import create_collection_content, remove_content, get_trans_title_url_name
from backstage.config import PORTAL_SERVER
import os
import uuid
collections_app = Blueprint('collections', __name__)
def processImgFile(filename):
return os.path.splitext(filename)[0].replace(".","") + os.path.splitext(filename)[1]
@collections_app.route('/backstage/collections')
def collection_list():
response = requests.get('{}contents?url=/collection'.format(PORTAL_SERVER))
if response.status_code == 200:
#sortedData = sorted(response.json(), key='date', reverse=True)
#print(response.json())
#aa = json.loads(response.text)
#sortedData = sorted(response.json(), key=lambda x:x['date'], reverse=True)
""" i=1
for d in sortedData:
if i == 1:
print(d)
for key, value in d.items():
print(key+' '+value)
i+=1 """
#print(response.text)
return render_template('collections.html',
title='家具規劃作品',
legend='家具規劃作品列表',
collections=response.json(),
length=len(response.json()),
form=CollectionCreateForm())
@collections_app.route('/backstage/collection/create', methods=['POST'])
def create():
form = CollectionCreateForm()
csliderimg = []
csliderimgfilename = []
ownerimgfile = ""
commentimgfile = ""
form.image.data.filename = processImgFile(form.image.data.filename)
# for file in form.collectionslider.data:
# file.filename = processImgFile(file.filename)
# csliderimg.append(file)
# csliderimgfilename.append(file.filename)
eng_name = get_trans_title_url_name(form.title.data)
form.image.data.filename = str(uuid.uuid4()).replace('-','') + ".webp"
form.coverimg.data.filename = str(uuid.uuid4()).replace('-','') + ".webp"
print(form.ownerimg.data)
if form.ownerimg.data:
form.ownerimg.data.filename = str(uuid.uuid4()).replace('-','') + ".webp"
ownerimgfile = "img/" + form.ownerimg.data.filename
if form.comment.data:
form.comment.data.filename = str(uuid.uuid4()).replace('-','') + ".webp"
commentimgfile = "img/" + form.comment.data.filename
front_matter = '''---
meta_title: "{}"\nmeta_description: "{}"\ntitle: "{}"\ndate: {}\n\
draft: {}\n\
type: "{}"\n\
url: "{}"\n\
image: "/img/title/{}"\n\
collection_name: "{}"\n\
cover_img: "/img/title/{}"\n\
description: "{}"\n\
tags: ["{}"]\n\
banner_img_text: "{}"\n\
homeowner: "{}"\n\
owner_img: "{}"\n\
size: "{}"\n\
house_size: "{}"\n\
bed_num: "{}"\n\
room_count: "{}"\n\
house_cat: "{}"\n\
designer: "{}"\n\
space: "{}"\n\
loc: "{}"\n\
budget_range: "{}"\n\
budget: "{}"\n\
construction: "{}"\n\
collection_description: "{}"\n\
collection_slider: [] \n\
comment: "{}"\n\
---'''.format(form.title.data, form.description.data.replace('\r\n','
'), form.title.data,
get_now_time(), 'true', 'collection', '/collection/{}'.format(eng_name),
form.image.data.filename, form.collectiontitle.data, form.coverimg.data.filename, form.description.data.replace('\r\n','
'), form.tags.data,
form.bannerimgtext.data, form.homeowner.data, ownerimgfile, form.size.data, form.housesize.data, form.bednum.data,
form.roomcount.data, form.housetype.data, form.designer.data, form.space.data, form.loc.data, form.budgetr.data,
form.budget.data, form.construction.data, form.collectiondesc.data.replace('\r\n','
'), commentimgfile)
print(front_matter)
data = {'frontMatter': front_matter,
'name': eng_name,
'type': 'collection',
'dest': '/collection/{}'.format(eng_name)}
return create_collection_content(data, form.image.data, form.coverimg.data, csliderimg, form.comment.data, form.ownerimg.data)
@collections_app.route('/backstage/collection/remove', methods=['POST'])
def remove():
remove_content()
return redirect(url_for('collections.collection_list'))