Skip to content. | Skip to navigation

Personal tools

Navigation

You are here: Home / Tips / Archetypes QuickStart

Archetypes QuickStart

Archetypes 是 Plone 建置內容型別的框架之一,也是預設內容型別的基礎。

Archetypes 原本只是 Plone 的擴充模組,在 Plone 2.1 版本時,成為核心模組的一部份。Plone 預設的內容項目模組,稱為 Products.ATContentTypes,其中的 AT 就是 Archetypes 的縮寫。到 Plone 5.0 版本時,成為預設不安裝的內建模組,改由 Dexterity 預設提供它的功能。

Best Practice

決定使用 Archetypes 前的評估方式,包括設計通則,常見型別,需要留意的細節。從基本觀念到工具方法,都可以從 Best Practice 心法獲得。

General Concetps

Arcehtypes Manual 是必讀的文件,想要發揮它的效能,下列通則值得留意:

  • 先了解 Plone 預設提供的介面項目,並分析需要的資料內容模型,以選用合適的 Field 與 Widget,通常是最有效的方式。
  • 對於 one-to-many 的關連模型,可以試著將 one 利用 container 來實作。
  • 如果 many 部份無法放在 one 裡,可以使用 Reference Field 來實作。
  • many-to-many 的關連模型,要使用 Reference Field 來實作。
  • 盡量避免修改預設的 Edit 介面。
  • 控制 Type 和 Field 的數量。

Archetypes 目前有些限制:

  • 舊版本的 Type 無法用到 Plone 的新功能。
  • Collection 無法搜尋新的 Type 項目。
  • Catalog 無法搜尋新的 Type 項目。

on Plone 3 and below you must use interfaces inside the "Products.ATContentTypes.interface" module (note: missing final "s"). 從 Plone 3 開始,Archetypes 在 BaseObject 的 schema 加入 SchemaExtender 支援,調整 Type 的工作,變得更有彈性。

class BaseObject(Referenceable):
# ... ...

def Schema(self):
"""Return a (wrapped) schema instance
for this object instance.
"""
schema = ISchema(self)
return ImplicitAcquisitionWrapper(schema, self)

無法在新版 Plone 環境工作的話,可以先和老板溝通

Tools

ZopeSkel:

$ ../bin/zopeskel archetype

archetype: A Plone project that uses Archetypes content types

This creates a Plone project that uses Archetypes content types.
It has local commands that will allow you to add content types
and to add fields to your new content types.


This template expects a project name with 1 dot in it (a 'basic
namespace', like 'foo.bar').

Enter project name: myproj.atcontent

If at any point, you need additional help for a question, you can enter
'?' and press RETURN.

Expert Mode? (What question mode would you like? (easy/expert/all)?) ['easy']: Project Title (Title of the project) ['Example Name']: MyProject ATContent Version (Version number for project) ['1.0']: Description (One-line description of the project) ['']: MyProject AT Content Types Creating directory ./myproj.atcontent Replace 0 bytes with 119 bytes (0/0 lines changed; 5 lines added) Replace 913 bytes with 1069 bytes (0/32 lines changed; 6 lines added) Replace 119 bytes with 2074 bytes (2/5 lines changed; 45 lines added) Replace 623 bytes with 868 bytes (5/19 lines changed; 7 lines added) Replace 70 bytes with 371 bytes (3/6 lines changed; 11 lines added) Replace 1069 bytes with 1849 bytes (12/38 lines changed; 32 lines added) ------------------------------------------------------------------------------ The project you just created has local commands. These can be used from within the product. usage: paster COMMAND Commands: addcontent Adds plone content types to your project For more information: paster help COMMAND ------------------------------------------------------------------------------ ************************************************************************** ** There is a local command to add individual Archetype content ** types and to add fields to those content types. See the ** instructions above on how to use this command. **************************************************************************
$ ../../bin/paster addcontent --list
Available templates:
    atschema:      A handy AT schema builder
    browserlayer:  A Plone browserlayer
    contenttype:   A content type skeleton
    form:          A form skeleton
    formfield:     Schema field for a form
    i18nlocale:    An i18n locale directory structure
    portlet:       A Plone 3 portlet
    view:          A browser view skeleton
    zcmlmeta:      A ZCML meta directive skeleton

Files and Directories

準備好 Debug Mode 環境,依照順序要處理的項目包括:

  • __init__.py 通常包括註冊 message factory 處理訊息中文化的工作。
  • configure.zcml 負責註冊 component 的工作,載入 content, browser, portlets 等子模組。
  • config.py 包含全域變數,例如專案名稱、權限名稱等。
  • interfaces.py 包含型別介面的定義值,定義的結果再由 content 裡的 class 來實作。
  • content/ 包含內容型別的實作方式,包括 adapter, event handler 等。
  • browser/ 內含和畫面顯示有關的檔案,包括 view class, template, icon, CSS 等。
  • profiles/ 內含 GenericSetup 的安裝資訊。
  • portlets/ 仿照 plone.app.portlets.portlets 運作模式的檔案。
  • test/ 內含測試用的程式碼。
  • README.txt 內含說明資訊,包括 doctest 等。
  • version.txt 提供給 Plone 版本資訊。

Content Type

下列是表單的關鍵字詞:

property list

Products/Archetypes/BaseObject.py 檔案裡包含物件定義。

getText vs getRawText

Metadata

預設的 Dublin Core metadata 相關欄位,設定在 Products/Archetypes/ExtensibleMetadata.py 檔案裡,例如 rights 欄位的資訊:

TextField(
'rights',
accessor="Rights",
default_method='defaultRights',
widget=TextAreaWidget(
label=_(u'label_copyrights', default=u'Rights'),
description=_(u'help_copyrights',
default=u'Copyright statement.'),
)),

透過 default_method 可以指定預設的內容,例子裡的 defaultRights 就是呼叫 portal_metadata 的 listSchemas() 來比對是否存在預設值,而且在 Products/ATContentTypes/content/schemata.py 檔案裡,利用 finalizeATCTSchema() 來指派各欄位的 schemata 位置。

對於 Dexterity 而言,用的是 Products.CMFDefault.DublinCore 裡的實作,兩者可能還有些不同,例如 Dexterity 也有 setModificationDate,但沒有 setCreationDate,而是直接使用 creation_date 屬性值就行,直到有人改寫程式碼才會兩者一致。

設定欄位順序

用英文版的網頁可以看到 tab 名稱, 例如 Default 頁籤, 在程式碼裡就是要用schemata='default' 記住: 全部都小寫的英文字母, 顯示時, 它會自動把第一個字母變大寫, 但程式碼裡, 要全都是小寫, 最後再手動處理它的中文翻譯問題 (如果指定自訂的 tab 就需要另行處理中文翻譯的工作)

Reference Field and Widget

Viewlet: plone.belowcontentbody.relateditems | ViewletManager: plone.belowcontentbody (plone.app.layout.viewlets.interfaces.IBelowContentBody)

<div tal:replace="structure provider:plone.belowcontentbody" />

custom widget archetypes.referencebrowserwidget upgraded to 2.4.21 to fix ReferenceError: refbrowser_moveReferenceUp is not defined

atapi.ReferenceField(
    'tags',
    storage=atapi.AnnotationStorage(),
    widget=atapi.ReferenceWidget(
    ... ...
),

Dexterity version: z3c.relationfield.schema.{RelationList, RelationChoice}

BackReferenceField

https://dev.inigo-tech.com/svn/actalliance/act.content/tags/1.2.5/act/content/content/story.py

Related Items Invisible by Default

for Folerish types:

YourSchema['relatedItems'].widget.visible['edit'] = 'visible'

Selection and Vocabulary

下拉選單裡的選項可以利用 DisplayList 來產生,資料來源包括 SQL query 等。

透過 ATVocabularyManager 可以利用網頁表單來建立 portal_vocabulary 裡的關鍵詞表,再由 DisplayList 來取值。

設定 icon 的工作,慣例上是在 browser 目錄裡進行,先把 mytype_icon.gif 圖檔放在 browser 目錄,再編輯 browser/configure.zcml
<browser:resource
name="mytype_icon.gif"
image="mytypee_icon.gif"
/>
profiles/default/types/mytype.xml:
<property name="content_icon">++resource++mytype_icon.gif</property>

Purge

profiles/defaults/types.xml purge=False

安裝 plone.app.event 時,會以 Dexterity 取代 Folder 型別

Customizing Form Behavior

Modify View or Edit templates

http://plone.org/documentation/kb/easy-forms-with-plone3

Icon

設定 icon 的常見方式,先把 mytype_icon.gif 圖檔放在 browser 目錄,再編輯 browser/configure.zcml 內容:

<browser:resource
        name="mytype_icon.gif"
        image="mytypee_icon.gif"
        />

同時要編輯 profiles/default/types/mytype.xml 內容:

<property name="content_icon">++resource++mytype_icon.gif</property>

除了 http://plone.org/documentation/manual/plone-community-developer-documentation/troubleshooting/exceptions 提到 RuntimeError 之外,因為少了 storage=atapi.AnnotationStorage(),或用了 storage=atapi.AnnotationStorage(migration),同樣造成 maximum recursion depth exceeded 錯誤。

AttributeError: getAllowedContentTypes

這個錯誤發生的情況之一,是誤用 StringField 和 RichWidget 造成。

TypeError: object.__new__() takes no parameters

* Module Products.Archetypes.Field, line 461, in Vocabulary
TypeError: object.__new__() takes no parameters

vocabularies.zcml 該使用 ooxxFactory 卻只使用 ooxx

WARNING OFS.Uninstalled Could not import class 'MyType' from module 'my.atcontent.content.mytype'

Go ZMI check folder contents

favicon

http://plone.org/documentation/manual/theme-reference/elements/hiddenelements/plone.links.favicon

WARNING Init Class my.atcontent.content.mytype.MyType has a security declaration for nonexistent method 'some_key'

http://osdir.com/ml/web.zope.z3base.five/2007-04/msg00023.html

TypeError: ('Could not adapt', <persistent broken my.atcontent.portlets.quickuploadportlet.Assignment instance '\x00\x00\x00\x00\x00\x1a\xca1'>, <InterfaceClass plone.portlets.interfaces.IPortletAssignmentSettings>)

https://dev.plone.org/ticket/7375

AttributeError: 'list' object has no attribute 'encode'

http://plone.org/products/atsuccessstory/issues/1

collective.fingerpointing: zope.lifecycleevent.IObjectCreatedEvent is fired multiple times for archetypes. we'd be better off using Products.Archetypes.interfaces.IObjectInitializedEvent

Change ID Programatically

Add Content Programmatically

explicit IATUnifiedFolder implementation can be removed

Show the user who made the last modification on a document in Plone

Content Type Conversion Script

ATSchemaEditorNG based Generic Plone Content Migrate Non-Folderish to Folderish Types

Using Archetypes in Plone5

archetypes.referencebrowserwidget needs plone.app.form, needs five.formlib and Products.CMFDefault

Related content
Dexterity QuickStart