root / snf-astakos-app / astakos / im / api / service.py @ 19246578
History | View | Annotate | Download (4.1 kB)
1 |
# Copyright 2011-2012 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 |
import logging |
35 |
|
36 |
from functools import wraps |
37 |
from time import time, mktime |
38 |
|
39 |
from django.http import HttpResponse |
40 |
from django.views.decorators.csrf import csrf_exempt |
41 |
from django.utils import simplejson as json |
42 |
|
43 |
from . import render_fault, __get_uuid_displayname_catalogs, __send_feedback
|
44 |
from .faults import ( |
45 |
Fault, Unauthorized, InternalServerError, BadRequest, ItemNotFound) |
46 |
from astakos.im.models import Service |
47 |
|
48 |
logger = logging.getLogger(__name__) |
49 |
|
50 |
|
51 |
def api_method(http_method=None, token_required=False): |
52 |
"""Decorator function for views that implement an API method."""
|
53 |
def decorator(func): |
54 |
@wraps(func)
|
55 |
def wrapper(request, *args, **kwargs): |
56 |
try:
|
57 |
if http_method and request.method != http_method: |
58 |
raise BadRequest('Method not allowed.') |
59 |
x_auth_token = request.META.get('HTTP_X_AUTH_TOKEN')
|
60 |
if token_required:
|
61 |
if not x_auth_token: |
62 |
raise Unauthorized('Access denied') |
63 |
try:
|
64 |
service = Service.objects.get(auth_token=x_auth_token) |
65 |
|
66 |
# Check if the token has expired.
|
67 |
if service.auth_token_expires:
|
68 |
if (time() - mktime(service.auth_token_expires.timetuple())) > 0: |
69 |
raise Unauthorized('Authentication expired') |
70 |
except Service.DoesNotExist, e:
|
71 |
raise Unauthorized('Invalid X-Auth-Token') |
72 |
response = func(request, *args, **kwargs) |
73 |
return response
|
74 |
except Fault, fault:
|
75 |
return render_fault(request, fault)
|
76 |
except BaseException, e: |
77 |
logger.exception('Unexpected error: %s' % e)
|
78 |
fault = InternalServerError('Unexpected error')
|
79 |
return render_fault(request, fault)
|
80 |
return wrapper
|
81 |
return decorator
|
82 |
|
83 |
@csrf_exempt
|
84 |
@api_method(http_method='POST', token_required=True) |
85 |
def get_uuid_displayname_catalogs(request): |
86 |
# Normal Response Codes: 200
|
87 |
# Error Response Codes: internalServerError (500)
|
88 |
# badRequest (400)
|
89 |
# unauthorised (401)
|
90 |
|
91 |
return __get_uuid_displayname_catalogs(request, user_call=False) |
92 |
|
93 |
@csrf_exempt
|
94 |
@api_method(http_method='POST', token_required=True) |
95 |
def send_feedback(request, email_template_name='im/feedback_mail.txt'): |
96 |
# Normal Response Codes: 200
|
97 |
# Error Response Codes: internalServerError (500)
|
98 |
# badRequest (400)
|
99 |
# unauthorised (401)
|
100 |
|
101 |
return __send_feedback(request, email_template_name)
|