1. Jean-Tiare Le Bigot
  2. dynamodb-mapper-get

Commits

Jean-Tiare LE BIGOT  committed b7d336b Draft

add hook to compute keys based on random values

  • Participants
  • Parent commits d62d074
  • Branches default

Comments (0)

Files changed (1)

File dynamodb_mapper/model.py

View file
         return instance
 
     @classmethod
-    def get(cls, hash_key_value, range_key_value=None, consistent_read=False):
+    def compute_keys(cls, *args, **kwargs):
+        """Compute ``hash_key`` and ``range_key`` based on a list of
+        passed to get.
+
+        This class method is intended to be derived for custom keys. for example,
+        you may want to compute a hash of multiple values or concatenate multiple
+        components as a single key.
+
+        :return: The ``hash_key``, ``range_key`` tuple.n Unknown keys are ``None``
+        """
+        # This is a class methos because it intends to be calles from...
+        # clasmethods
+
+        # This code needs to maintain backward compatibility to be fully
+        # transparent. This is why it is so weird.
+        # Applications overloading this method for internal purpose will
+        # most likely do not need this level of complexity as the are
+        # already 'aware' of the coding conventions
+
+        h = None
+        r = None
+
+        args_len = len(args)
+
+        if args_len > 0:
+            h = args[0]
+            if args_len > 1:
+                r = args[1]
+            elif 'range_key_value' in kwargs:
+                r = kwargs['range_key_value']
+        elif 'hash_key_value' in kwargs:
+            h = kwargs['hash_key_value']
+            if 'range_key_value' in kwargs:
+                r = kwargs['range_key_value']
+
+        return (h, r)
+
+    @classmethod
+    def get(cls, *args, **kwargs):
         """Retrieve a single object from DynamoDB according to its primary key.
 
         Note that this is not a query method -- it will only return the object
         Objects loaded by this method are marked as coming from the DB. Hence
         their initial state is saved in ``self._raw_data``.
 
+        When a custom compute_keys is provided, it is the one responsible of
+        computing the ``hash_key`` and the ``range_key`` on the basis of the
+        *args and **kwargs.
+
         :param hash_key_value: The value of the requested item's hash_key.
 
         :param range_key_value: The value of the requested item's range_key,
         :param consistent_read: If False (default), an eventually consistent
             read is performed. Set to True for strongly consistent reads.
         """
+
+        consistent_read=False
+        if 'consistent_read' in kwargs:
+            consistent_read = kwargs['consistent_read']
+            del kwargs['consistent_read']
+
+        (hash_key_value, range_key_value) = cls.compute_keys(*args, **kwargs)
+
         table = ConnectionBorg().get_table(cls.__table__)
         # Convert the keys to DynamoDB values.
         h_value = _python_to_dynamodb(hash_key_value)