2016-03-02 120 views
0

我有一個使用基於Archetypes的內容的Plone網站家族。Plone,Archetypes:將文本字段從文本/純文本更改爲文本/ html

我有一些TextField s需要從text/plain改爲text/html;如倒塌的發言權,架構剪斷

TextField(
     name='summary', 
     default='', 
     read_permission=Access_contents_information, 
     default_content_type='text/plain', 
     allowable_content_types=('text/plain',), 
     storage=AnnotationStorage(migrate=True), 
     widget=TextAreaWidget(
      label=_('my_label_summary', 
        default='Summary'), 
      i18n_domain='plone', 
    ), 
), 

應更改爲類似

TextField(
     name='summary', 
     default='', 
     read_permission=Access_contents_information, 
     default_content_type='text/html', 
     default_output_type='text/html', 
     allowable_content_types=('text/html',), 
     storage=AnnotationStorage(migrate=True), 
     widget=RichWidget(
      label=_('my_label_summary', 
        default='Summary'), 
      i18n_domain='plone', 
    ), 
), 

由於對象的數量很小,我願意接受受影響的域的臨時醜陋的外觀(換行符);更重要的是要有可視化編輯器(這對我來說不適用於可切換的內容類型)。

當然,最好的解決方案是使用當前的text/plain字段,並且在編輯對象時,將它們轉換爲等效的合理text/html,然後可以使用可視化編輯器很好地進行編輯( CKEditor,就我而言)。但是,如果我簡單地使用已更改的模式編輯對象,則可視編輯器看起來很好,但存儲的文本被標籤包圍,並被解釋爲text/plain

我找到了/archetype_tool/manage_updateSchemaForm,但更新我的班級模式沒有幫助。

我發現https://plone.org/products/archetypes/documentation/old/ArchetypesDeveloperGuide/,但這看起來既不完整又過時。

任何指針?謝謝!

更新:

因爲這將不適合評論: 我已經創建了一個upgrades現在分裝; configure.zcml

<configure 
    xmlns="http://namespaces.zope.org/zope" 
    xmlns:genericsetup="http://namespaces.zope.org/genericsetup" 
    i18n_domain="plone"> 

    <genericsetup:upgradeStep 
     source="*" 
     destination="1001" 
     title="text/html fields for MyType" 
     profile="Products.myproduct:default" 
     handler=".to_1001.fix_mimetypes"/> 

</configure> 

模塊代碼(to_1001.py):

import logging 
from Products.CMFCore.utils import getToolByName 
from ..tools.log import getLogSupport 

logger, debug_active, DEBUG = getLogSupport(fn=__file__) 

def htmlify_attribute(o, attr_name, brain=None, default=u''): 
    """ 
    Change MIME type of a TextField to text/html 
    """ 
    attr = getattr(o, attr_name, None) 
    changed = False 

    brain_url = (brain is not None 
       and brain.getURL() 
       or None) 
    if not attr: 
     mutator = o.getField(attr_name).getMutator(o) 
     mutator(default) 
     attr = getattr(o, attr_name, None) 
     changed = True 

    convert = False 
    mimetype = getattr(attr, 'mimetype', 'text/plain') 
    if mimetype != 'text/html': 
     if brain_url is not None: 
      logger.info('Fixing MIME type of %(attr_name)s' 
         ' for %(brain_url)s', locals()) 
     setattr(attr, 'mimetype', 'text/html') 
     changed = True 

    return changed 

def fix_mimetypes(context): 
    """ 
    text/plain --> text/html for some MyType fields 
    """ 
    pc = getToolByName(context, 'portal_catalog') 
    TYPES = ['MyType'] 
    brains = pc.unrestrictedSearchResults(portal_type=TYPES) 
    total = len(brains) 
    MASK = 'Fixing MIME types for %(total)d %(TYPES)s objects' 
    logger.info(MASK + ' ...', locals()) 
    cnt = 0 

    import pdb; pdb.set_trace() 
    for brain in brains: 
     obj = brain.getObject() 
     if htmlify_attribute(obj, 'summary', brain): 
      cnt += 1 

    if cnt or True: 
     logger.info('%(cnt)d objects changed', locals()) 
    logger.info(MASK + ': DONE', locals()) 
    return ('Done '+MASK) % locals() 

