[z3-checkins] r41944 - in z3/deliverance/DeliveranceVHoster/trunk: docs dvhoster tests

ianb at codespeak.net ianb at codespeak.net
Fri Apr 6 20:03:48 CEST 2007


Author: ianb
Date: Fri Apr  6 20:03:47 2007
New Revision: 41944

Modified:
   z3/deliverance/DeliveranceVHoster/trunk/docs/rest-api.txt
   z3/deliverance/DeliveranceVHoster/trunk/dvhoster/model.py
   z3/deliverance/DeliveranceVHoster/trunk/tests/test_functional_api.py
Log:
Added POST API for redirects

Modified: z3/deliverance/DeliveranceVHoster/trunk/docs/rest-api.txt
==============================================================================
--- z3/deliverance/DeliveranceVHoster/trunk/docs/rest-api.txt	(original)
+++ z3/deliverance/DeliveranceVHoster/trunk/docs/rest-api.txt	Fri Apr  6 20:03:47 2007
@@ -117,3 +117,8 @@
   ``path`` (a fixed path to redirect) or ``prefix`` (a directory to
   redirect).  It is redirected to the value of ``rewrite``.  You may
   optionally include a comment.
+
+``/.deliverance/redirects?{add or remove}``
+
+  Like ``remote_uris`` you can POST to these locations to add and
+  remove redirects, without reseting all redirects.

Modified: z3/deliverance/DeliveranceVHoster/trunk/dvhoster/model.py
==============================================================================
--- z3/deliverance/DeliveranceVHoster/trunk/dvhoster/model.py	(original)
+++ z3/deliverance/DeliveranceVHoster/trunk/dvhoster/model.py	Fri Apr  6 20:03:47 2007
@@ -6,6 +6,7 @@
 from ohm import lildav
 from ohm.validators import LineConverter, JSONConverter
 from formencode import validators
+from formencode.api import Invalid
 from formencode.foreach import ForEach
 from formencode.compound import All
 
@@ -355,10 +356,11 @@
                     # Found a match
                     break
             else:
-                raise ValueError(
+                raise Invalid(
                     "There's no current remote_uri matching %r "
                     "(of paths: %s)" %
-                    (path, ', '.join([repr(e['path']) for e in cur])))
+                    (path, ', '.join([repr(e['path']) for e in cur])),
+                    body, None)
         for item in body:
             path = item['path']
             for existing in cur[:]:
@@ -366,6 +368,49 @@
                     cur.remove(existing)
         self.remote_uris = cur
 
+    def add_redirects(self, body):
+        validator = RewriteValidator()
+        body = validator.to_python(body)
+        self.redirects = validator.to_python(self.redirects + body)
+
+    def remove_redirects(self, body):
+        cur = self.redirects
+        for item in body:
+            if item.get('path'):
+                path = item['path']
+                for existing in cur:
+                    if existing.get('path') == path:
+                        break
+                else:
+                    raise Invalid(
+                        "There's no current redirect matching path=%r"
+                        % path, body, None)
+            else:
+                prefix = item['prefix']
+                if not prefix.endswith('/'):
+                    item['prefix'] = prefix = prefix + '/'
+                for existing in cur:
+                    if existing.get('prefix') == prefix:
+                        break
+                else:
+                    raise Invalid(
+                        "There's no current redirect matching prefix=%r"
+                        % prefix, body, None)
+        for item in body:
+            if item.get('path'):
+                path = item['path']
+                for existing in cur[:]:
+                    if existing.get('path') == path:
+                        cur.remove(existing)
+                        break
+            else:
+                prefix = item['prefix']
+                for existing in cur[:]:
+                    if existing.get('prefix') == prefix:
+                        cur.remove(existing)
+                        break
+        self.redirects = cur
+
     theme_uri = server.Setter(
         validator=validators.URL())
     domain = server.Setter(
@@ -377,7 +422,9 @@
         POST={'add': (JSONConverter(), add_remote_uris),
               'remove': (JSONConverter(), remove_remote_uris)})
     redirects = server.JSONSetter(
-        validator=RewriteValidator())
+        validator=RewriteValidator(),
+        POST={'add': (JSONConverter(), add_redirects),
+              'remove': (JSONConverter(), remove_redirects)})
     additional_request_headers = server.JSONSetter(
         validator=HeaderValidator())
 

Modified: z3/deliverance/DeliveranceVHoster/trunk/tests/test_functional_api.py
==============================================================================
--- z3/deliverance/DeliveranceVHoster/trunk/tests/test_functional_api.py	(original)
+++ z3/deliverance/DeliveranceVHoster/trunk/tests/test_functional_api.py	Fri Apr  6 20:03:47 2007
@@ -111,6 +111,26 @@
      {"path": "/other.html", "rewrite": "http://otherexample.com/other.html"}]
      '''
     put('/.deliverance/redirects', data)
+
+    app.post('/.deliverance/redirects?add',
+             '[{"path": "/something-special", "rewrite": "http://whatever.com"}]',
+             status=204)
+    res = app.get('/.deliverance/redirects')
+    res.mustcontain('whatever.com', 'otherexample.com')
+    # Let's try a bad request:
+    app.post('/.deliverance/redirects?remove',
+             '[{"path": "/blahblah"}]', status=400)
+    # Then a good one:
+    app.post('/.deliverance/redirects?remove',
+             '[{"path": "/something-special"}]',
+             status=204)
+    res = app.get('/.deliverance/redirects')
+    res.mustcontain('otherexample.com')
+    data = json_loads(res.body)
+    for item in data:
+        if not item.get('path'):
+            continue
+        assert not item.get('path').startswith('/something-special')
     
     data = '<html>some data!</html>'
     put('/.deliverance/static/data.html', data)


More information about the z3-checkins mailing list