From a85a4d71f77012b267f503870d2c7fc927237d4e Mon Sep 17 00:00:00 2001
From: Zooko O'Whielacronx <zooko@zooko.com>
Date: Thu, 20 Dec 2007 15:31:12 -0700
Subject: [PATCH] webish: add button to make directories and unit test of it
 Unfortunately although it passes the unit tests, it doesn't work, because the
 unit tests and the implementation use the "encode params into URL" technique
 but the button uses the "encode params into request body" technique.

---
 src/allmydata/test/test_web.py  | 19 +++++++++++++++++++
 src/allmydata/web/welcome.xhtml |  1 +
 src/allmydata/webish.py         | 19 +++++++++++++++----
 3 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/src/allmydata/test/test_web.py b/src/allmydata/test/test_web.py
index 1c96c8d3..8cf6e648 100644
--- a/src/allmydata/test/test_web.py
+++ b/src/allmydata/test/test_web.py
@@ -1114,6 +1114,25 @@ class Web(WebMixin, unittest.TestCase):
         d.addBoth(self.shouldRedirect, None, statuscode='303')
         return d
 
+    def test_welcome_page_mkdir_button(self):
+        # Fetch the welcome page.
+        d = self.GET("/")
+        def _after_get_welcome_page(res):
+            MKDIR_BUTTON_RE=re.compile('<form action="([^"]*)" method="post".*<input type="hidden" name="t" value="([^"]*)" /><input type="hidden" name="([^"]*)" value="([^"]*)" /><input type="submit" value="create" />', re.I)
+            mo = MKDIR_BUTTON_RE.search(res)
+            formaction = mo.group(1)
+            formt = mo.group(2)
+            formaname = mo.group(3)
+            formavalue = mo.group(4)
+            return (formaction, formt, formaname, formavalue)
+        d.addCallback(_after_get_welcome_page)
+        def _after_parse_form(res):
+            (formaction, formt, formaname, formavalue) = res
+            return self.POST("/%s?t=%s&%s=%s" % (formaction, formt, formaname, formavalue))
+        d.addCallback(_after_parse_form)
+        d.addBoth(self.shouldRedirect, None, statuscode='303')
+        return d
+
     def test_POST_mkdir_replace(self): # return value?
         d = self.POST(self.public_url + "/foo", t="mkdir", name="sub")
         d.addCallback(lambda res: self._foo_node.get("sub"))
diff --git a/src/allmydata/web/welcome.xhtml b/src/allmydata/web/welcome.xhtml
index 300f598d..bc0deee7 100644
--- a/src/allmydata/web/welcome.xhtml
+++ b/src/allmydata/web/welcome.xhtml
@@ -37,6 +37,7 @@ tool</a> may also be useful.</div>
 </div>
 
 <div n:render="download_form"/>
+<div n:render="mkdir_form"/>
 
   </body>
 </html>
diff --git a/src/allmydata/webish.py b/src/allmydata/webish.py
index 91539c8b..46605df0 100644
--- a/src/allmydata/webish.py
+++ b/src/allmydata/webish.py
@@ -1237,7 +1237,7 @@ class URIPOSTHandler(rend.Page):
             return d
 
         if t == "mkdir":
-            # "PUT /uri?t=mkdir", to create an unlinked directory.
+            # "POST /uri?t=mkdir", to create an unlinked directory.
             d = IClient(ctx).create_empty_dirnode()
             redirect = req.args.has_key("redirect_to_result") and boolean_of_arg(req.args["redirect_to_result"][0])
             if redirect:
@@ -1352,17 +1352,28 @@ class Root(rend.Page):
         return T.p["personal vdrive not available."]
 
     # this is a form where users can download files by URI
-
     def render_download_form(self, ctx, data):
         form = T.form(action="uri", method="get",
                       enctype="multipart/form-data")[
             T.fieldset[
-            T.legend(class_="freeform-form-label")["Download a file"],
+            T.legend(class_="freeform-form-label")["download a file"],
             "URI of file to download: ",
             T.input(type="text", name="uri"), " ",
             "Filename to download as: ",
             T.input(type="text", name="filename"), " ",
-            T.input(type="submit", value="Download"),
+            T.input(type="submit", value="download"),
+            ]]
+        return T.div[form]
+
+    # this is a form where users can create new directories
+    def render_mkdir_form(self, ctx, data):
+        form = T.form(action="uri", method="post",
+                      enctype="multipart/form-data")[
+            T.fieldset[
+            T.legend(class_="freeform-form-label")["create a directory"],
+            T.input(type="hidden", name="t", value="mkdir"),
+            T.input(type="hidden", name="redirect_to_result", value="true"),
+            T.input(type="submit", value="create"),
             ]]
         return T.div[form]
 
-- 
2.45.2