Fix property create

This commit is contained in:
Rick Harris 2011-01-22 19:15:55 +00:00
parent 9c57d403b2
commit ba947efb78
2 changed files with 49 additions and 13 deletions

View File

@ -103,12 +103,24 @@ def image_update(_context, image_id, values):
###################
def image_property_create(_context, values):
_drop_protected_attrs(models.Image, values)
image_property_ref = models.ImageProperty()
image_property_ref.update(values)
image_property_ref.save()
return image_property_ref
def image_property_create(_context, values, session=None):
"""Create an ImageProperty object"""
prop_ref = models.ImageProperty()
return _image_property_update(_context, prop_ref, values, session=session)
def image_property_update(_context, prop_ref, values, session=None):
"""Update an ImageProperty object"""
return _image_property_update(_context, prop_ref, values, session=session)
def _image_property_update(_context, prop_ref, values, session=None):
"""Used internally by image_property_create and image_property_update
"""
_drop_protected_attrs(models.ImageProperty, values)
prop_ref.update(values)
prop_ref.save(session=session)
return prop_ref
def _drop_protected_attrs(model_class, values):
@ -123,6 +135,8 @@ def _drop_protected_attrs(model_class, values):
def _image_update(_context, values, image_id):
"""Used internally by image_create and image_update
:param _context: Request context
:param values: A dict of attributes to set
:param image_id: If None, create the image, otherwise, find and update it
"""
session = get_session()
@ -143,10 +157,31 @@ def _image_update(_context, values, image_id):
image_ref.update(values)
image_ref.save(session=session)
for key, value in properties.iteritems():
prop_values = {'image_id': image_ref.id,
'key': key,
'value': value}
image_property_create(_context, prop_values)
_set_properties_for_image(_context, image_ref, properties, session)
return image_get(_context, image_ref.id)
def _set_properties_for_image(_context, image_ref, properties, session=None):
"""
Create or update a set of image_properties for a given image
:param _context: Request context
:param image_ref: An Image object
:param properties: A dict of properties to set
:param session: A SQLAlchemy session to use (if present)
"""
orig_properties = {}
for prop_ref in image_ref.properties:
orig_properties[prop_ref.key] = prop_ref
for key, value in properties.iteritems():
prop_values = {'image_id': image_ref.id,
'key': key,
'value': value}
if key in orig_properties:
prop_ref = orig_properties[key]
image_property_update(_context, prop_ref, prop_values,
session=session)
else:
image_property_create(_context, prop_values, session=session)

View File

@ -76,8 +76,9 @@ def get_image_meta_from_headers(response):
for key, value in headers:
key = str(key.lower())
if key.startswith('x-image-meta-property-'):
properties[key[len('x-image-meta-property-'):]] = value
if key.startswith('x-image-meta-'):
field_name = key[len('x-image-meta-property-'):].replace('-', '_')
properties[field_name] = value
elif key.startswith('x-image-meta-'):
field_name = key[len('x-image-meta-'):].replace('-', '_')
result[field_name] = value
result['properties'] = properties