Skip to content. | Skip to navigation

Personal tools

Navigation

You are here: Home / Tips / Python Script

Python Script

比對變數的型別

String Escaping

HTTP Request and Response

權限

可以讀取設定值:

from Products.CMFCore.utils import getToolByName

properties = getToolByName(context, 'portal_properties')
site_lang = properties.site_properties.default_language

return site_lang

不能指定設定值:

properties.site_properties.default_language = 'zh_TW'

TypeError: attribute-less object (assign or del)

Python Script 不能執行 urllib2.urlopen() 或 datetime.datetime.now() 之類的指令。

request = container.REQUEST

for name, value in request.environ.items():
    print "%s: %s" % (name, value)

return printed

http://stackoverflow.com/questions/9174747/python-script-in-plone

# Example code:

# Import a standard function, and get the HTML request and response objects.
from Products.PythonScripts.standard import html_quote
request = container.REQUEST
response =  request.response

# Return a string identifying this script.
print "This is the", script.meta_type, '"%s"' % script.getId(),
if script.title:
    print "(%s)" % html_quote(script.title),
print "in", container.absolute_url()
return printed

getSelectedField

for brain in context.portal_catalog(portal_type="Book"):
    try:
        obj = brain.getObject()
        print "%s %s" % (obj.Title(), obj.getIsbn())
    except:
        pass
return printed
from Products.CMFCore.utils import getToolByName

request = container.REQUEST
catalog = getToolByName(context, 'portal_catalog')
path = '/crgis/temple/TainanCounty'

for brain in catalog(portal_type='Temple', path=path):
    try:
        obj = brain.getObject()
        print "%s %s %s" % (obj.getId(), obj.Title(), obj.getEra())
    except:
        pass
return printed
for i in range(len(brain)):
    print brain[i].getPath().split('/')[-1] + ', ' + 'http://mysite.com' + brain[i].getPath()[6:]

only allow python scripts to be called by DTML methods or other python scripts and not directly TTW

check that the script is not the published object with:

if container.REQUEST['PUBLISHED'] is script:
    raise 'Forbidden'

For newer versions of Zope raise an exception object:

from zExceptions import Forbidden
if container.REQUEST['PUBLISHED'] is script:
    raise Forbidden('Script may not be published.')