54
54
def image_create(_context, values):
55
values['size'] = int(values['size'])
56
values['is_public'] = bool(values.get('is_public', False))
57
properties = values.pop('properties', {})
59
image_ref = models.Image()
60
image_ref.update(values)
63
for key, value in properties.iteritems():
64
prop_values = {'image_id': image_ref.id, 'key': key, 'value': value}
65
image_property_create(_context, prop_values)
67
return image_get(_context, image_ref.id)
55
return _image_update(_context, values, None)
70
58
def image_destroy(_context, image_id):
111
99
def image_update(_context, image_id, values):
100
return _image_update(_context, values, image_id)
106
def image_property_create(_context, values):
107
_drop_protected_attrs(models.Image, values)
108
image_property_ref = models.ImageProperty()
109
image_property_ref.update(values)
110
image_property_ref.save()
111
return image_property_ref
114
def _drop_protected_attrs(model_class, values):
115
"""Removed protected attributes from values dictionary using the models
116
__protected_attributes__ field.
118
for attr in model_class.__protected_attributes__:
123
def _image_update(_context, values, image_id):
124
"""Used internally by image_create and image_update
126
:param image_id: If None, create the image, otherwise, find and update it
112
128
session = get_session()
113
129
with session.begin():
130
_drop_protected_attrs(models.Image, values)
114
132
values['size'] = int(values['size'])
115
133
values['is_public'] = bool(values.get('is_public', False))
116
134
properties = values.pop('properties', {})
118
image_ref = models.Image.find(image_id, session=session)
137
image_ref = models.Image.find(image_id, session=session)
139
image_ref = models.Image()
119
141
image_ref.update(values)
120
142
image_ref.save(session=session)
123
145
prop_values = {'image_id': image_ref.id, 'key': key, 'value': value}
124
146
image_property_create(_context, prop_values)
130
def image_file_create(_context, values):
131
image_file_ref = models.ImageFile()
132
image_file_ref.update(values)
133
image_file_ref.save()
134
return image_file_ref
140
def image_property_create(_context, values):
141
image_property_ref = models.ImageProperty()
142
image_property_ref.update(values)
143
image_property_ref.save()
144
return image_property_ref
148
return image_get(_context, image_ref.id)