Data Access Tips
建立新的 Metadata 或 Index 欄位可能是沒必要的
像 Creators、Contributors 使用 Tuple 型別儲存,變數是 creators 或 contributors,參考 plone.app.dexterity/behaviors/metadata.py 和 plone.app.dexterity/behaviors/tests/test_metadata.py 可以了解細節。像 Subjects / Tags 雖然同樣使用 Tuple 型別,但變數要用 subject 而非 subjects,參考 plone.dexterity/content.py 可看到細節。
更新 Author 名稱的範例:
>>> item = app.mysite['front-page']
>>> item.creators
(u'admin',)
>>> item.creators = (u'marr',)
根據系統 C 函式庫支援的 strftime 格式,Python 可能透過 %-d 來取得「沒有 0 帶頭」的時間值。
當屬性值未被儲存之前,Dexterity 使用 __getattr__ 來確保能夠讀取 schema 的預設值。參考 簡報檔 p33 說明。
Get the Context for a Sub-folder with a Hyphen in the Shortname
使用 obj.context.__parent__ 來存取項目所屬目錄的資訊。
Timestamps TimeZone
why DateTime Messy 時間資料是重要的資訊,舊版使用 Zope2 DateTime 物件格式,可能出現 Localized TimeZone 不一致的問題,新版使用 datetime 格式。希望有簡單的設定方式。GSoC 2018 構想 2018/01 討論
import datetime time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S") fn = "filename{}.txt".format(time)
dexterity creation_date plone.app.event IEventAccessor data_postprocessing 是處理 open_end 和 whole_day 資訊,後來從程式碼裡移除了。新版 plone.app.event 影響 Solgema.fullcalendar 的 icalexport 功能。
import DateTime app.mysite.news['my-item'].creation_date = DateTime.DateTime('2013/01/01 09:00:00 GMT+8')
creation_date 調整之後,created() 的結果也會跟著調整。
>>> item = app.mysite.events['my-item'] >>> type(item.start) <type 'datetime.datetime'> >>> type(item.creation_date) <class 'DateTime.DateTime.DateTime'> >>> item.modification_date DateTime('2016/07/01 03:26:52.396626 UTC') >>> item.creation_date DateTime('2016/06/30 00:35:23.674693 UTC') >>> item.start datetime.datetime(2016, 3, 23, 16, 0, tzinfo=<UTC>) >>> item.end datetime.datetime(2016, 3, 24, 15, 59, 59, tzinfo=<UTC>) >>> item.whole_day True >>> item.open_end True
上述 item.Start 或 item.StartDate 都未定義。時區數值 UTC 帶來的困擾,設定 Default Timezone 的方法,轉換時區的方法。
if behavior.whole_day: start = dt_start_of_day(start) 從 from plone.app.event.base import dt_start_of_day 得知 return dt.replace(hour=0, minute=0, second=0, microsecond=0) 試著把 hour 指定為 10
from datetime import datetime from pytz import timezone fmt = "%Y-%m-%d %H:%M:%S %Z%z" print item.start.strftime(fmt) print item.start.astimezone(timezone('Asia/Taipei'))
datetime 預設不處理時區問題,交由第三方函式庫 pytz 來轉換時區,用 astimezone 來轉換結果。
可以執行 app.mysite.news['my-item'].reindexObject() 和 transaction.commit() 確保變更結果實際有存進 ZODB 裡。
import transaction transaction.commit() app._p_jar.sync()
collective.solr: 如果 iso8601date 被輸入 string 內容,試著轉成 DateTime,以維護 Collection 相容性。
Difference in GMT When Creating DateTime Object From String With "-" or "/" Separators
from datetime import date today = date.today() today.timetuple() # time.struct_time(tm_year=2015, tm_mon=1, tm_mday=3, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=3, tm_isdst=-1) today.timetuple()[-2]
datetime.datetime(2009, 8, 2, 16, 0, tzinfo=<UTC>) 使用 mydt.isoformat() 會回傳 '2009-08-02T16:00:00+00:00' 字串。透過 data.start.isoformat()[0:10] 可以再傳給 toLocalizedTime() 轉成在地化日期。
TimeError: The time is beyond the range of this Python implementation.
NotFound Error Localized DateTime Format
plone.dexterity/utils.py datify() Get a DateTime object from a string (or anything parsable by DateTime, a datetime.date, a datetime.datetime)
C# DateTime.today 在測試情境會有日期不同的狀況
Geo Coordinate
getObjSize format_size
format_size 移進 utils 並改名成 human_readable_size
Traverse
讓物件具備 Traversal 功能,方法很多,包括使用 __getitem__()、IPublishTraverse、__bobo_traverse__() 等。
collective.folderishtraverse: 讀取目錄裡的項目
Custom Traversing with IPublishTraverse Products.CMFPlone/browser/author.py
Repeated URL Name Due to Acquisition of DefaultPublishTraverse