Revision d8e50a39 api/images.py

b/api/images.py
2 2
# Copyright (c) 2010 Greek Research and Technology Network
3 3
#
4 4

  
5
from synnefo.api.util import *
6
from synnefo.db.models import Image
5
from synnefo.api.common import method_not_allowed
6
from synnefo.api.util import isoformat, isoparse, get_user, get_image, get_request_dict, api_method
7
from synnefo.db.models import Image, ImageMetadata, VirtualMachine
7 8

  
8
from django.conf.urls.defaults import *
9
from django.conf.urls.defaults import patterns
9 10
from django.http import HttpResponse
10 11
from django.template.loader import render_to_string
11 12
from django.utils import simplejson as json
......
23 24
    elif request.method == 'POST':
24 25
        return create_image(request)
25 26
    else:
26
        fault = BadRequest()
27
        return render_fault(request, fault)
27
        return method_not_allowed(request)
28 28

  
29 29
def image_demux(request, image_id):
30 30
    if request.method == 'GET':
......
32 32
    elif request.method == 'DELETE':
33 33
        return delete_image(request, image_id)
34 34
    else:
35
        fault = BadRequest()
36
        return render_fault(request, fault)
35
        return method_not_allowed(request)
37 36

  
38 37

  
39 38
def image_to_dict(image, detail=True):
40 39
    d = {'id': image.id, 'name': image.name}
41 40
    if detail:
42
        d['updated'] = image.updated.isoformat()
43
        d['created'] = image.created.isoformat()
41
        d['updated'] = isoformat(image.updated)
42
        d['created'] = isoformat(image.created)
44 43
        d['status'] = image.state
45 44
        d['progress'] = 100 if image.state == 'ACTIVE' else 0
46 45
        d['description'] = image.description
......
66 65
    #                       badRequest (400),
67 66
    #                       overLimit (413)
68 67
    
69
    all_images = Image.objects.all()
70
    images = [image_to_dict(image, detail) for image in all_images]
68
    since = isoparse(request.GET.get('changes-since'))
71 69
    
72
    if request.type == 'xml':
73
        mimetype = 'application/xml'
70
    if since:
71
        avail_images = Image.objects.filter(updated__gt=since)
72
        if not avail_images:
73
            return HttpResponse(status=304)
74
    else:
75
        avail_images = Image.objects.all()
76
    
77
    images = [image_to_dict(image, detail) for image in avail_images]
78
    
79
    if request.serialization == 'xml':
74 80
        data = render_to_string('list_images.xml', {'images': images, 'detail': detail})
75 81
    else:
76
        mimetype = 'application/json'
77 82
        data = json.dumps({'images': {'values': images}})
78 83
    
79
    return HttpResponse(data, mimetype=mimetype, status=200)
84
    return HttpResponse(data, status=200)
80 85

  
81 86
@api_method('POST')
82 87
def create_image(request):
......
110 115
        raise ItemNotFound
111 116
    
112 117
    imagedict = image_to_dict(image)
113
    if request.type == 'xml':
118
    if request.serialization == 'xml':
114 119
        data = render_to_string('image.xml', {'image': imagedict})
115 120
    else:
116 121
        data = json.dumps({'image': imagedict})
......
127 132
    #                       itemNotFound (404),
128 133
    #                       overLimit (413)
129 134
    
130
    try:
131
        image_id = int(image_id)
132
        imagedict = image_to_dict(Image.objects.get(id=image_id))
133
    except Image.DoesNotExist:
134
        raise ItemNotFound
135
    image = get_image(image_id)
136
    imagedict = image_to_dict(image)
135 137
    
136
    if request.type == 'xml':
138
    if request.serialization == 'xml':
137 139
        data = render_to_string('image.xml', {'image': imagedict})
138 140
    else:
139 141
        data = json.dumps({'image': imagedict})
......
149 151
    #                       itemNotFound (404),
150 152
    #                       overLimit (413)
151 153
    
152
    try:
153
        image_id = int(image_id)
154
        image = Image.objects.get(id=image_id)
155
    except Image.DoesNotExist:
156
        raise ItemNotFound
157
    
154
    image = get_image(image_id)
158 155
    if image.owner != get_user():
159 156
        raise Unauthorized()
160 157
    image.delete()

Also available in: Unified diff