绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
plone中zodb数据库的分析
2022-05-05 16:31:53

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

 

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

 

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

 

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

 

首先找到根目录

  1. from ZEO.ClientStorage import ClientStorage
  2. from ZODB import DB
  3. from myzodb import MyZODB, transaction
  4. class MyRemoteZODB(object):
  5. def __init__(self, server, port):
  6. server_and_port = (server, port)
  7. self.storage = ClientStorage(server_and_port)
  8. self.db = DB(self.storage)
  9. self.connection = self.db.open()
  10. self.dbroot = self.connection.root()
  11. def close(self):
  12. self.connection.close()
  13. self.db.close()
  14. self.storage.close()
  15. mydb = MyRemoteZODB('localhost', 8100)
  16. dbroot = mydb.dbroot

 

 

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

  1. >>>print dbroot.keys()
  2. ['Application']
  3. >>>application = dbroot['Application']
  4. >>>print application.keys()
  5. ['Control_Panel', 'temp_folder', 'session_data_manager', 'browser_id_manager', 'error_log', 'favicon.ico', 'standard_error_message', 'index_html', 'virtual_hosting', 'MyPlone', 'acl_users']
  6. >>>myplone = application['MyPlone']
  7. >>>print myplone.keys()
  8. ['portal_setup', 'MailHost', 'caching_policy_manager', 'content_type_registry', 'error_log',...]

 

 

上面的代码相当于:

  1. >>>ls dbroot
  2. Application
  3. >>>cd Application
  4. >>>ls
  5. 'Control_Panel', 'temp_folder', 'session_data_manager', 'browser_id_manager', 'error_log', 'favicon.ico', 'standard_error_message', 'index_html', 'virtual_hosting', 'MyPlone', 'acl_users'
  6. >>>cd MyPlone
  7. >>>ls
  8. 'portal_setup', 'MailHost', 'caching_policy_manager', 'content_type_registry', 'error_log',...

 

 

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

 

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

 

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

  1. <html>
  2. <head>
  3. <title tal:content="template/title">The title</title>
  4. <meta http-equiv="content-type" content="text/html;charset=utf-8">
  5. </head>
  6. <body>
  7. <h2><span tal:replace="here/title_or_id">content title or id</span>
  8. <span tal:condition="template/title"
  9. tal:replace="template/title">optional template title</span></h2>
  10. This is Page Template <em tal:content="template/id">template id</em>.
  11. </body>
  12. </html>

 

 

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

 

尝试步骤:

 

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

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

 

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

  1. >>>mytest = myplone['mytest']
  2. >>>print mytest
  3. <ZopePageTemplate at mytest>
  4. >>>print mytest.read()
  5. <html>
  6. <head>
  7. <title tal:content="template/title">The title</title>
  8. <meta http-equiv="content-type" content="text/html;charset=utf-8">
  9. </head>
  10. <body>
  11. <h2><span tal:replace="here/title_or_id">content title or id</span>
  12. <span tal:condition="template/title"
  13. tal:replace="template/title">optional template title</span></h2>
  14. This is Page Template <em tal:content="template/id">template id</em>.
  15. </body>
  16. </html>
  17. >>>mytest.write("<html></html>")
  18. >>>transaction.commit()

 

 

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

<html></html>

 

分享好友

分享这个小栈给你的朋友们,一起进步吧。

ZODB
创建时间:2022-04-18 13:36:36
ZODB
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

技术专家

查看更多
  • 飘絮絮絮丶
    专家
戳我,来吐槽~