Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / api / __init__.py @ 6609821f

History | View | Annotate | Download (6.4 kB)

1 79b5d61b Sofia Papagiannaki
# Copyright 2013 GRNET S.A. All rights reserved.
2 79b5d61b Sofia Papagiannaki
#
3 79b5d61b Sofia Papagiannaki
# Redistribution and use in source and binary forms, with or
4 79b5d61b Sofia Papagiannaki
# without modification, are permitted provided that the following
5 79b5d61b Sofia Papagiannaki
# conditions are met:
6 79b5d61b Sofia Papagiannaki
#
7 79b5d61b Sofia Papagiannaki
#   1. Redistributions of source code must retain the above
8 79b5d61b Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
9 79b5d61b Sofia Papagiannaki
#      disclaimer.
10 79b5d61b Sofia Papagiannaki
#
11 79b5d61b Sofia Papagiannaki
#   2. Redistributions in binary form must reproduce the above
12 79b5d61b Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
13 79b5d61b Sofia Papagiannaki
#      disclaimer in the documentation and/or other materials
14 79b5d61b Sofia Papagiannaki
#      provided with the distribution.
15 79b5d61b Sofia Papagiannaki
#
16 79b5d61b Sofia Papagiannaki
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 79b5d61b Sofia Papagiannaki
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 79b5d61b Sofia Papagiannaki
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 79b5d61b Sofia Papagiannaki
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 79b5d61b Sofia Papagiannaki
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 79b5d61b Sofia Papagiannaki
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 79b5d61b Sofia Papagiannaki
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 79b5d61b Sofia Papagiannaki
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 79b5d61b Sofia Papagiannaki
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 79b5d61b Sofia Papagiannaki
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 79b5d61b Sofia Papagiannaki
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 79b5d61b Sofia Papagiannaki
# POSSIBILITY OF SUCH DAMAGE.
28 79b5d61b Sofia Papagiannaki
#
29 79b5d61b Sofia Papagiannaki
# The views and conclusions contained in the software and
30 79b5d61b Sofia Papagiannaki
# documentation are those of the authors and should not be
31 79b5d61b Sofia Papagiannaki
# interpreted as representing official policies, either expressed
32 79b5d61b Sofia Papagiannaki
# or implied, of GRNET S.A.
33 79b5d61b Sofia Papagiannaki
34 79b5d61b Sofia Papagiannaki
# Decorator for API methods, using common utils.api_method decorator.
35 79b5d61b Sofia Papagiannaki
# It is used for 'get_services' and 'get_menu' methods that do not
36 79b5d61b Sofia Papagiannaki
# require any sort of authentication
37 79b5d61b Sofia Papagiannaki
38 79b5d61b Sofia Papagiannaki
from functools import partial
39 79b5d61b Sofia Papagiannaki
40 79b5d61b Sofia Papagiannaki
from django.http import HttpResponse
41 79b5d61b Sofia Papagiannaki
from django.utils import simplejson as json
42 79b5d61b Sofia Papagiannaki
from django.core.urlresolvers import reverse
43 79b5d61b Sofia Papagiannaki
from django.utils.translation import ugettext as _
44 79b5d61b Sofia Papagiannaki
from django.contrib import messages
45 79b5d61b Sofia Papagiannaki
46 79b5d61b Sofia Papagiannaki
from snf_django.lib import api
47 79b5d61b Sofia Papagiannaki
from astakos.im.models import Service
48 79b5d61b Sofia Papagiannaki
from astakos.im.settings import (INVITATIONS_ENABLED, QUOTAHOLDER_URL,
49 79b5d61b Sofia Papagiannaki
                                 PROJECTS_VISIBLE)
50 79b5d61b Sofia Papagiannaki
51 79b5d61b Sofia Papagiannaki
import logging
52 79b5d61b Sofia Papagiannaki
logger = logging.getLogger(__name__)
53 79b5d61b Sofia Papagiannaki
54 79b5d61b Sofia Papagiannaki
absolute = lambda request, url: request.build_absolute_uri(url)
55 79b5d61b Sofia Papagiannaki
56 79b5d61b Sofia Papagiannaki
api_method = partial(api.api_method, user_required=False,
57 79b5d61b Sofia Papagiannaki
                     token_required=False, logger=logger)
58 79b5d61b Sofia Papagiannaki
59 79b5d61b Sofia Papagiannaki
60 79b5d61b Sofia Papagiannaki
@api_method(http_method=None)
61 79b5d61b Sofia Papagiannaki
def get_services(request):
62 79b5d61b Sofia Papagiannaki
    callback = request.GET.get('callback', None)
