Revision 529178b1 api/util.py
b/api/util.py | ||
---|---|---|
10 | 10 |
from traceback import format_exc |
11 | 11 |
from wsgiref.handlers import format_date_time |
12 | 12 |
|
13 |
import datetime |
|
14 |
import dateutil.parser |
|
15 |
import logging |
|
16 |
|
|
13 | 17 |
from django.conf import settings |
14 | 18 |
from django.http import HttpResponse |
15 | 19 |
from django.template.loader import render_to_string |
16 | 20 |
from django.utils import simplejson as json |
17 | 21 |
|
18 |
from synnefo.api.faults import Fault, BadRequest, ItemNotFound, ServiceUnavailable |
|
19 |
from synnefo.db.models import SynnefoUser, Image, ImageMetadata, VirtualMachine, VirtualMachineMetadata |
|
20 |
|
|
21 |
import datetime |
|
22 |
import dateutil.parser |
|
23 |
import logging |
|
22 |
from synnefo.api.faults import Fault, BadRequest, ItemNotFound, ServiceUnavailable, Unauthorized |
|
23 |
from synnefo.db.models import (SynnefoUser, Flavor, Image, ImageMetadata, |
|
24 |
VirtualMachine, VirtualMachineMetadata) |
|
24 | 25 |
|
25 | 26 |
|
26 | 27 |
class UTC(tzinfo): |
... | ... | |
35 | 36 |
|
36 | 37 |
|
37 | 38 |
def isoformat(d): |
38 |
"""Return an ISO8601 date string that includes a timezon.""" |
|
39 |
"""Return an ISO8601 date string that includes a timezone."""
|
|
39 | 40 |
|
40 | 41 |
return d.replace(tzinfo=UTC()).isoformat() |
41 | 42 |
|
... | ... | |
70 | 71 |
try: |
71 | 72 |
return SynnefoUser.objects.all()[0] |
72 | 73 |
except IndexError: |
73 |
raise Unauthorized |
|
74 |
raise Unauthorized('No users found.')
|
|
74 | 75 |
|
75 | 76 |
def get_vm(server_id): |
76 | 77 |
"""Return a VirtualMachine instance or raise ItemNotFound.""" |
... | ... | |
110 | 111 |
except ImageMetadata.DoesNotExist: |
111 | 112 |
raise ItemNotFound('Metadata key not found.') |
112 | 113 |
|
114 |
def get_flavor(flavor_id): |
|
115 |
"""Return a Flavor instance or raise ItemNotFound.""" |
|
116 |
|
|
117 |
try: |
|
118 |
flavor_id = int(flavor_id) |
|
119 |
return Flavor.objects.get(id=flavor_id) |
|
120 |
except Flavor.DoesNotExist: |
|
121 |
raise ItemNotFound('Flavor not found.') |
|
113 | 122 |
|
114 | 123 |
def get_request_dict(request): |
115 | 124 |
"""Returns data sent by the client as a python dict.""" |
... | ... | |
168 | 177 |
def request_serialization(request, atom_allowed=False): |
169 | 178 |
"""Return the serialization format requested. |
170 | 179 |
|
171 |
Valid formats are 'json', 'xml' and 'atom' if `atom_allowed` is True.
|
|
180 |
Valid formats are 'json', 'xml' and 'atom' if `atom_allowed` is True. |
|
172 | 181 |
""" |
173 | 182 |
|
174 | 183 |
path = request.path |
... | ... | |
209 | 218 |
except Fault, fault: |
210 | 219 |
return render_fault(request, fault) |
211 | 220 |
except BaseException, e: |
212 |
logging.exception('Unexpected error: %s' % e)
|
|
213 |
fault = ServiceUnavailable('Unexpected error') |
|
221 |
logging.exception('Unexpected error: %s', e)
|
|
222 |
fault = ServiceUnavailable('Unexpected error.')
|
|
214 | 223 |
return render_fault(request, fault) |
215 | 224 |
return wrapper |
216 | 225 |
return decorator |
Also available in: Unified diff