root / snf-astakos-app / astakos / api / __init__.py @ 0be81d73
History | View | Annotate | Download (3.2 kB)
1 |
# Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
# Decorator for API methods, using common utils.api_method decorator.
|
35 |
# It is used for 'get_services' and 'get_menu' methods that do not
|
36 |
# require any sort of authentication
|
37 |
|
38 |
from functools import partial |
39 |
|
40 |
from django.http import HttpResponse |
41 |
from django.utils import simplejson as json |
42 |
from django.core.urlresolvers import reverse |
43 |
from django.utils.translation import ugettext as _ |
44 |
from django.contrib import messages |
45 |
from django.contrib.auth.models import User |
46 |
|
47 |
from snf_django.lib import api |
48 |
from astakos.im.models import Service |
49 |
from astakos.im import settings |
50 |
|
51 |
import logging |
52 |
logger = logging.getLogger(__name__) |
53 |
|
54 |
absolute = lambda request, url: request.build_absolute_uri(url)
|
55 |
|
56 |
api_method = partial(api.api_method, user_required=False,
|
57 |
token_required=False, logger=logger)
|
58 |
|
59 |
|
60 |
@api_method(http_method=None) |
61 |
def get_services(request): |
62 |
callback = request.GET.get('callback', None) |
63 |
mimetype = 'application/json'
|
64 |
data = json.dumps(Service.catalog().values()) |
65 |
|
66 |
if callback:
|
67 |
# Consume session messages. When get_services is loaded from an astakos
|
68 |
# page, messages should have already been consumed in the html
|
69 |
# response. When get_services is loaded from another domain/service we
|
70 |
# consume them here so that no stale messages to appear if user visits
|
71 |
# an astakos view later on.
|
72 |
# TODO: messages could be served to other services/sites in the dict
|
73 |
# response of get_services and/or get_menu. Services could handle those
|
74 |
# messages respectively.
|
75 |
messages_list = list(messages.get_messages(request))
|
76 |
mimetype = 'application/javascript'
|
77 |
data = '%s(%s)' % (callback, data)
|
78 |
|
79 |
return HttpResponse(content=data, mimetype=mimetype)
|