63 79b5d61b Sofia Papagiannaki
    mimetype = 'application/json'
64 79b5d61b Sofia Papagiannaki
    data = json.dumps(Service.catalog().values())
65 79b5d61b Sofia Papagiannaki
66 79b5d61b Sofia Papagiannaki
    if callback:
67 79b5d61b Sofia Papagiannaki
        # Consume session messages. When get_services is loaded from an astakos
68 79b5d61b Sofia Papagiannaki
        # page, messages should have already been consumed in the html
69 79b5d61b Sofia Papagiannaki
        # response. When get_services is loaded from another domain/service we
70 79b5d61b Sofia Papagiannaki
        # consume them here so that no stale messages to appear if user visits
71 79b5d61b Sofia Papagiannaki
        # an astakos view later on.
72 79b5d61b Sofia Papagiannaki
        # TODO: messages could be served to other services/sites in the dict
73 79b5d61b Sofia Papagiannaki
        # response of get_services and/or get_menu. Services could handle those
74 79b5d61b Sofia Papagiannaki
        # messages respectively.
75 79b5d61b Sofia Papagiannaki
        messages_list = list(messages.get_messages(request))
76 79b5d61b Sofia Papagiannaki
        mimetype = 'application/javascript'
77 79b5d61b Sofia Papagiannaki
        data = '%s(%s)' % (callback, data)
78 79b5d61b Sofia Papagiannaki
79 79b5d61b Sofia Papagiannaki
    return HttpResponse(content=data, mimetype=mimetype)
80 79b5d61b Sofia Papagiannaki
81 79b5d61b Sofia Papagiannaki
82 79b5d61b Sofia Papagiannaki
@api_method()
83 79b5d61b Sofia Papagiannaki
def get_menu(request, with_extra_links=False, with_signout=True):
84 79b5d61b Sofia Papagiannaki
    user = request.user
85 79b5d61b Sofia Papagiannaki
    index_url = reverse('index')
86 79b5d61b Sofia Papagiannaki
87 79b5d61b Sofia Papagiannaki
    if user.is_authenticated():
88 79b5d61b Sofia Papagiannaki
        l = []
89 79b5d61b Sofia Papagiannaki
        append = l.append
90 79b5d61b Sofia Papagiannaki
        item = MenuItem
91 79b5d61b Sofia Papagiannaki
        item.current_path = absolute(request, request.path)
92 79b5d61b Sofia Papagiannaki
        append(item(url=absolute(request, reverse('index')),
93 79b5d61b Sofia Papagiannaki
                    name=user.email))
94 79b5d61b Sofia Papagiannaki
        if with_extra_links:
95 79b5d61b Sofia Papagiannaki
            append(item(url=absolute(request, reverse('landing')),
96 79b5d61b Sofia Papagiannaki
                        name="Overview"))
97 79b5d61b Sofia Papagiannaki
        if with_signout:
98 79b5d61b Sofia Papagiannaki
            append(item(url=absolute(request, reverse('landing')),
99 79b5d61b Sofia Papagiannaki
                        name="Dashboard"))
100 79b5d61b Sofia Papagiannaki
        if with_extra_links:
101 79b5d61b Sofia Papagiannaki
            append(item(url=absolute(request, reverse('edit_profile')),
102 79b5d61b Sofia Papagiannaki
                        name="Profile"))
103 79b5d61b Sofia Papagiannaki
104 79b5d61b Sofia Papagiannaki
        if with_extra_links:
105 79b5d61b Sofia Papagiannaki
            if INVITATIONS_ENABLED:
106 79b5d61b Sofia Papagiannaki
                append(item(url=absolute(request, reverse('invite')),
107 79b5d61b Sofia Papagiannaki
                            name="Invitations"))
108 79b5d61b Sofia Papagiannaki
109 79b5d61b Sofia Papagiannaki
            append(item(url=absolute(request, reverse('resource_usage')),
110 79b5d61b Sofia Papagiannaki
                        name="Usage"))
111 79b5d61b Sofia Papagiannaki
112 79b5d61b Sofia Papagiannaki
            if QUOTAHOLDER_URL and PROJECTS_VISIBLE:
113 79b5d61b Sofia Papagiannaki
                append(item(url=absolute(request, reverse('project_list')),
114 79b5d61b Sofia Papagiannaki
                            name="Projects"))
115 79b5d61b Sofia Papagiannaki
            #append(item(
