跳转到主要内容

定义来自`zope.file`模块的文件和图像对象的WebDAV数据模型。

项目描述

============================
zope.file webdav集成
============================

>>> import z3c.etree
>>> import zope.event
>>> from zope.lifecycleevent import ObjectCreatedEvent
>>> from zope.file.file import File

>>> etree = z3c.etree.getEngine()

在根目录中创建一个名为`textfile.txt`的内容对象。

>>> f = File('text/plain', {'charset': 'ascii'})
>>> fp = f.open('w')
>>> fp.write('%s\n' %('x' * 10) * 5)
>>> fp.close()

发出`CreateObjectEvent`以生成最后修改和创建日期。

>>> zope.event.notify(ObjectCreatedEvent(f))
>>> getRootFolder()['testfile.txt'] = f


PROPFIND
========

查询文件对象的WebDAV数据模型。以下属性
目前构成了zope.file的数据模型

+ {DAV:}creationdate

+ {DAV:}displayname

+ {DAV:}getcontentlength

+ {DAV:}getcontenttype

+ {DAV:}getlastmodified

+ {DAV:}resourcetype

查询测试文件的全部属性。

>>> resp = webdav("""
... PROPFIND /testfile.txt HTTP/1.1
... """, handle_errors = False)

>>> print resp.getBody() #doctest:+XMLDATA,+ELLIPSIS
<multistatus xmlns="DAV:">
<response>
<href>https://127.0.0.1/testfile.txt</href>
<propstat>
<prop>
<creationdate>...</creationdate>
<displayname />
<getlastmodified>...</getlastmodified>
<getcontenttype>text/plain</getcontenttype>
<getcontentlength>55</getcontentlength>
<resourcetype />
</prop>
<status>HTTP/1.1 200 正常</status>
</propstat>
</response>
</multistatus>

在文件对象上尚未定义 `{DAV:}getetag` 和 `{DAV:}getcontentlanguage` 属性。
(目前)

>>> resp.getMSProperty('https://127.0.0.1/testfile.txt', '{DAV:}getetag')
Traceback (最后调用最接近)
...
KeyError: "'{DAV:}getetag' 属性未找到,资源为 https://127.0.0.1/testfile.txt (200)"
>>> resp.getMSProperty(
... 'https://127.0.0.1/testfile.txt', '{DAV:}getcontentlanguage')
Traceback (最后调用最接近)
...
KeyError: "'{DAV:}getcontentlanguage' 属性未找到,资源为 https://127.0.0.1/testfile.txt (200)"

PROPPATCH
=========

我们需要登录才能更新属性。

>>> reqbody = """<propertyupdate xmlns="DAV:">
... <set>
... <prop>
... <displayname>测试标题</displayname>
... </prop>
... </set>
... </propertyupdate>"""
>>> resp = webdav("""
... PROPPATCH /testfile.txt HTTP/1.1
... Content-Type: application/xml
... Content-Length: %d
...
... %s""" %(len(reqbody), reqbody))
>>> resp.getStatus()
401

现在更新文件的标题。

>>> resp = webdav("""
... PROPPATCH /testfile.txt HTTP/1.1
... Authorization: Basic mgr:mgrpw
... Content-Type: application/xml
... Content-Length: %d
...
... %s""" %(len(reqbody), reqbody), handle_errors = False)
>>> resp.getStatus()
207
>>> resp.getHeader('content-type')
'application/xml'
>>> print resp.getBody() #doctest:+XMLDATA
<multistatus xmlns="DAV:">
<response>
<href>https://127.0.0.1/testfile.txt</href>
<propstat>
<prop>
<displayname />
</prop>
<status>HTTP/1.1 200 正常</status>
</propstat>
</response>
</multistatus>

使用 PROPFIND 检索 displayname 属性并注意差异。

>>> reqbody = """<propfind xmlns="DAV:">
... <prop>
... <displayname />
... </prop>
... </propfind>
... """
>>> resp = webdav("""
... PROPFIND /testfile.txt HTTP/1.1
... Content-type: application/xml
... Content-Length: %d
...
... %s""" %(len(reqbody), reqbody), handle_errors = False)
>>> resp.getStatus()
207
>>> resp.getHeader('content-type')
'application/xml'
>>> print resp.getBody() #doctest:+XMLDATA
<multistatus xmlns="DAV:">
<response>
<href>https://127.0.0.1/testfile.txt</href>
<propstat>
<prop>
<displayname>测试标题</displayname>
</prop>
<status>HTTP/1.1 200 正常</status>
</propstat>
</response>
</multistatus>

The `{DAV:}getcontentlength` 属性是一个活动属性,因此我们
不能修改它。

