お知らせ表示テンプレートの作成 [Plone3.3]

お知らせ表示として「日付+タイトル」を表示する「ENG TOPIC VIEW」と、「日付+本文」を表示する「ENG BLOG VIEW」を定義する。

ENG TOPIC VIEW

Topicタイプのビューとして ENG TOPIC VIEW を定義する

工学研究科のお知らせ表示用に、Topicタイプのビューを定義する。スキンフォルダ内にテンプレートを eng_topic_view として作成する。タイトルは「ENG TOPIC VIEW」とする。

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
      lang="en"
      metal:use-macro="here/main_template/macros/master"
      i18n:domain="plone">

<body>
<div metal:fill-slot="main">
<metal:main_macro define-macro="main" tal:condition="here/listCriteria | nothing">
    <metal:body define-macro="body_macro"
                tal:define="kssClassesView context/@@kss_field_decorator_view;
                            getKssClasses nocall:kssClassesView/getKssClassesInlineEditable;
                            templateId template/getId">

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

        <h1 class="documentFirstHeading"> 
            <metal:field use-macro="python:here.widget('title', mode='view')">
            Title
            </metal:field>
        </h1>

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

        <p class="documentDescription">
            <metal:field use-macro="python:here.widget('description', mode='view')">
            Description
            </metal:field>
        </p>

        <div tal:replace="structure provider:plone.abovecontentbody" />
 
        <metal:listingmacro define-macro="listing">
        <tal:topiccontents define="b_size b_size | python:50;
                                   topicContents python:here.queryCatalog(batch=True, b_size=b_size);
                                   batch topicContents;">
          
            <div metal:define-macro="text-field-view"
                 id="parent-fieldname-text" class="stx"
                 tal:define="kss_class python:getKssClasses('text',
                             templateId=templateId, macro='text-field-view');
                             text here/getText|nothing"
                 tal:condition="text"
                 tal:attributes="class python:test(here.Format() in ('text/structured',
                                                       'text/x-rst', ), 'stx' + kss_class, 'plain' + kss_class)">
                <div metal:define-slot="inside" tal:replace="structure text">The body</div>
            </div>

            <tal:listing condition="topicContents">

            <!-- Navigation -->
            <div metal:use-macro="here/batch_macros/macros/navigation" />

            <div class="newsline">
                <tal:items tal:repeat="obj topicContents">
                <div class="tline">
                <div tal:define="effective_date obj/EffectiveDate;
                                 modification_date obj/ModificationDate;
                                 effective_date python:test(effective_date != 'None', effective_date, modification_date);
                                 effective_date python:here.toLocalizedTime(effective_date)"
                     class="date">
                <span tal:replace="string:$effective_date">2007/11/11</span>
                </div>
                <div tal:attributes="class string:ctype-${obj/portal_type};">
                  <a tal:attributes="href string:${obj/getURL}/view;">
                     <tal:title tal:content="obj/pretty_title_or_id">title</tal:title>
                  </a>
                </div>
                </div>
                </tal:items>
            </div>

            </tal:listing>

            <p class="discreet"
               tal:condition="python: not topicContents and here.listCriteria()"
               tal:content="python:test(language=='ja',
                   u'\u63b2\u8f09\u8a18\u4e8b\u306f\u3042\u308a\u307e\u305b\u3093\u3002',
                   'There is currently no item to display.')">
            </p>

            <p class="discreet"
               tal:condition="not: here/listCriteria"
               i18n:domain="atcontenttypes"
               i18n:translate="description_no_criteria_in_topic">
                There are currently no criteria on which to search.
                Please add them using the 'criteria' tab.
            </p>

            <!-- Navigation -->
            <div metal:use-macro="here/batch_macros/macros/navigation" />

        </tal:topiccontents>
        </metal:listingmacro>
    </metal:body>

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

</metal:main_macro>
</div>
</body>
</html>

 

お知らせ一覧のイベント表示対応

