2018/09/26

Plone5 New Features

對網頁技術人員而言,多少都要接觸 Javascript 語言衍生的生態圈,為了和這股潮流接軌,新版 Plone 透過 Mockup 讓 Javascript 客戶端的整合,變得更容易。這個導入及轉換工作,採用 PatternsLib 設計概念,會逐步改寫 Plone 的服務工具,例如 plone.app.widgets 和 plone.app.toolbar 這兩個模組,是利用 Mockup 機制做出的成果範例。

多國語系的支援是透過 plone.app.multilingual 模組,它在 Plone 4.3 時代就存在,新版環境就是預設安裝,啟用後就能套用。下列是批次匯入內容的範例:

#!/usr/bin/python
# -*- coding: utf-8 -*-

from Testing import makerequest
from AccessControl.SecurityManagement import newSecurityManager
from Products.CMFCore.utils import getToolByName

root = makerequest.makerequest(app)
site = root.mysite
admin = root.acl_users.getUserById('admin')
admin = admin.__of__(site.acl_users)

newSecurityManager(None, admin)

from zope.site.hooks import setHooks
from zope.component.hooks import setSite
setHooks()
setSite(site)
site.setupCurrentSkin(site.REQUEST)


from plone.dexterity.utils import createContentInContainer
from plone.app.multilingual.interfaces import ITranslationManager
from plone.app.multilingual import api as pamapi
from plone import api as ploneapi
from Products.CMFPlone.interfaces import ILanguage
from plone.dexterity.utils import iterSchemata, iterSchemataForType
#from plone.rfc822 import constructMessageFromSchemata
#from plone.rfc822 import initializeObjectFromSchemata

import csv
def unicode_csv_reader(utf8_data, dialect=csv.excel, **kwargs):
    csv_reader = csv.reader(utf8_data, dialect=dialect, **kwargs)
    for row in csv_reader:
        yield [unicode(cell, 'utf-8') for cell in row]

wftool = getToolByName(site, 'portal_workflow')

with open('/home/marr/plone511/zinstance/csv_script/item.csv', 'rb') as f:
    dialect = csv.Sniffer().sniff(f.read(), delimiters="\t")
    f.seek(0)
    reader = unicode_csv_reader(f, dialect)
    for row in reader:
        folder = site.en.events
        item = createContentInContainer(folder, 'Event', title=row[3])
        oldid = item.id
#       language = ILanguage(item).get_language()
#       print language
        manager = ITranslationManager(item)
        manager.add_translation('en')
        en = manager.get_translation('en')
#       manager.register_translation('en', en)
        en.title = row[3]
        en.description = row[4]
        manager.add_translation('zh')
        zh = manager.get_translation('zh')
        zh.title = row[1]
        zh.description = row[2]
        ploneapi.content.delete(obj=item)
        # site.en.events.manage_renameObject(oldid, str(row[0]))
        # site.zh.events.manage_renameObject(oldid, str(row[0]))
        #wftool.doActionFor(site.en.events[str(row[0])], 'publish')
        wftool.doActionFor(en, 'publish')
        #wftool.doActionFor(site.zh.events[str(row[0])], 'publish')
        wftool.doActionFor(zh, 'publish')
        en.reindexObject()
        #print row[0] + " en done."
        zh.reindexObject()
        #print row[0] + " zh done."
        #print pamapi.get_translation_manager(item).get_translations()

import transaction
transaction.commit()

2018/09/04

Archetypes Migration to Dexterity

Link Integrity Check

Be sure to set it disabled

Image Attributes in RichText Fields

<p><img class="image-inline" title="開基武廟外觀" src="kjwm-wg.jpg/@@images/c57ff855-3d5e-4eaf-8e5c-5e32db58867e.jpeg" alt="開基武廟外觀" /></p>
<p>開基武廟外觀   謝奇峰拍攝2011.8.6</p>
<p><img class="image-inline" title="開基武廟前殿" src="kjwm-qd.jpg/@@images/9c0d6de5-3295-49a5-970d-e26f51b117d6.jpeg" alt="開基武廟前殿" /></p>
<p>開基武廟前殿   謝奇峰拍攝2011.8.6</p>
<p><img class="image-inline" title="開基武廟前殿神龕" src="kjwm-qdsz.jpg/@@images/42c54979-0cad-4244-b692-80d736e25f77.jpeg" alt="開基武廟前殿神龕" /></p>
<p>開基武廟前殿神龕    謝奇峰拍攝2011.8.6</p>
<p> </p>
<p><img class="image-inline" title="開基武廟後殿" src="kjwm-hd.jpg/@@images/e74bc633-d278-47bd-8c13-223c16c1e5eb.jpeg" alt="開基武廟後殿" /></p>
<p>開基武廟後殿   謝奇峰拍攝2011.8.6</p>
<p><img class="image-inline" title="開基武廟後殿之三川殿" src="kjwm-hdscd.jpg/@@images/4919cf95-a664-48c1-a0d7-5cef3b91c4a3.jpeg" alt="開基武廟後殿之三川殿" /></p>
<p>開基武廟後殿之三川殿   謝奇峰拍攝2011.8.6</p>
<p><img class="image-inline" title="開基武廟後殿之拜殿" src="kjwm-hdbd.jpg/@@images/66b3e3e9-7be7-4513-beff-53b1a70bba18.jpeg" alt="開基武廟後殿之拜殿" /></p>
<p>開基武廟後殿之拜殿   謝奇峰拍攝2011.8.6</p>
<p><img class="image-inline" title="開基武廟後殿之正殿" src="kjwm-hdzd.jpg/@@images/76e89581-2bf4-4410-b08e-05882fada26d.jpeg" alt="開基武廟後殿之正殿" /></p>
<p>開基武廟後殿之正殿  謝奇峰拍攝2011.8.6</p>
import re

text_list = html_doc.split('\n')
result = []

for text in text_list:
    find = re.search(r'(^.* src\s*=\s*")(.*\.jpg/@@images/)(.*\.jpeg")( alt=.*$)', text)
    if find:
        result.append(find.group(1) + find.group(2) + 'image/preview"' + find.group(4))
    else:
        result.append(text)
print '\n'.join(result)

利用 bin/plonectl run update.py 來更新 RichText 內容

#!/usr/bin/python
# -*- coding: utf-8 -*-

from Testing import makerequest
from AccessControl.SecurityManagement import newSecurityManager
from Products.CMFCore.utils import getToolByName

root = makerequest.makerequest(app)
site = root.mysite
admin = root.acl_users.getUserById('admin')
admin = admin.__of__(site.acl_users)

newSecurityManager(None, admin)

from zope.site.hooks import setHooks
from zope.component.hooks import setSite
setHooks()
setSite(site)
site.setupCurrentSkin(site.REQUEST)


import re
from plone import api
from plone.app.textfield.value import RichTextValue
import transaction

for item in api.content.find(Type='MyType'):
    obj = item.getObject()
    if obj.myfield == None:
        text = []
    else:
        text = obj.myfield.raw.split('\n')
    result = []
    for line in text:
        find = re.search(r'(^.* src\s*=\s*")(.*\.jpg/@@images/)(.*\.jpeg")( .*$)', line)
        if find:
            result.append(find.group(1)+find.group(2)+'image/preview"'+find.group(4))
        else:
            result.append(line)
    result = '\n'.join(result)
    obj.myfield = RichTextValue(result, 'text/html', 'text/x-html-safe', 'utf-8')

    obj.reindexObject()

transaction.commit()