Daniel Ostrowski avatar Daniel Ostrowski committed 5ab54ab

add DocumentExists exception for noclobber to raise; allow passing query parameters to Riak via __init__ such as pw, dw, r, etc.

Comments (0)

Files changed (2)

riakdoc/documents.py

 from blinker import signal
 import copy
 from riak import RiakHttpTransport, RiakClient
+from riakdoc.exceptions import DocumentExists
 from riakdoc.keygen import simple_hex_key
 from riakdoc.indexes import BaseIndex, GetterShell
 
 
     __metaclass__ = BaseDocumentMeta
 
-    def __init__(self, key, obj=None, d=None, using=None, noclobber=True, indexes=None, *args, **kwargs):
+    def __init__(self, key, obj=None, d=None, using=None, noclobber=True, indexes=None, getparams=None, *args, **kwargs):
         """
         Create a basic Riak model.
 
         @type key: str
         @param obj: A seed object to use for this model.
         @type obj: riak.riak_object.RiakObject
-        @param d: A seed dictionary to use.
-        @type d: dict
+        @param d: Seed data to use.
         @param using: Name of the Riak connection to use.
         @type using: str
+        @param getparams: The tunable query parameters Riak accepts on get. examples: pw, r, w, dw
+        @type getparams: dict
         @param noclobber: Whether or not to accept both a `d` and a `obj` with data.
         @type noclobber: bool
         """
         self.key = str(key)
         if not obj:
-            obj = self.get_bucket(using=using).get(self.key)
+            obj = self.get_bucket(using=using).get(self.key, **(getparams or {}))
             obj.set_content_type('application/json')
         self._obj = obj
 
 
         if d:
             if self._obj.exists() and self._obj.get_data() and noclobber:
-                raise Exception('No clobber set but data and Riak object passed.')
+                raise DocumentExists('No clobber set but data and Riak object passed.')
             self.data = d
             self._obj.set_data(d)
         else:
     def save_primary(self, using=None, **kwargs):
         """
         Save to a quorum of primary nodes.
+        TODO: I think this is totally unneeded because I think you can just say "pw=quorum" in
+        the kwargs and it will work. Then the configs don't have to be involved.
         """
-        N = self.get_config_for(using=using).get('N', 4)
-        kwargs.update({'pw': N / 2 + 1})
+        if not 'pw' in kwargs:
+            N = self.get_config_for(using=using).get('N', 4)
+            kwargs.update({'pw': N / 2 + 1})
         return self.save(using=using, **kwargs)
 
     def update_indexes(self, indexes):

riakdoc/exceptions.py

+__author__ = 'Dan Ostrowski <dan.ostrowski@gmail.com>'
+
+class DocumentException(Exception):
+    pass
+
+class DocumentExists(DocumentException):
+    pass
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.