Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 9f9fb70

Browse files
authored
Merge pull request #181 from cloudant/175-support-rewrites-design-document
Rewrite rules property in design_document.py
2 parents c99cc85 + d24112f commit 9f9fb70

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
==================
33
- [NEW] Added support for Cloudant Search execution.
44
- [NEW] Added support for Cloudant Search index management.
5+
- [NEW] Added ``rewrites`` accessor property for URL rewriting.
56

67
2.0.3 (2016-06-03)
78
==================

src/cloudant/design_document.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,44 @@ def __init__(self, database, document_id=None):
4747
self.setdefault('views', dict())
4848
self.setdefault('indexes', dict())
4949

50+
@property
51+
def rewrites(self):
52+
"""
53+
Provides an accessor property to a list of dictionaries with rewrite
54+
rules in the locally cached DesignDocument. Each rule for URL rewriting
55+
is a JSON object with four fields: ``from``, ``to``, ``method``,
56+
and ``query``.
57+
58+
Note: Requests that match the rewrite rules must have a URL path that
59+
starts with ``/$DATABASE/_design/doc/_rewrite``.
60+
61+
Rewrite rule example:
62+
63+
.. code-block:: python
64+
65+
# Add the rule to ``rewrites`` and save the design document
66+
ddoc = DesignDocument(self.db, '_design/ddoc001')
67+
ddoc['rewrites'] = [
68+
{"from": "/old/topic",
69+
"to": "/new/",
70+
"method": "GET",
71+
"query": {}
72+
}
73+
]
74+
ddoc.save()
75+
76+
Once the rewrite rule is saved to the remote database, the GET
77+
request URL ``/$DATABASE/_design/doc/_rewrite/old/topic?k=v`` would be
78+
rewritten as ``/$DATABASE/_design/doc/_rewrite/new?k=v``.
79+
80+
For more details on URL rewriting, see the `rewrite rules
81+
documentation <https://docs.cloudant.com/design_documents.html
82+
#rewrite-rules>`_.
83+
84+
:returns: List of dictionaries containing rewrite rules as key/value
85+
"""
86+
return self.get('rewrites')
87+
5088
@property
5189
def views(self):
5290
"""

tests/unit/design_document_tests.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,5 +1035,31 @@ def test_get_search_index(self):
10351035
'{"store": true}); }\n}'}
10361036
)
10371037

1038+
def test_rewrite_rule(self):
1039+
"""
1040+
Test that design document URL is rewritten to the expected test document.
1041+
"""
1042+
ddoc = DesignDocument(self.db, '_design/ddoc001')
1043+
ddoc['rewrites'] = [
1044+
{"from": "",
1045+
"to": "/../../rewrite_doc",
1046+
"method": "GET",
1047+
"query": {}
1048+
}
1049+
]
1050+
self.assertIsInstance(ddoc.rewrites, list)
1051+
self.assertIsInstance(ddoc.rewrites[0], dict)
1052+
ddoc.save()
1053+
doc = Document(self.db, 'rewrite_doc')
1054+
doc.save()
1055+
resp = self.client.r_session.get('/'.join([ddoc.document_url, '_rewrite']))
1056+
self.assertEquals(
1057+
resp.json(),
1058+
{
1059+
'_id': 'rewrite_doc',
1060+
'_rev': doc['_rev']
1061+
}
1062+
)
1063+
10381064
if __name__ == '__main__':
10391065
unittest.main()

0 commit comments

Comments
 (0)