问题描述
我正在用python生成xml文件,我需要将其保存到我的plone网站,但不确定如何保存.
I am generating an xml file in python, I need to save it to my plone site but am unsure how to.
def generate_sitemap_index_file(self, sites):
""" Generate a google sitemap index file
"""
root = ET.Element("sitemapindex")
for site in sites:
sitemap = ET.SubElement(root, "sitemap")
loc = ET.SubElement(sitemap, "loc")
loc.text = self.aq_parent.absolute_url() + "/googlesitemap/" + site
ET.ElementTree(root).write("sitemap_index.xml")
此功能将文件保存到我的zinstance
文件夹中,但是zope无法识别它.
This function saves the file into my zinstance
folder, but zope isn't aware of it.
推荐答案
您可以在Plone中创建一个名为File的内容对象.请参阅: http://docs.plone.org/external /plone.api/docs/content.html#create-content
You can create a content object called File in Plone.See: http://docs.plone.org/external/plone.api/docs/content.html#create-content
这里有一个未经证实的例子,可以给你一个猜测:
Here an untestet example to give you a guess:
# -*- coding: utf-8 -*-
from zope.site.hooks import setSite
from plone.namedfile.file import NamedBlobFile
from plone import api
import transaction
portal = app["Plone"] # Plone is your Plone site in Zope root
setSite(portal)
container = portal
# create the file object:
file_obj = api.content.create(
container, 'File',
id='sitemap_index.xml',
title='sitemap_index.xml',
safe_id=True
)
# attache the file to the file object:
file_obj.file = NamedBlobFile(
data=your_file_handle,
contentType='application/xml',
filename='sitemap_index.xml',
)
transaction.commit()
确保已在buildout.cfg鸡蛋部分中添加了plone.api!您可以从命令行运行以下脚本:
Make sure you have added plone.api in your buildout.cfg eggs part!This script you can run from commandline:
./bin/instance run your_script.py
但是要注意的是,您可以让Plone为您构建一个sitemap_index.xml.gz,只需在site_setup> main中启用它即可.
But as a side note, you can let Plone build you a sitemap_index.xml.gz, just enable it in site_setup > main.
这篇关于使用python将文件保存到plone网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!