plone中zodb数据库的分析

目标:分析plone中zodb数据库,能通过直接操作数据库对plone进行设置和显示

有关zodb数据的操作可参考:http://xiaolin0199.iteye.com/blog/2019678

由于zodb中的数据是以层次结构存储的,就像一个一个文件夹一样,而plone就是一个大的文件夹,里面还嵌套着很多个文件夹,现在就是要看看这个大文件夹的数据结构

现在以本地Plone为例,通过ZEO去连接数据库,就像打开文件夹一样,一步一步的进入文件夹查看内容

首先找到根目录

from ZEO.ClientStorage import ClientStorage
from ZODB import DB
from myzodb import MyZODB, transaction

class MyRemoteZODB(object):
    def __init__(self, server, port):
        server_and_port = (server, port)
        self.storage = ClientStorage(server_and_port)
        self.db = DB(self.storage)
        self.connection = self.db.open()
        self.dbroot = self.connection.root()

    def close(self):
        self.connection.close()
        self.db.close()
        self.storage.close()

mydb = MyRemoteZODB('localhost', 8100)
dbroot = mydb.dbroot

上面的代码就是通过本地端口8100的zeo访问zodb,而dbroot就是根目录,接着我们打开这个根目录

>>>print dbroot.keys()
['Application']
>>>application = dbroot['Application']
>>>print application.keys()
['Control_Panel', 'temp_folder', 'session_data_manager', 'browser_id_manager', 'error_log', 'favicon.ico', 'standard_error_message', 'index_html', 'virtual_hosting', 'MyPlone', 'acl_users']
>>>myplone = application['MyPlone']
>>>print myplone.keys()
['portal_setup', 'MailHost', 'caching_policy_manager', 'content_type_registry', 'error_log',...]

上面的代码相当于:

>>>ls dbroot
Application
>>>cd Application
>>>ls
'Control_Panel', 'temp_folder', 'session_data_manager', 'browser_id_manager', 'error_log', 'favicon.ico', 'standard_error_message', 'index_html', 'virtual_hosting', 'MyPlone', 'acl_users'
>>>cd MyPlone
>>>ls
'portal_setup', 'MailHost', 'caching_policy_manager', 'content_type_registry', 'error_log',...

结构比较清楚,跟zmi后台显示的是对应的,但里面的内容虽然显示的是字符串,但其实都是一个一个zope定义的类,要弄清这些类的添加使用就比较困难了

现在对一个简单的类测试下:

先在zmi后台/MyPlone目录下创建一个Page Template,名为mytest,里面默认显示的代码是

<html>
  <head>
    <title tal:content="template/title">The title</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
  </head>
  <body>
    
    <h2><span tal:replace="here/title_or_id">content title or id</span>
        <span tal:condition="template/title"
              tal:replace="template/title">optional template title</span></h2>

    This is Page Template <em tal:content="template/id">template id</em>.
  </body>
</html>

现尝试通过直接操作zodb修改里面的代码

尝试步骤:

1.进入MyPlone,会发现刚创建的mytest

>>>myplone = application['MyPlone']
>>>print myplone.keys()
['portal_setup', 'MailHost', 'caching_policy_manager', 'content_type_registry', 'error_log',...'mytest']

2.赋值mytest,通过dir查看方法,发现有read和write的方法

>>>mytest = myplone['mytest']
>>>print mytest
<ZopePageTemplate at mytest>
>>>print mytest.read()
<html>
  <head>
    <title tal:content="template/title">The title</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
  </head>
  <body>
    
    <h2><span tal:replace="here/title_or_id">content title or id</span>
        <span tal:condition="template/title"
              tal:replace="template/title">optional template title</span></h2>

    This is Page Template <em tal:content="template/id">template id</em>.
  </body>
</html>
>>>mytest.write("<html></html>")
>>>transaction.commit()

3.最后在zmi后台再查看totest,发现代码已经改变了

<html></html>

猜你喜欢

转载自xiaolin0199.iteye.com/blog/2019680
今日推荐