Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def commit(self):
"""
Writes all of the changes that are pending
"""
log.debug("%s committing batch operation", self.model)
put_items = []
delete_items = []
attrs_name = pythonic(ATTRIBUTES)
for item in self.pending_operations:
if item['action'] == PUT:
put_items.append(item['item']._serialize(attr_map=True)[attrs_name])
elif item['action'] == DELETE:
delete_items.append(item['item']._get_keys())
self.pending_operations = []
if not len(put_items) and not len(delete_items):
return
data = self.model._get_connection().batch_write_item(
put_items=put_items,
delete_items=delete_items
)
if data is None:
return
retries = 0
unprocessed_items = data.get(UNPROCESSED_ITEMS, {}).get(self.model.Meta.table_name)
pythonic(ATTR_DEFINITIONS): [],
pythonic(KEY_SCHEMA): []
}
for attr_name, attr_cls in cls.get_attributes().items():
if attr_cls.is_hash_key or attr_cls.is_range_key:
schema[pythonic(ATTR_DEFINITIONS)].append({
pythonic(ATTR_NAME): attr_cls.attr_name,
pythonic(ATTR_TYPE): ATTR_TYPE_MAP[attr_cls.attr_type]
})
if attr_cls.is_hash_key:
schema[pythonic(KEY_SCHEMA)].append({
pythonic(KEY_TYPE): HASH,
pythonic(ATTR_NAME): attr_cls.attr_name
})
elif attr_cls.is_range_key:
schema[pythonic(KEY_SCHEMA)].append({
pythonic(KEY_TYPE): RANGE,
pythonic(ATTR_NAME): attr_cls.attr_name
})
return schema
def _get_save_args(self, attributes=True, null_check=True):
"""
Gets the proper *args, **kwargs for saving and retrieving this object
This is used for serializing items to be saved, or for serializing just the keys.
:param attributes: If True, then attributes are included.
:param null_check: If True, then attributes are checked for null.
"""
kwargs = {}
serialized = self._serialize(null_check=null_check)
hash_key = serialized.get(HASH)
range_key = serialized.get(RANGE, None)
args = (hash_key, )
if range_key is not None:
kwargs[pythonic(RANGE_KEY)] = range_key
if attributes:
kwargs[pythonic(ATTRIBUTES)] = serialized[pythonic(ATTRIBUTES)]
return args, kwargs
if hasattr(cls.Meta, pythonic(STREAM_VIEW_TYPE)):
schema[pythonic(STREAM_SPECIFICATION)] = {
pythonic(STREAM_ENABLED): True,
pythonic(STREAM_VIEW_TYPE): cls.Meta.stream_view_type
}
if hasattr(cls.Meta, pythonic(BILLING_MODE)):
schema[pythonic(BILLING_MODE)] = cls.Meta.billing_mode
if read_capacity_units is not None:
schema[pythonic(READ_CAPACITY_UNITS)] = read_capacity_units
if write_capacity_units is not None:
schema[pythonic(WRITE_CAPACITY_UNITS)] = write_capacity_units
if billing_mode is not None:
schema[pythonic(BILLING_MODE)] = billing_mode
index_data = cls._get_indexes()
schema[pythonic(GLOBAL_SECONDARY_INDEXES)] = index_data.get(pythonic(GLOBAL_SECONDARY_INDEXES))
schema[pythonic(LOCAL_SECONDARY_INDEXES)] = index_data.get(pythonic(LOCAL_SECONDARY_INDEXES))
index_attrs = index_data.get(pythonic(ATTR_DEFINITIONS))
attr_keys = [attr.get(pythonic(ATTR_NAME)) for attr in schema.get(pythonic(ATTR_DEFINITIONS))]
for attr in index_attrs:
attr_name = attr.get(pythonic(ATTR_NAME))
if attr_name not in attr_keys:
schema[pythonic(ATTR_DEFINITIONS)].append(attr)
attr_keys.append(attr_name)
cls._get_connection().create_table(
**schema
)
if wait:
while True:
status = cls._get_connection().describe_table()
if status:
data = status.get(TABLE_STATUS)
if data == ACTIVE:
:param actions: a list of Action updates to apply
:param condition: an optional Condition on which to update
"""
if not isinstance(actions, list) or len(actions) == 0:
raise TypeError("the value of `actions` is expected to be a non-empty list")
args, save_kwargs = self._get_save_args(null_check=False)
version_condition = self._handle_version_attribute(save_kwargs, actions=actions)
if version_condition is not None:
condition &= version_condition
kwargs = {
pythonic(RETURN_VALUES): ALL_NEW,
}
if pythonic(RANGE_KEY) in save_kwargs:
kwargs[pythonic(RANGE_KEY)] = save_kwargs[pythonic(RANGE_KEY)]
kwargs.update(condition=condition)
kwargs.update(actions=actions)
data = self._get_connection().update_item(*args, **kwargs)
for name, value in data[ATTRIBUTES].items():
attr_name = self._dynamo_to_python_attr(name)
attr = self.get_attributes().get(attr_name)
if attr:
setattr(self, attr_name, attr.deserialize(attr.get_value(value)))
return data
"""
Returns the schema for this table
"""
schema = {
pythonic(ATTR_DEFINITIONS): [],
pythonic(KEY_SCHEMA): []
}
for attr_name, attr_cls in cls._get_attributes().items():
if attr_cls.is_hash_key or attr_cls.is_range_key:
schema[pythonic(ATTR_DEFINITIONS)].append({
pythonic(ATTR_NAME): attr_cls.attr_name,
pythonic(ATTR_TYPE): ATTR_TYPE_MAP[attr_cls.attr_type]
})
if attr_cls.is_hash_key:
schema[pythonic(KEY_SCHEMA)].append({
pythonic(KEY_TYPE): HASH,
pythonic(ATTR_NAME): attr_cls.attr_name
})
elif attr_cls.is_range_key:
schema[pythonic(KEY_SCHEMA)].append({
pythonic(KEY_TYPE): RANGE,
pythonic(ATTR_NAME): attr_cls.attr_name
})
return schema
"""
Returns the schema for this table
"""
schema = {
pythonic(ATTR_DEFINITIONS): [],
pythonic(KEY_SCHEMA): []
}
for attr_name, attr_cls in cls.get_attributes().items():
if attr_cls.is_hash_key or attr_cls.is_range_key:
schema[pythonic(ATTR_DEFINITIONS)].append({
pythonic(ATTR_NAME): attr_cls.attr_name,
pythonic(ATTR_TYPE): ATTR_TYPE_MAP[attr_cls.attr_type]
})
if attr_cls.is_hash_key:
schema[pythonic(KEY_SCHEMA)].append({
pythonic(KEY_TYPE): HASH,
pythonic(ATTR_NAME): attr_cls.attr_name
})
elif attr_cls.is_range_key:
schema[pythonic(KEY_SCHEMA)].append({
pythonic(KEY_TYPE): RANGE,
pythonic(ATTR_NAME): attr_cls.attr_name
})
return schema
async def create_table(cls, wait=False, read_capacity_units=None, write_capacity_units=None):
"""
:param wait: If set, then this call will block until the table is ready for use
:param read_capacity_units: Sets the read capacity units for this table
:param write_capacity_units: Sets the write capacity units for this table
"""
if not await cls.exists():
schema = cls._get_schema()
if hasattr(cls.Meta, pythonic(READ_CAPACITY_UNITS)):
schema[pythonic(READ_CAPACITY_UNITS)] = cls.Meta.read_capacity_units
if hasattr(cls.Meta, pythonic(WRITE_CAPACITY_UNITS)):
schema[pythonic(WRITE_CAPACITY_UNITS)] = cls.Meta.write_capacity_units
if hasattr(cls.Meta, pythonic(STREAM_VIEW_TYPE)):
schema[pythonic(STREAM_SPECIFICATION)] = {
pythonic(STREAM_ENABLED): True,
pythonic(STREAM_VIEW_TYPE): cls.Meta.stream_view_type
}
if read_capacity_units is not None:
schema[pythonic(READ_CAPACITY_UNITS)] = read_capacity_units
if write_capacity_units is not None:
schema[pythonic(WRITE_CAPACITY_UNITS)] = write_capacity_units
index_data = cls._get_indexes()
schema[pythonic(GLOBAL_SECONDARY_INDEXES)] = index_data.get(pythonic(GLOBAL_SECONDARY_INDEXES))
schema[pythonic(LOCAL_SECONDARY_INDEXES)] = index_data.get(pythonic(LOCAL_SECONDARY_INDEXES))
index_attrs = index_data.get(pythonic(ATTR_DEFINITIONS))
attr_keys = [attr.get(pythonic(ATTR_NAME)) for attr in schema.get(pythonic(ATTR_DEFINITIONS))]
for attr in index_attrs:
attr_name = attr.get(pythonic(ATTR_NAME))
if attr_name not in attr_keys: