~ewanmellor/glance/lp694382

« back to all changes in this revision

Viewing changes to glance/registry/db/sqlalchemy/api.py

  • Committer: Tarmac
  • Author(s): Rick Harris
  • Date: 2010-12-23 19:41:59 UTC
  • mfrom: (27.1.2 use_datetime_obj)
  • Revision ID: tarmac-20101223194159-304tpg996wuo6mwf
Converts timestamp attributes to datetime objects before persisting.

Refactors image_update and image_create to use the same basic code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
 
53
53
 
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', {})
58
 
 
59
 
    image_ref = models.Image()
60
 
    image_ref.update(values)
61
 
    image_ref.save()
62
 
 
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)
66
 
 
67
 
    return image_get(_context, image_ref.id)
 
55
    return _image_update(_context, values, None)
68
56
 
69
57
 
70
58
def image_destroy(_context, image_id):
109
97
 
110
98
 
111
99
def image_update(_context, image_id, values):
 
100
    return _image_update(_context, values, image_id)
 
101
 
 
102
 
 
103
###################
 
104
 
 
105
 
 
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
 
112
 
 
113
 
 
114
def _drop_protected_attrs(model_class, values):
 
115
    """Removed protected attributes from values dictionary using the models
 
116
    __protected_attributes__ field.
 
117
    """
 
118
    for attr in model_class.__protected_attributes__:
 
119
        if attr in values:
 
120
            del values[attr]
 
121
 
 
122
 
 
123
def _image_update(_context, values, image_id):
 
124
    """Used internally by image_create and image_update
 
125
 
 
126
    :param image_id: If None, create the image, otherwise, find and update it
 
127
    """
112
128
    session = get_session()
113
129
    with session.begin():
 
130
        _drop_protected_attrs(models.Image, values)
 
131
 
114
132
        values['size'] = int(values['size'])
115
133
        values['is_public'] = bool(values.get('is_public', False))
116
134
        properties = values.pop('properties', {})
117
135
 
118
 
        image_ref = models.Image.find(image_id, session=session)
 
136
        if image_id:
 
137
            image_ref = models.Image.find(image_id, session=session)
 
138
        else:
 
139
            image_ref = models.Image()
 
140
 
119
141
        image_ref.update(values)
120
142
        image_ref.save(session=session)
121
143
 
123
145
            prop_values = {'image_id': image_ref.id, 'key': key, 'value': value}
124
146
            image_property_create(_context, prop_values)
125
147
 
126
 
 
127
 
###################
128
 
 
129
 
 
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
135
 
 
136
 
 
137
 
###################
138
 
 
139
 
 
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)