2014/07/30

Collection Group By View

A Collection in Plone works much like a report or query does in a database. Based on a set of Criteria such as: content types, dates, or keywords, you can search items and display them in a variety of dynamic ways. By default, there are Standard View, Summary View, Full View, Tabular View and Thumbnail View. What I need is to add an Aggregate View (like Group By). Here is how:

First, create a view.py file in the browser folder, where you define the View class. In my case, the results need to be grouped by category field values. Now I get a dictionary like {'category1': [<plone.app.contentlisting.catalog.CatalogContentListingObject instance at /mysite/myfolder/101-c-1>, <plone.app.contentlisting.catalog.CatalogContentListingObject instance at /mysite/myfolder/101-c-2>, <plone.app.contentlisting.catalog.CatalogContentListingObject instance at /mysite/myfolder/101-c-3>], 'category2': [...]}. With TAL nested loop in the aggregate_view.pt template, we can display the result. Finally register this browser view.

Note that this tip might suit only for collections of limited items. There should be much room for performance improvement. For those who are interested in the details how Collection works, see reference at plone.app.contenttypes/behaviors/collection.py

2014/06/25

Install Plone on Codio.com

This is a follow-up for Install Plone in under 5 minutes on Codio.com by David Bain, except Plone 4.3.3 standalone installation is used here.

Codio.com 提供線上開發環境,透過瀏覽器就能操作 Linux 系統,效能相當不錯。

臨時需要乾淨的開發或測試環境嗎? 可試看看 Codio 是否滿足需求。已知沒有 sudo 權限,透過 Box Parts 可以擴充程式語言、資料庫、函式庫等功能,例如 dropbox 就有支援,在持續改進的情況下,說不定教學環境也可採用它。

最簡單的測試方式,是建立一個 Empty Project 的範本。

再從 Tools 選單點選 Terminal 來打開命令介面。

想要安裝 Plone 4.3.3 環境? 在 Terminal 裡執行下列指令:

curl -L http://goo.gl/drKJp8 | bash

在 Project 選單點選 Box Info 可以查詢服務連線的資訊,存取網址的技巧,可參考 Non-Standard Port 的規則說明。或是查看 ChangeLog 了解最新版的支援狀況。

查詢 Package 狀況的範例:

$ parts search python                                                                                                               
apache2_mod_wsgi (3.4)
googleappengine (1.9.5)
jython (2.5.3)
pip (1.5.4-2)
pypy (2.2.1)
python2 (2.7.6-2)
python3 (3.4.1)
setuptools (2.2-2)
swig (2.0.11)
virtualenv (1.11.4-2)

2014/06/07

PyCon APAC 2014 Review

很多過去的事,原本以為只是生命裡的點,你不知道它們會連成怎樣的線,直到機緣成熟,才得以窺見故事的輪廓。

第一年辦 PyCon 時,力邀 Drake 參加,那時彼此不認識,為了介紹這是怎樣的活動,一起吃飯聊天,意外發現我們有哪些共同的朋友。今年,Drake 負責 PyCon APAC 2014 贊助工作,貢獻良多。

PyCon Taiwan 之前的社群聚會,最早包括 2004年 Song 籌辦的 PyZope 活動,還記得的人不多,後來 Thinker 號召的 PycTW 形塑定期活動的基礎,特別是 2011年,活動當天遇到颱風,會眾展現風雨無阻的意志,讓 Yung-Yu 有信心召喚蟒眾啟動定期的大型研討會。台灣年會規模遞增,算是站穩腳步,今年 Yung-Yu 籌畫了專屬的 SciPy 議程,還有 APAC Community Panel,就算三年前能預見這些事,實際看著它們發生,還是有種超現實的感覺。

2000年初學 Python,一見鍾情,雖然懂得很少,還是迫不急待在雜誌上撰文介紹。Tim 那時候是讀者,現在結合 Python 技術開公司,今年成為 PyCon APAC 2014 主席。能由 Mosky 獻花慶生,相信 Tim 那一刻已經忘卻籌備的辛勞。

1995年 Tom 小學剛畢業,學了 Linux 架 BBS 站,2004年學了 Python,現在已經成為全端工程師,最新目標是投入數位音樂表演,今年的閃電秀裡,Tom 展現了創意實作的成果。

PyCon APAC 2014 亮點太多了 [1][2][3],每年規模成長的情況下,號稱這是史上最棒的年會,殆無疑義。

期待明年更棒的年會之餘,別忘了嚮應 Jessica 的呼籲,來年之內為 Python 社群的成長,做一件新鮮事!

2014/06/06

Title Customization Based On URLs