お知らせ一覧表示テンプレートでイベントの場合は「[開催日] 開催」と表示できるようにする。工学研究科のお知らせ表示用 eng_topic_view を次のように修正する。

  <a tal:attributes="href string:${obj/getURL}/view;">
     <tal:title tal:content="obj/pretty_title_or_id">title</tal:title>
  </a>
+ <tal:event tal:condition="python:obj.Type=='Event'">
+     <span class="event_date">
+     <tal:date tal:define="event_start python:toLocalizedTime(obj.start, long_format=0);
+               event_end python:toLocalizedTime(obj.end, long_format=0);"
+               tal:replace="python:test(event_start==event_end, event_start, event_start+'-'+event_end)" />
+    <tal:title tal:replace="python:test(context.Language()=='ja', u'\u958b\u50ac', u'')">
+         title</tal:title></span>
+ </tal:event>

お知らせ一覧のキーワードタグ対応

お知らせ一覧表示テンプレートに、記事のカテゴリに応じた class 属性を出力して、カテゴリ別アイコンを表示できるようにする。

convert_categoryスクリプトの作成

カテゴリは日本語で作成しているため、class 属性に付与するためには英数字にする必要がある。変換スクリプトとしてスキンフォルダに convert_category という名前で Python Script を作成する。key_dic に変換するカテゴリと、出力する文字列一覧の対応を定義しておく。また no_key にはキーワードが設定されていない場合に出力する文字列を定義しておく。

keywords = context.Subject()

# カテゴリ未定義
no_key = 'Nocategory'

# カテゴリ変換
key_dic = { 'お知らせ':'TopicsGeneral',
            '入学案内(学部)':'TopicsAdmission',
            '入学案内(大学院)':'TopicsAdmission',
            '学生':'TopicsStudent',
            '研究':'TopicsResearch',
            '国際交流':'TopicsInternational' }
try:
    if context.Type() == 'Event':
        return str('TopicsEvent')
    for keyword in keywords:
        if keyword in key_dic.keys():
            return str(key_dic[keyword])
    return str(no_key)
except:
    return str(no_key)

eng_topic_viewの修正

工学研究科のお知らせ表示用 eng_topic_view を修正して上記の変換スクリプトを呼び出し、class 属性に付与されるようにする。

- <div tal:attributes="class string:ctype-${obj/portal_type};">
+ <div tal:define="cat python:obj.getObject().convert_category"
+      tal:attributes="class string:ctype-${cat};">

Topicのビューで選択できるものを指定する

フォルダのビューで選択できるものを指定する。上で作成した、eng_topic_view も設定できるようにする。portal_types/Topic を開き Available view methods に次の行を追加する。

eng_topic_view

 

ENG BLOG VIEW

各ページ内のURL変換スクリプトを定義する

ブログ表示するともとのページと Base URL が変更になるため、変換スクリプトを定義する。スキンフォルダに Python Script を transformRelatedURLs という名前で作成する。

# this is a quick hack
a_splitted = htmltext.split(' href="')
result = a_splitted[0]

for href in a_splitted[1:]:
    if not href.startswith('http') and not href.startswith('/') and not href.startswith('file://') and not href.startswith('mailto:'):
        result += ' href="' + base_url + '/../' + href
    else:
        result += ' href="' + href

htmltext = result
img_splitted = htmltext.split(' src="')
result = img_splitted[0]

for src in img_splitted[1:]:
    if not src.startswith('http') and not src.startswith('/') and not src.startswith('file://'):
        result += ' src="' + base_url + '/../' + src
    else:
        result += ' src="' + src
htmltext = result

return result

また Parameter List は次のように指定しておく。

Parameter List : htmltext, base_url

Topicタイプのビューとして ENG BLOG VIEW を定義する

