Skip to content

Commit 64c7d14

Browse files
kxepaliblislin
authored andcommitted
[server] Server cmd: ddoc updates
Author: Alexander Shorin <[email protected]> Patched by: Iblis Lin <[email protected]> Reference: djc#268 See Also: djc#276
1 parent 8e7b197 commit 64c7d14

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

couchdb/server/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ def __init__(self, *args, **kwargs):
337337
ddoc_commands = {}
338338
ddoc_commands['shows'] = render.ddoc_show
339339
ddoc_commands['lists'] = render.ddoc_list
340+
ddoc_commands['updates'] = render.ddoc_update
340341

341342
if self.version >= (1, 1, 0):
342343
self.commands['add_lib'] = state.add_lib
@@ -705,6 +706,30 @@ def ddoc_list(self, ddoc_id, func_path, rows, head=None, req=None):
705706
self._receive, self._respond = _input, _output
706707
return result
707708

709+
def ddoc_update(self, ddoc_id, func_path, doc=None, req=None):
710+
"""Runs ``ddoc`` ``updates`` command.
711+
Requires teached ddoc by :meth:`add_ddoc`.
712+
713+
:param ddoc_id: DDoc id.
714+
:type ddoc_id: str
715+
716+
:param func_path: List of keys which holds filter function within ddoc.
717+
:type func_path: list
718+
719+
:param doc: Document object.
720+
:type doc: dict
721+
722+
:param req: Request object.
723+
:type req: dict
724+
725+
:return: Three-element list with ``up`` token, new document object
726+
and response object.
727+
728+
.. versionadded:: 0.11.0
729+
"""
730+
args = [doc or {}, req or {}]
731+
return self.ddoc_cmd(ddoc_id, 'updates', func_path, args)
732+
708733
@property
709734
def ddocs(self):
710735
"""Returns dict with registered ddocs"""

couchdb/server/render.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,36 @@ def update(server, funsrc, doc, req):
349349
return run_update(server, server.compile(funsrc), doc, req)
350350

351351

352+
def ddoc_update(server, func, doc, req):
353+
"""Implementation of ddoc `updates` commands.
354+
355+
:command: updates
356+
357+
:param server: Query server instance.
358+
:type server: :class:`~couchdb.server.BaseQueryServer`
359+
360+
:param func: Update function object.
361+
:type func: function
362+
363+
:param doc: Document object.
364+
:type doc: dict
365+
366+
:param req: Request info.
367+
:type req: dict
368+
369+
:return: Three element list: ["up", doc, response]
370+
:rtype: list
371+
372+
:raises:
373+
- :exc:`~couchdb.server.exceptions.Error`
374+
If request method was GET.
375+
If response was not dict object or basestring.
376+
377+
.. versionadded:: 0.11.0
378+
"""
379+
return run_update(server, func, doc, req)
380+
381+
352382
################################################################################
353383
# Old render used only for 0.9.x
354384
#

couchdb/tests/server/qs.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,19 @@ def func(head, req):
533533
self.assertEqual(rows[2], ['chunks', ['baz']])
534534
self.assertEqual(tail, ['end', ['early']])
535535

536+
def test_ddoc_update(self):
537+
def func(doc, req):
538+
doc['world'] = 'hello'
539+
return [doc, 'hello, doc']
540+
541+
server = self.server((0, 11, 0))
542+
server.add_ddoc(wrap_func_to_ddoc('foo', ['updates', 'hello'], func))
543+
result = server.ddoc_update('foo', ['hello'], {'_id': 'foo'})
544+
self.assertEqual(
545+
result,
546+
['up', {'_id': 'foo', 'world': 'hello'}, {'body': 'hello, doc'}]
547+
)
548+
536549

537550
def suite():
538551
suite = unittest.TestSuite()

0 commit comments

Comments
 (0)