Plone 網站的 <title /> 內容值,預設是由 Content Title 和 Site Title 合併而成,所以瀏覽首頁時,會看到類似「首頁 -- 我的網站」字樣。我想要把結果改成「我的網站」,方法不止一種,最簡單的方法如下:

到 ZMI portal_view_customization 找到 plone.htmlhead.title,它負責 <title /> 的顯示工作。

定義 is_home 變數,判斷網頁位置是否在首頁。

<tal:block define="state context/@@plone_context_state;
 is_home python:state.is_portal_root() and state.is_default_page()">
<title tal:condition="python: not is_home" tal:content="structure view/site_title">Site Title</title>
<title tal:condition="python: is_home" tal:content="string:我的網站">Site Title</title>
</tal:block>

以上方法只需要編輯 template 內容,不必修改檔案系統裡的 view 程式碼。

2014/06/05

Front Page Editing Made Easy

首頁給人的第一印象,很重要,對 Web Master 而言,他們還在意能否輕易地編輯首頁內容。

長久以來,Plone 有很多模組都想處理這個問題,但改善有限,直到 collective.cover 模組問世,才讓人眼睛一亮。有圖有真相? 先看影片介紹,還有這個範例網站 http://www.vtv.gob.ve/

在實作上,它利用 Cover Content Type 來儲存 Layout 資訊,你可以選用預設 Layout,或是透過 Layout tab 自訂細節。每個小格子被稱為 Tile,它可以再被指派 Carousel、Collection、Embed、RichText 等不同功能。

2014/05/27

plone.i18n Configuration

Plone 多國語文支援,相當成熟,想要讓網站支援中英文訊息介面,下列是基本的設定技巧:

在 ZMI 的 portal_languages 先勾選 Allow combined language codes like de_DE or en_UK. 並儲存,再從 Allowd Languages 欄位設定要支援的語系值,例如同時要支援「中文」和「英文」,可以先點選 Chinese (Taiwan) (zh-tw) 項目,再按住 Ctrl 鍵,點選 English (en) 並儲存,再從 Default Language 欄位選擇預設語系,例如 Chinese (Taiwan) 就行。

Negotiation Scheme 欄位,通常只需要勾選 Use cookie for manual override. 就行。

預設顯示的訊息文字是「繁體中文(臺灣)」,如果想要修改,可以到 plone.i18n/locales/languages.py 檔案,找到下列的設定內容:

u'zh-tw' : {u'name' : 'Chinese (Taiwan)',
 u'native' : '繁體中文(臺灣)',
 u'flag' : u'/++resource++country-flags/tw.gif'},

2014/05/25

Support Our Computer Teachers

"What will happen if users can program their own computer?" This is the question asked in Computer Programming for Everybody (CP4E). Recent developments in computer and communication hardware have given many people access to powerful computers, in the form of desktops, laptops, and mobile devices. It is time to give these users more control over their computers through education and supporting software. If users have a general understanding of computers at the level of software design and implementation, this will cause a massive surge in productivity and creativity, with a far-ranging impact that can barely be anticipated or imagined.

Why Learning CS?

Computer science is a foundational field for every 21st century career or field of study. Learning the basics of computer science prepares students for a world that is increasingly dominated by technology. Research shows that students who study computer science also perform better at math. Besides, computer science is where the jobs are.

Why Python?

A key goal for Education is to guide the learners to solve problems and discover solutions. This principal also applies to computer education (CS classes). Every programming language can be used to teach logical thinking. Why and how can Python stand out in such a language war?

Consider this analogy: When running an international conference, a shared language is required. What should it be and how should it be selected? To efficiently communicate in one language, the ideal choice is one with the lowest learning curve and translation cost. Just as how English has become an international language, I believe Python will become the dominant language in the programming world.

No, Python should not be the only language to learn. For a CS professional, it's common to learn various languages, including C and Java. However, Python is still one of the best languages for beginners and those whose limited resources only provide access to one language.

Give Teachers A Community

Ok, I want to learn Python, how can I find teachers or trainers? There are more and more online Python courses, just to name some, including Codecademy, Coursera, Udacity and LearnStreet. However, there are few, if any, teachers in schools providing courses based on Python. Besides, CS teachers are usually all alone.

Luckily, a global push for more computer science in classrooms is starting to bear fruit. In response to Jessica McKellar's call to action, "Do One Thing Before the next year's PyCon", a group of Taiwan folks run a meetup to support CS teachers, after the PyCon APAC 2014 Education Panel and BoF.

Education is a tree shaking another tree, another cloud to promote a cloud, a soul awakening another soul. -- Karl Theodor Jaspers

Note: thanks to Ken Hu's help on editing this snippet.