Revision aa197ee4 api/images.py

b/api/images.py
66 66
        d['progress'] = 100 if image.state == 'ACTIVE' else 0
67 67
        if image.sourcevm:
68 68
            d['serverRef'] = image.sourcevm.id
69
        
69

  
70 70
        metadata = {}
71 71
        for meta in ImageMetadata.objects.filter(image=image):
72 72
            metadata[meta.meta_key] = meta.meta_value
73
        
73

  
74 74
        if metadata:
75 75
            d['metadata'] = {'values': metadata}
76
    
76

  
77 77
    return d
78 78

  
79 79
def metadata_to_dict(image):
......
89 89
    #                       unauthorized (401),
90 90
    #                       badRequest (400),
91 91
    #                       overLimit (413)
92
    
92

  
93 93
    since = isoparse(request.GET.get('changes-since'))
94
    
94

  
95 95
    if since:
96 96
        avail_images = Image.objects.filter(owner=request.user, updated__gte=since)
97 97
        if not avail_images:
98 98
            return HttpResponse(status=304)
99 99
    else:
100 100
        avail_images = Image.objects.filter(owner=request.user)
101
    
101

  
102 102
    images = [image_to_dict(image, detail) for image in avail_images]
103
    
103

  
104 104
    if request.serialization == 'xml':
105 105
        data = render_to_string('list_images.xml', {'images': images, 'detail': detail})
106 106
    else:
107 107
        data = json.dumps({'images': {'values': images}})
108
    
108

  
109 109
    return HttpResponse(data, status=200)
110 110

  
111 111
@api_method('POST')
......
122 122
    #                       resizeNotAllowed (403),
123 123
    #                       backupOrResizeInProgress (409),
124 124
    #                       overLimit (413)
125
    
125

  
126 126
    req = get_request_dict(request)
127
    
127

  
128 128
    try:
129 129
        d = req['image']
130 130
        server_id = d['serverRef']
131 131
        name = d['name']
132 132
    except (KeyError, ValueError):
133 133
        raise BadRequest('Malformed request.')
134
    
134

  
135 135
    owner = request.user
136 136
    vm = get_vm(server_id, owner)
137 137
    image = Image.objects.create(name=name, owner=owner, sourcevm=vm)
138
    
138

  
139 139
    imagedict = image_to_dict(image)
140 140
    if request.serialization == 'xml':
141 141
        data = render_to_string('image.xml', {'image': imagedict})
142 142
    else:
143 143
        data = json.dumps({'image': imagedict})
144
    
144

  
145 145
    return HttpResponse(data, status=202)
146 146

  
147 147
@api_method('GET')
......
153 153
    #                       badRequest (400),
154 154
    #                       itemNotFound (404),
155 155
    #                       overLimit (413)
156
    
156

  
157 157
    image = get_image(image_id, request.user)
158 158
    imagedict = image_to_dict(image)
159
    
159

  
160 160
    if request.serialization == 'xml':
161 161
        data = render_to_string('image.xml', {'image': imagedict})
162 162
    else:
163 163
        data = json.dumps({'image': imagedict})
164
    
164

  
165 165
    return HttpResponse(data, status=200)
166 166

  
167 167
@api_method('DELETE')
......
172 172
    #                       unauthorized (401),
173 173
    #                       itemNotFound (404),
174 174
    #                       overLimit (413)
175
    
175

  
176 176
    image = get_image(image_id, request.user)
177 177
    image.delete()
178 178
    return HttpResponse(status=204)
......
231 231
    #                       itemNotFound (404),
232 232
    #                       badRequest (400),
233 233
    #                       overLimit (413)
234
    
234

  
235 235
    image = get_image(image_id, request.user)
236 236
    meta = get_image_meta(image, key)
237 237
    return render_meta(request, meta, status=200)
......
274 274
    #                       buildInProgress (409),
275 275
    #                       badMediaType(415),
276 276
    #                       overLimit (413),
277
    
277

  
278 278
    image = get_image(image_id, request.user)
279 279
    meta = get_image_meta(image, key)
280 280
    meta.delete()

Also available in: Unified diff