wiki.zope.jp
ZSQLMethodDTMLTree
   
RecentChanges WikiHelp WikiPractice JumpSearch

ZSQLMethodDTMLTree

データベース内の情報をツリー表示する方法。 ページテンプレートPageTemplateでも表示可能です。


DB の データをツリー表示する方法、昨日かなりてこずったので、忘れても大 丈夫なようにメモ、メモ....Wiki にも ペーストしておこうっと...

かなり長文です

dtml-tree を使って、データベース内の情報を表示するには。まずは、DB に 階層構造のテーブルを作ります。:

  親                           子
  bigtheme <- smalltheme <- theme

試しにgadfly を使ってみました。:

  create bigtheme(
  id            integer
  name          varchar
  )

  create smalltheme(
  parentid      integer #bigtheme の id を入れる
  id            integer
  name          varchar
  )

  create theme(
  parentid      integer #smalltheme の id を入れる
  id            integer
  name          varchar
  )

3つのテーブル情報を取得する SQL Method 作成:

  getbigtheme

        select * from bigtheme

  getsmalltheme
  引数 : parentid

        select * from smalltheme
        where <dtml-sqltest parentid type=int>

  gettheme
  引数 : parentid

        select * from theme
        where <dtml-sqltest parentid type=int>

それぞれの DB 情報をオブジェクトとして取得したいので、ZSQLMethod の Advanced タブに移動して、ClassName と ClassFile? を設定する。

設定するまえに、設定するためのファイルを書かないといけないか。

Zope を インストールしたディレクトリの Extensions に以下のようなファイ ルを入れる:

  ===File /usr/local/zope2.5.1/Extensions/Theme.py============
  class BigTheme:

    def tpId(self):
      """
      ZTUtils.Tree.py で なぜか Id は設定できないので
      絶対に名前を tpId にしておく必要あり。
      dtml-tree のときもこのほうが面倒じゃないか...
      """
      return self.id

    def title(self):
      return self.name

    def icon(self):
      return 'Big'

    def getChild(self):
      """get data from children table"""
      record=self.getsmalltheme(parentid=self.id)
      if record:
        seq=[]
        for i in record:
          seq.append(i)
        return seq

  class SmallTheme:

    def tpId(self):
      """
      ZTUtils.Tree.py で なぜか Id は設定できないので
      絶対に名前を tpId にしておく必要あり。
      dtml-tree のときもこのほうが面倒じゃないか...
      """
      return self.id

    def icon(self):
      return 'small'

    def title(self):
      return self.name

    def getChild(self):
      """get data from children table"""
      record=self.gettheme(parentid=self.id)
      if record:
        seq=[]
        for i in record:
          seq.append(i)
        return seq

  class Theme:

    def tpId(self):
      """
      ZTUtils.Tree.py で なぜか Id は設定できないので
      絶対に名前を tpId にしておく必要あり。
      dtml-tree のときもこのほうが面倒じゃないか...
      """
      return self.id

    def icon(self):
      return 'small'

    def title(self):
      return self.name

    def getChild(self):
      """get data from children table"""
      return []
  ============================================================

つぎに Advanced タブで、それぞれのテーブルにそれぞれのクラスを設定:

  =====================================
  getbigtheme
        class name : BigTheme
        class file : Theme

  getsmalltheme
        class name : SmallTheme
        class file : Theme

  gettheme
        class name : Theme
        class file : Theme
  =====================================

次に、同フォルダに、getChild という名前のPython スクリプトを作る。:

  #=====================================
  ## Script (Python) "getChild"
  ##bind container=container
  ##bind context=context
  ##bind namespace=
  ##bind script=script
  ##bind subpath=traverse_subpath
  ##parameters=
  ##title=
  ##
  record=context.getbigtheme()
  if record:
    seq=[]
    for i in record:
      seq.append(i)
    return seq
  #=====================================

  さて、これで鋭い人なら気付いたかもしれませんが、
  同フォルダから見て getChild メソッド で繋がった一つのツリーができてい
  ますよね。::

  Folder.getChild
        getbigtheme.getChild
                getsmalltheme.getChild
                        gettheme.getChild(帰り値は[]なので、終わり)

ということで、同フォルダの中で:

  <dtml-tree branches="getChild">
  <dtml-var title>
  </dtml-tree>

というDTMLを作れば、見事 DB 内のデータがツリーで表示されます!

最後に dtml-tree とまったく同じ表示を ページテンプレートで作るための方 法を。

まず、PythonScript を作ります。こんな感じ。 ZTUtils? のなかの SimpleTreeMaker モジュールを使うんです。 ZTUtils? は Batch モジュールなどもあり、DTML でできることを ページテン プレートで行えるようにするためのパッケージです。:

  #=====================================
  ## Script (Python) "dbtree"
  ##bind container=container
  ##bind context=context
  ##bind namespace=
  ##bind script=script
  ##bind subpath=traverse_subpath
  ##parameters=tree_root
  ##title=Standard Tree
  ##
  from ZTUtils import SimpleTreeMaker

  REQUEST=context.REQUEST

  tm = SimpleTreeMaker('db_tree')
  def getKids(object):
        return object.getChild()
  tm.setChildAccess(function=getKids)

  tree, rows = tm.cookieTree(tree_root)
  rows.pop(0)
  return {'root': tree, 'rows': rows}
  #=====================================

最後に、上で作ったPythonScript を呼んで、ツリー表示するための ページテ ンプレートです。:

  <html>
  <head>
  <title tal:content="template/title">The title</title>
  </head>
  <body tal:define="t python:here.dbtree(here); height t/root/height">
  <table cellspacing="0" border="0">
  <tr tal:repeat="row t/rows">
  <td tal:define="indent python:row.depth - 1"
          tal:condition="indent"
          tal:attributes="colspan indent" colspan="1"></td>
  <td width="16" tal:define="rlink row/branch">
  <a tal:condition="rlink"
           tal:attributes="name row/id;href rlink/link"
           tal:content="structure rlink/img" href name><img src="/p_/pl"></a>
  </td>
  <td tal:attributes="colspan python:height-row.depth"
          tal:define="obj nocall:row/object;"
          colspan="1" width="99%">
  <span tal:content="obj/title" href="">Title</span>
  </td>
  </tr>
  </table>


subtopics:

Last edited Fri, 15 Sep 2006 08:37:33 +0900 Edit this page