由於我的產品缺乏special profile version,我創建一個.../profiles/default/metadata.xml文件並設置的1000的值;因爲在啓動時沒有任何事情發生,並且QuickInstaller中沒有什麼特別的東西可以觀察到,所以我重新安裝,然後將數字增加一。

我的to_1001模塊是在啓動時導入的,正如我可以通過註冊記錄器 (已記錄)所看到的;但它不是使用(因爲我知道因爲 pdb.set_trace()),既沒有啓動(bin/instance fg)與增加版本號,也沒有在QuickInstaller中重新安裝時。

缺什麼? 這個升級步驟應該如何工作,即被觸發?

回答

2

您可能需要現有對象的升級步驟。例如見eea。soercontent evolve19.pyconfigure.zcml

爲了測試,如果是這樣的話,寫的升級步驟之前,請編輯保存在不改變任何東西。現在,如果再次進入編輯狀態,則應該有適當的富文本編輯器。

+0

是的,我想我需要一個升級步驟;我會研究這個。我的主要問題不在於獲取富文本編輯器,而是爲了獲取正確的內容類型。 – Tobias

+0

我通過創建升級步驟的努力更新了我的問題。這應該如何工作,即被觸發? – Tobias

+0

將'1001'版本放入'profiles/default/metadata.xml'中,然後你的升級步驟應該出現在'Site Setup> Add-ons'或者通過ZMI:'portal_setup> manage_upgrades' – avoinea

1

我已經爲兩個客戶端使用此代碼來初始化新的富文本字段。我想不管他們是老的還是新的領域都沒關係。該函數將內容實例作爲輸入。所以你可以迭代目錄大腦並傳遞這個函數brain.getObject()

def initialize_rich_text_fields(instance): 
    """New rich text fields should have mimetype text/html. 

    Adapted from setDefaults in Archetypes BasicSchema. 
    """ 
    default_output_type = 'text/x-html-safe' 
    mimetype = 'text/html' 
    schema = instance.Schema() 
    for field in schema.values(): 
     # We only need to do this for fields with one specific mimetype. 
     if not shasattr(field, 'default_output_type'): 
      continue 
     if field.default_output_type != default_output_type: 
      continue 
     # only touch writable fields 
     mutator = field.getMutator(instance) 
     if mutator is None: 
      continue 
     base_unit = field.getBaseUnit(instance) 
     if base_unit.mimetype == mimetype: 
      continue 
     # If content has already been set, we respect it. 
     if base_unit: 
      continue 
     default = field.getDefault(instance) 
     args = (default,) 
     kw = {'field': field.__name__, 
       '_initializing_': True} 
     kw['mimetype'] = mimetype 
     mapply(mutator, *args, **kw) 
+0

啊,有趣的(雖然也許不*完全*我需要什麼)。對於可能包含「text/plain」或「text/html」的*每個字段,「text/x-html-safe」應該用作default_output_type嗎? – Tobias

+0

對不起 - 我猶豫了太久,無法編輯以前的評論... 因此'text/x-html-safe'是關於剝離危險的HTML標籤,並且建議用戶提供的任何內容。 標準轉換並非偶然將簡單的'text/plain'(文本段落和項目符號列表)轉換爲HTML,對嗎?我很抱歉 - 它是我的對象中的文本/純文本,而不是文本/結構化文件或類似文件。 – Tobias

+0

Plone附帶Products.PortalTransforms中的portal_transform工具。您可以使用它從一種MIME類型轉換爲另一種MIME類型。 – maurits