116 79b5d61b Sofia Papagiannaki
                #url=absolute(request, reverse('api_access')),
117 79b5d61b Sofia Papagiannaki
                #name="API Access"))
118 79b5d61b Sofia Papagiannaki
119 79b5d61b Sofia Papagiannaki
            append(item(url=absolute(request, reverse('feedback')),
120 79b5d61b Sofia Papagiannaki
                        name="Contact"))
121 79b5d61b Sofia Papagiannaki
        if with_signout:
122 79b5d61b Sofia Papagiannaki
            append(item(url=absolute(request, reverse('logout')),
123 79b5d61b Sofia Papagiannaki
                        name="Sign out"))
124 79b5d61b Sofia Papagiannaki
    else:
125 79b5d61b Sofia Papagiannaki
        l = [{'url': absolute(request, index_url),
126 79b5d61b Sofia Papagiannaki
              'name': _("Sign in")}]
127 79b5d61b Sofia Papagiannaki
128 79b5d61b Sofia Papagiannaki
    callback = request.GET.get('callback', None)
129 79b5d61b Sofia Papagiannaki
    data = json.dumps(tuple(l))
130 79b5d61b Sofia Papagiannaki
    mimetype = 'application/json'
131 79b5d61b Sofia Papagiannaki
132 79b5d61b Sofia Papagiannaki
    if callback:
133 79b5d61b Sofia Papagiannaki
        mimetype = 'application/javascript'
134 79b5d61b Sofia Papagiannaki
        data = '%s(%s)' % (callback, data)
135 79b5d61b Sofia Papagiannaki
136 79b5d61b Sofia Papagiannaki
    return HttpResponse(content=data, mimetype=mimetype)
137 79b5d61b Sofia Papagiannaki
138 79b5d61b Sofia Papagiannaki
139 79b5d61b Sofia Papagiannaki
class MenuItem(dict):
140 79b5d61b Sofia Papagiannaki
    current_path = ''
141 79b5d61b Sofia Papagiannaki
142 79b5d61b Sofia Papagiannaki
    def __init__(self, *args, **kwargs):
143 79b5d61b Sofia Papagiannaki
        super(MenuItem, self).__init__(*args, **kwargs)
144 79b5d61b Sofia Papagiannaki
        if kwargs.get('url') or kwargs.get('submenu'):
145 79b5d61b Sofia Papagiannaki
            self.__set_is_active__()
146 79b5d61b Sofia Papagiannaki
147 79b5d61b Sofia Papagiannaki
    def __setitem__(self, key, value):
148 79b5d61b Sofia Papagiannaki
        super(MenuItem, self).__setitem__(key, value)
149 79b5d61b Sofia Papagiannaki
        if key in ('url', 'submenu'):
150 79b5d61b Sofia Papagiannaki
            self.__set_is_active__()
151 79b5d61b Sofia Papagiannaki
152 79b5d61b Sofia Papagiannaki
    def __set_is_active__(self):
153 79b5d61b Sofia Papagiannaki
        if self.get('is_active'):
154 79b5d61b Sofia Papagiannaki
            return
155 79b5d61b Sofia Papagiannaki
        if self.current_path.startswith(self.get('url')):
156 79b5d61b Sofia Papagiannaki
            self.__setitem__('is_active', True)
157 79b5d61b Sofia Papagiannaki
        else:
158 79b5d61b Sofia Papagiannaki
            submenu = self.get('submenu', ())
159 79b5d61b Sofia Papagiannaki
            current = (i for i in submenu if i.get('url') == self.current_path)
160 79b5d61b Sofia Papagiannaki
            try:
161 79b5d61b Sofia Papagiannaki
                current_node = current.next()
162 79b5d61b Sofia Papagiannaki
                if not current_node.get('is_active'):
163 79b5d61b Sofia Papagiannaki
                    current_node.__setitem__('is_active', True)
164 79b5d61b Sofia Papagiannaki
                self.__setitem__('is_active', True)
165 79b5d61b Sofia Papagiannaki
            except StopIteration:
166 79b5d61b Sofia Papagiannaki
                return
167 79b5d61b Sofia Papagiannaki
168 79b5d61b Sofia Papagiannaki
    def __setattribute__(self, name, value):
169 79b5d61b Sofia Papagiannaki
        super(MenuItem, self).__setattribute__(name, value)
170 79b5d61b Sofia Papagiannaki
        if name == 'current_path':
171 79b5d61b Sofia Papagiannaki
            self.__set_is_active__()