工学研究科のお知らせ表示用に、Topicタイプのビューを定義する。スキンフォルダ内にテンプレートを eng_blog_view として作成する。タイトルは「ENG BLOG VIEW」とする。

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
      lang="en"
      metal:use-macro="here/main_template/macros/master"
      i18n:domain="plone">

<body>
<div metal:fill-slot="main">
<metal:main_macro define-macro="main" tal:condition="here/listCriteria | nothing">
    <metal:body define-macro="body_macro"
                tal:define="kssClassesView context/@@kss_field_decorator_view;
                            getKssClasses nocall:kssClassesView/getKssClassesInlineEditable;
                            templateId template/getId">

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

        <h1 class="documentFirstHeading"> 
            <metal:field use-macro="python:here.widget('title', mode='view')">
            Title
            </metal:field>
        </h1>

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

        <p class="documentDescription">
            <metal:field use-macro="python:here.widget('description', mode='view')">
            Description
            </metal:field>
        </p>

        <div tal:replace="structure provider:plone.abovecontentbody" />
 
        <metal:listingmacro define-macro="listing">
        <tal:topiccontents define="b_size b_size | python:20;
                                   topicContents python:here.queryCatalog(batch=True, b_size=b_size);
                                   batch topicContents;">
          
            <div metal:define-macro="text-field-view"
                 id="parent-fieldname-text" class="stx"
                 tal:define="kss_class python:getKssClasses('text',
                             templateId=templateId, macro='text-field-view');
                             text here/getText|nothing"
                 tal:condition="text"
                 tal:attributes="class python:test(here.Format() in ('text/structured',
                                                       'text/x-rst', ), 'stx' + kss_class, 'plain' + kss_class)">
                <div metal:define-slot="inside" tal:replace="structure text">The body</div>
            </div>

            <tal:listing condition="topicContents">

            <!-- Navigation -->
            <div metal:use-macro="here/batch_macros/macros/navigation" />

            <div class="newsline">
                <tal:items tal:repeat="obj topicContents">
                <div class="bline">
                <div tal:define="effective_date obj/EffectiveDate;
                                 modification_date obj/ModificationDate;
                                 effective_date python:test(effective_date != 'None', effective_date, modification_date);
                                 effective_date python:here.toLocalizedTime(effective_date)"
                      class="date">
                <span tal:replace="string:$effective_date">2007/11/11</span>
                </div>
                <div tal:define="translation python:obj.getObject();
                                 base_url python:obj.getObject().absolute_url();"
                     tal:attributes="class string:ctype-${obj/portal_type};">
                  <div class="blog_title">
                    <a tal:attributes="href string:${obj/getURL}/view;">
                      <tal:title tal:content="obj/pretty_title_or_id">title</tal:title>
                    </a>
                  </div>
                  <div tal:replace="structure python:here.transformRelatedURLs(translation.CookedBody(stx_level=2), base_url)" />
                </div>
                </div>
                </tal:items>
            </div>

            </tal:listing>

            <p class="discreet"
               tal:condition="python: not topicContents and here.listCriteria()"
               tal:content="python:test(language=='ja',
                   u'\u63b2\u8f09\u8a18\u4e8b\u306f\u3042\u308a\u307e\u305b\u3093\u3002',
                   'There is currently no item to display.')">
            </p>

            <p class="discreet"
               tal:condition="not: here/listCriteria"
               i18n:domain="atcontenttypes"
               i18n:translate="description_no_criteria_in_topic">
                There are currently no criteria on which to search.
                Please add them using the 'criteria' tab.
            </p>

            <!-- Navigation -->
            <div metal:use-macro="here/batch_macros/macros/navigation" />

        </tal:topiccontents>
        </metal:listingmacro>
    </metal:body>

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

</metal:main_macro>
</div>
</body>
</html>

Topicのビューで選択できるものを指定する

フォルダのビューで選択できるものを指定する。上で作成した、eng_blog_view も設定できるようにする。portal_types/Topic を開き Available view methods に次の行を追加する。

eng_blog_view