-->

使用python保存文件到Plone站点(Save file to plone site using

2019-10-22 12:00发布

我产生了一个Python的XML文件,我需要将其保存到我的Plone站点,但我不能确定如何。

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是没有意识到这一点。

Answer 1:

您可以创建在Plone内容对象称为文件。 请参阅: http://docs.plone.org/external/plone.api/docs/content.html#create-content

下面通过一个例子untestet给你一个猜测:

# -*- 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! 该脚本可以从命令行运行:

./bin/instance run your_script.py

但作为一个侧面说明,你可以让Plone的你创造一个sitemap_index.xml.gz,只是使其能够在site_setup>主。



Answer 2:

我不能确定你真的需要看到这个文件作为一个Plone内容。 也许你只需要打电话时,露出文件http://something/sitemap_index.xml

你可以简单地把它添加从ZMI一个Zope文件(这样该文件将可用,但不会是一个CMS的内容)或在Plone前面使用Apache / NGIX发布。



文章来源: Save file to plone site using python