Revision 14c5be5a snf-astakos-app/astakos/im/api/__init__.py
b/snf-astakos-app/astakos/im/api/__init__.py | ||
---|---|---|
31 | 31 |
# interpreted as representing official policies, either expressed |
32 | 32 |
# or implied, of GRNET S.A. |
33 | 33 |
|
34 |
from functools import wraps |
|
35 |
from traceback import format_exc |
|
36 |
from urllib import quote, unquote |
|
37 |
|
|
34 | 38 |
from django.http import HttpResponse |
35 | 39 |
from django.utils import simplejson as json |
40 |
from django.conf import settings |
|
41 |
from django.core.urlresolvers import reverse |
|
42 |
|
|
43 |
from astakos.im.models import AstakosUser, GroupKind, Service, Resource |
|
44 |
from astakos.im.encoder import ModelJSONEncoder |
|
45 |
from astakos.im.api.faults import Fault, ItemNotFound, InternalServerError |
|
46 |
from astakos.im.settings import INVITATIONS_ENABLED, COOKIE_NAME, EMAILCHANGE_ENABLED |
|
36 | 47 |
|
37 |
from astakos.im.models import AstakosUser
|
|
38 |
from astakos.im.api.faults import ItemNotFound
|
|
48 |
import logging
|
|
49 |
logger = logging.getLogger(__name__)
|
|
39 | 50 |
|
40 | 51 |
format = ('%a, %d %b %Y %H:%M:%S GMT') |
41 | 52 |
|
53 |
def render_fault(request, fault): |
|
54 |
if isinstance(fault, InternalServerError) and settings.DEBUG: |
|
55 |
fault.details = format_exc(fault) |
|
56 |
|
|
57 |
request.serialization = 'text' |
|
58 |
data = fault.message + '\n' |
|
59 |
if fault.details: |
|
60 |
data += '\n' + fault.details |
|
61 |
response = HttpResponse(data, status=fault.code) |
|
62 |
response['Content-Length'] = len(response.content) |
|
63 |
return response |
|
64 |
|
|
65 |
def api_method(http_method=None): |
|
66 |
"""Decorator function for views that implement an API method.""" |
|
67 |
def decorator(func): |
|
68 |
@wraps(func) |
|
69 |
def wrapper(request, *args, **kwargs): |
|
70 |
try: |
|
71 |
if http_method and request.method != http_method: |
|
72 |
raise BadRequest('Method not allowed.') |
|
73 |
response = func(request, *args, **kwargs) |
|
74 |
return response |
|
75 |
except Fault, fault: |
|
76 |
return render_fault(request, fault) |
|
77 |
except BaseException, e: |
|
78 |
logger.exception('Unexpected error: %s' % e) |
|
79 |
fault = InternalServerError('Unexpected error') |
|
80 |
return render_fault(request, fault) |
|
81 |
return wrapper |
|
82 |
return decorator |
|
83 |
|
|
42 | 84 |
def _get_user_by_username(user_id): |
43 | 85 |
try: |
44 | 86 |
user = AstakosUser.objects.get(username = user_id) |
... | ... | |
87 | 129 |
response.content = json.dumps(user_info) |
88 | 130 |
response['Content-Type'] = 'application/json; charset=UTF-8' |
89 | 131 |
response['Content-Length'] = len(response.content) |
90 |
return response |
|
132 |
return response |
|
133 |
|
|
134 |
@api_method(http_method='GET') |
|
135 |
def get_services(request): |
|
136 |
callback = request.GET.get('callback', None) |
|
137 |
services = Service.objects.all() |
|
138 |
data = tuple({'id':s.pk, 'name':s.name, 'url':s.url, 'icon':s.icon} for s in services) |
|
139 |
data = json.dumps(data) |
|
140 |
mimetype = 'application/json' |
|
141 |
|
|
142 |
if callback: |
|
143 |
mimetype = 'application/javascript' |
|
144 |
data = '%s(%s)' % (callback, data) |
|
145 |
|
|
146 |
return HttpResponse(content=data, mimetype=mimetype) |
|
147 |
|
|
148 |
@api_method() |
|
149 |
def get_menu(request, with_extra_links=False, with_signout=True): |
|
150 |
index_url = reverse('index') |
|
151 |
absolute = lambda (url): request.build_absolute_uri(url) |
|
152 |
l = [{ 'url': absolute(index_url), 'name': "Sign in"}] |
|
153 |
cookie = unquote(request.COOKIES.get(COOKIE_NAME, '')) |
|
154 |
email = cookie.partition('|')[0] |
|
155 |
try: |
|
156 |
if not email: |
|
157 |
raise ValueError |
|
158 |
user = AstakosUser.objects.get(email=email, is_active=True) |
|
159 |
except AstakosUser.DoesNotExist: |
|
160 |
pass |
|
161 |
except ValueError: |
|
162 |
pass |
|
163 |
else: |
|
164 |
l = [] |
|
165 |
l.append({ 'url': absolute(reverse('astakos.im.views.index')), |
|
166 |
'name': user.email}) |
|
167 |
l.append({ 'url': absolute(reverse('astakos.im.views.edit_profile')), |
|
168 |
'name': "My account" }) |
|
169 |
if with_extra_links: |
|
170 |
if user.has_usable_password() and user.provider == 'local': |
|
171 |
l.append({ 'url': absolute(reverse('password_change')), |
|
172 |
'name': "Change password" }) |
|
173 |
if EMAILCHANGE_ENABLED: |
|
174 |
l.append({'url':absolute(reverse('email_change')), |
|
175 |
'name': "Change email"}) |
|
176 |
if INVITATIONS_ENABLED: |
|
177 |
l.append({ 'url': absolute(reverse('astakos.im.views.invite')), |
|
178 |
'name': "Invitations" }) |
|
179 |
l.append({ 'url': absolute(reverse('astakos.im.views.feedback')), |
|
180 |
'name': "Feedback" }) |
|
181 |
l.append({ 'url': absolute(reverse('group_add')), |
|
182 |
'name': "Add group" }) |
|
183 |
if with_signout: |
|
184 |
l.append({ 'url': absolute(reverse('astakos.im.views.logout')), |
|
185 |
'name': "Sign out"}) |
|
186 |
|
|
187 |
callback = request.GET.get('callback', None) |
|
188 |
data = json.dumps(tuple(l)) |
|
189 |
mimetype = 'application/json' |
|
190 |
|
|
191 |
if callback: |
|
192 |
mimetype = 'application/javascript' |
|
193 |
data = '%s(%s)' % (callback, data) |
|
194 |
|
|
195 |
return HttpResponse(content=data, mimetype=mimetype) |
Also available in: Unified diff