>>> reqbody = """<propertyupdate xmlns="DAV:">
... <set>
... <prop>
... <getcontentlength>24</getcontentlength>
... </prop>
... </set>
... </propertyupdate>"""
>>> resp = webdav("""
... PROPPATCH /testfile.txt HTTP/1.1
... Authorization: Basic mgr:mgrpw
... Content-type: application/xml
... Content-length: %d
...
... %s""" %(len(reqbody), reqbody))
>>> resp.getStatus()
207
>>> resp.getHeader('content-type')
'application/xml'
>>> print resp.getBody() #doctest:+XMLDATA
<multistatus xmlns="DAV:">
<response>
<href>https://127.0.0.1/testfile.txt</href>
<propstat>
<prop>
<getcontentlength />
</prop>
<status>HTTP/1.1 403 禁止</status>
</propstat>
</response>
</multistatus>


不透明属性
=================

在我们的资源上设置两个已删除属性。

>>> reqbody = """<propertyupdate xmlns="DAV:">
... <set>
... <prop>
... <E:prop1 xmlns:E="examplens:">属性 1</E:prop1>
... <E:prop2 xmlns:E="examplens:">属性 2</E:prop2>
... </prop>
... </set>
... </propertyupdate>
... """
>>> resp = webdav("""
... PROPPATCH /testfile.txt HTTP/1.1
... Authorization: Basic mgr:mgrpw
... Content-type: application/xml
... Content-length: %d
...
... %s""" %(len(reqbody), reqbody), handle_errors = True)

>>> resp.getStatus()
207
>>> resp.getHeader('content-type')
'application/xml'
>>> print resp.getBody() #doctest:+XMLDATA
<multistatus xmlns="DAV:">
<response>
<href>https://127.0.0.1/testfile.txt</href>
<propstat>
<prop>
<ns1:prop1 xmlns:ns1="examplens:" />
<ns1:prop2 xmlns:ns1="examplens:" />
</prop>
<status>HTTP/1.1 200 正常</status>
</propstat>
</response>
</multistatus>

查询这些新属性。

>>> reqbody = """<propfind xmlns="DAV:">
... <prop xmlns:E="examplens:" >
... <E:prop1 />
... <E:prop2 />
... </prop>
... </propfind>
... """
>>> resp = webdav("""
... PROPFIND /testfile.txt HTTP/1.1
... Content-type: application/xml
... Content-length: %d
...
... %s""" %(len(reqbody), reqbody))
>>> resp.getStatus()
207
>>> resp.getHeader('content-type')
'application/xml'
>>> print resp.getBody() #doctest:+XMLDATA
<multistatus xmlns="DAV:">
<response>
<href>https://127.0.0.1/testfile.txt</href>
<propstat>
<prop>
<E:prop1 xmlns:E="examplens:">属性 1</E:prop1>
<E:prop2 xmlns:E="examplens:">属性 2</E:prop2>
</prop>
<status>HTTP/1.1 200 正常</status>
</propstat>
</response>
</multistatus>

更新第一个属性并删除第二个。

>>> reqbody = """<propertyupdate xmlns="DAV:">
... <set>
... <prop>
... <E:prop1 xmlns:E="examplens:">只有不透明属性</E:prop1>
... </prop>
... </set>
... <remove>
... <prop>
... <E:prop2 xmlns:E="examplens:" />
... </prop>
... </remove>
... </propertyupdate>"""
>>> resp = webdav("""
... PROPPATCH /testfile.txt HTTP/1.1
... Authorization: Basic mgr:mgrpw
... Content-type: application/xml
... Content-length: %d
...
... %s""" %(len(reqbody), reqbody))
>>> resp.getStatus()
207
>>> resp.getHeader('content-type')
'application/xml'
>>> print resp.getBody() #doctest:+XMLDATA
<multistatus xmlns="DAV:">
<response>
<href>https://127.0.0.1/testfile.txt</href>
<propstat>
<prop>
<E:prop1 xmlns:E="examplens:" />
<E:prop2 xmlns:E="examplens:" />
</prop>
<status>HTTP/1.1 200 正常</status>
</propstat>
</response>
</multistatus>

现在检查不透明属性是否已成功更新。

>>> reqbody = """<propfind xmlns="DAV:">
... <prop xmlns:E="examplens:" >
... <E:prop1 />
... <E:prop2 />
... </prop>
... </propfind>
... """
>>> resp = webdav("""
... PROPFIND /testfile.txt HTTP/1.1
... Content-type: application/xml
... Content-length: %d
...
... %s""" %(len(reqbody), reqbody))
>>> resp.getStatus()
207
>>> resp.getHeader('content-type')
'application/xml'
>>> print resp.getBody() #doctest:+XMLDATA
<multistatus xmlns="DAV:">
<response>
<href>https://127.0.0.1/testfile.txt</href>
<propstat>
<prop>
<ns1:prop1 xmlns:ns1="examplens:">只有不透明属性</ns1:prop1>
</prop>
<status>HTTP/1.1 200 正常</status>
</propstat>
<propstat>
<prop>
<ns1:prop2 xmlns:ns1="examplens:" />
</prop>
<status>HTTP/1.1 404 未找到</status>
</propstat>
</response>
</multistatus>


==============================
z3c.davapp.zopefile 中的更改
==============================

由以下支持