Commits

Max Noel  committed 959a468

* Always access class (not instance) attributes for __table__/__schema__/__hash_key__/__range_key__. More uniform and allows for some metaclass trickery.

  • Participants
  • Parent commits 2dd9c65

Comments (0)

Files changed (1)

File dynamodb_mapper/model.py

 
         We're supplying this method to avoid the need for extra checks in save.
         """
-        for (name, type_) in self.__schema__.iteritems():
+        for (name, type_) in type(self).__schema__.iteritems():
             setattr(self, name, type_())
 
     def to_db_dict(self):
         schema, suitable for direct storage in DynamoDB.
         """
         out = {}
-        for (name, type_) in self.__schema__.iteritems():
+        for (name, type_) in type(self).__schema__.iteritems():
             value = getattr(self, name)
             # Yes, that part is horrible. DynamoDB can't store empty
             # sets/strings, so we're representing them as missing
         update an existing one (iff allow_overwrite == True -- otherwise,
         the operation fails with OverwriteError).
         """
-        table = ConnectionBorg().get_table(type(self).__table__)
+        cls = type(self)
+        schema = cls.__schema__
+        hash_key = cls.__hash_key__
+        range_key = cls.__range_key__
+        table = ConnectionBorg().get_table(cls.__table__)
         item_data = self.to_db_dict()
         item = Item(table, attrs=item_data)
 
-        if (self.__schema__[self.__hash_key__] == autoincrement_int and
-                item_data[self.__hash_key__] == 0):
+        if schema[hash_key] == autoincrement_int and item_data[hash_key] == 0:
             # We're inserting a new item in an autoincrementing table.
             self._save_autoincrement_hash_key(item)
             # Update the primary key so that it reflects what it was saved as.
-            setattr(self, self.__hash_key__, item[self.__hash_key__])
+            setattr(self, hash_key, item[hash_key])
         else:
             if allow_overwrite:
                 expected_values = None
             else:
                 # Forbid overwrites: do a conditional write on
                 # "this hash_key doesn't exist"
-                expected_values = {self.__hash_key__: False}
-                if self.__range_key__:
-                    expected_values[self.__range_key__] = False
+                expected_values = {hash_key: False}
+                if range_key:
+                    expected_values[range_key] = False
             try:
                 item.put(expected_values)
             except DynamoDBResponseError as e:
 
     def delete(self):
         """Delete the current object from the database."""
-        hash_key_value = getattr(self, self.__hash_key__)
+        cls = type(self)
+        hash_key_value = getattr(self, cls.__hash_key__)
         # Range key is only present in composite primary keys
-        if self.__range_key__:
-            range_key_value = getattr(self, self.__range_key__)
+        if cls.__range_key__:
+            range_key_value = getattr(self, cls.__range_key__)
         else:
             range_key_value = None
 
-        table = ConnectionBorg().get_table(type(self).__table__)
+        table = ConnectionBorg().get_table(cls.__table__)
         Item(table, hash_key_value, range_key_value).delete()