web/directory: fix rw_uri output in t=JSON to reflect mutable files properly

This commit is contained in:
Brian Warner 2008-05-19 18:37:28 -07:00
parent d0685d42ff
commit daebb42060
2 changed files with 41 additions and 17 deletions

View File

@ -911,6 +911,7 @@ class Web(WebMixin, unittest.TestCase):
self.failUnless(IMutableFileNode.providedBy(newnode))
self.failUnless(newnode.is_mutable())
self.failIf(newnode.is_readonly())
self._mutable_node = newnode
self._mutable_uri = newnode.get_uri()
d.addCallback(_got)
@ -976,6 +977,21 @@ class Web(WebMixin, unittest.TestCase):
self.failUnlessEqual(self._mutable_uri, newnode.get_uri())
d.addCallback(_got3)
d.addCallback(lambda res:
self.GET(self.public_url + "/foo/?t=json",
followRedirect=True))
def _check_page_json(res):
parsed = simplejson.loads(res)
self.failUnlessEqual(parsed[0], "dirnode")
children = parsed[1]["children"]
self.failUnless("new.txt" in children)
new_json = children["new.txt"]
self.failUnlessEqual(new_json[0], "filenode")
self.failUnlessEqual(new_json[1]["rw_uri"], self._mutable_uri)
ro_uri = unicode(self._mutable_node.get_readonly().to_string())
self.failUnlessEqual(new_json[1]["ro_uri"], ro_uri)
d.addCallback(_check_page_json)
d.addErrback(self.dump_error)
return d

View File

@ -627,28 +627,36 @@ def DirectoryJSONMetadata(ctx, dirnode):
def _got(children):
kids = {}
for name, (childnode, metadata) in children.iteritems():
if childnode.is_readonly():
rw_uri = None
ro_uri = childnode.get_uri()
else:
rw_uri = childnode.get_uri()
ro_uri = childnode.get_readonly_uri()
if IFileNode.providedBy(childnode):
kiduri = childnode.get_uri()
kiddata = ("filenode",
{'ro_uri': kiduri,
'size': childnode.get_size(),
'metadata': metadata,
})
kiddata = ("filenode", {'size': childnode.get_size(),
'metadata': metadata,
})
else:
assert IDirectoryNode.providedBy(childnode), (childnode,
children,)
kiddata = ("dirnode",
{'ro_uri': childnode.get_readonly_uri(),
'metadata': metadata,
})
if not childnode.is_readonly():
kiddata[1]['rw_uri'] = childnode.get_uri()
kiddata = ("dirnode", {'metadata': metadata})
if ro_uri:
kiddata[1]["ro_uri"] = ro_uri
if rw_uri:
kiddata[1]["rw_uri"] = rw_uri
kids[name] = kiddata
contents = { 'children': kids,
'ro_uri': dirnode.get_readonly_uri(),
}
if not dirnode.is_readonly():
contents['rw_uri'] = dirnode.get_uri()
if dirnode.is_readonly():
drw_uri = None
dro_uri = dirnode.get_uri()
else:
drw_uri = dirnode.get_uri()
dro_uri = dirnode.get_readonly_uri()
contents = { 'children': kids }
if dro_uri:
contents['ro_uri'] = dro_uri
if drw_uri:
contents['rw_uri'] = drw_uri
data = ("dirnode", contents)
return simplejson.dumps(data, indent=1)
d.addCallback(_got)