Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / plankton / util.py @ f4366b6c

History | View | Annotate | Download (3.1 kB)

1 c34de90f Giorgos Verigakis
# Copyright 2011 GRNET S.A. All rights reserved.
2 c34de90f Giorgos Verigakis
#
3 c34de90f Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 c34de90f Giorgos Verigakis
# without modification, are permitted provided that the following
5 c34de90f Giorgos Verigakis
# conditions are met:
6 c34de90f Giorgos Verigakis
#
7 c34de90f Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 c34de90f Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 c34de90f Giorgos Verigakis
#      disclaimer.
10 c34de90f Giorgos Verigakis
#
11 c34de90f Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 c34de90f Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 c34de90f Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 c34de90f Giorgos Verigakis
#      provided with the distribution.
15 c34de90f Giorgos Verigakis
#
16 c34de90f Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 c34de90f Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 c34de90f Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 c34de90f Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 c34de90f Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 c34de90f Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 c34de90f Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 c34de90f Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 c34de90f Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 c34de90f Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 c34de90f Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 c34de90f Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 c34de90f Giorgos Verigakis
#
29 c34de90f Giorgos Verigakis
# The views and conclusions contained in the software and
30 c34de90f Giorgos Verigakis
# documentation are those of the authors and should not be
31 c34de90f Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 c34de90f Giorgos Verigakis
# or implied, of GRNET S.A.
33 c34de90f Giorgos Verigakis
34 c34de90f Giorgos Verigakis
from functools import wraps
35 2035039b Giorgos Verigakis
from logging import getLogger
36 15137c54 Giorgos Verigakis
from traceback import format_exc
37 c34de90f Giorgos Verigakis
38 15137c54 Giorgos Verigakis
from django.conf import settings
39 15137c54 Giorgos Verigakis
from django.http import (HttpResponse, HttpResponseBadRequest,
40 1f478427 Sofia Papagiannaki
                         HttpResponseServerError, HttpResponseForbidden)
41 c34de90f Giorgos Verigakis
42 4b3b8688 Giorgos Verigakis
from synnefo.lib.astakos import get_user
43 2db7d9df Christos Stavrakakis
from synnefo.plankton.backend import (ImageBackend, BackendException,
44 2db7d9df Christos Stavrakakis
                                      NotAllowedError)
45 0a72907b Giorgos Verigakis
46 0a72907b Giorgos Verigakis
log = getLogger('synnefo.plankton')
47 c34de90f Giorgos Verigakis
48 c34de90f Giorgos Verigakis
49 c34de90f Giorgos Verigakis
def plankton_method(method):
50 c34de90f Giorgos Verigakis
    def decorator(func):
51 c34de90f Giorgos Verigakis
        @wraps(func)
52 c34de90f Giorgos Verigakis
        def wrapper(request, *args, **kwargs):
53 c34de90f Giorgos Verigakis
            try:
54 4b3b8688 Giorgos Verigakis
                get_user(request, settings.ASTAKOS_URL)
55 4b3b8688 Giorgos Verigakis
                if not request.user_uniq:
56 5a497049 Giorgos Verigakis
                    return HttpResponse(status=401)
57 3d653f72 Giorgos Verigakis
                if request.method != method:
58 3d653f72 Giorgos Verigakis
                    return HttpResponse(status=405)
59 4b3b8688 Giorgos Verigakis
                request.backend = ImageBackend(request.user_uniq)
60 c34de90f Giorgos Verigakis
                return func(request, *args, **kwargs)
61 c34de90f Giorgos Verigakis
            except (AssertionError, BackendException) as e:
62 c34de90f Giorgos Verigakis
                message = e.args[0] if e.args else ''
63 c34de90f Giorgos Verigakis
                return HttpResponseBadRequest(message)
64 1f478427 Sofia Papagiannaki
            except NotAllowedError:
65 1f478427 Sofia Papagiannaki
                return HttpResponseForbidden()
66 15137c54 Giorgos Verigakis
            except Exception as e:
67 15137c54 Giorgos Verigakis
                if settings.DEBUG:
68 15137c54 Giorgos Verigakis
                    message = format_exc(e)
69 15137c54 Giorgos Verigakis
                else:
70 15137c54 Giorgos Verigakis
                    message = ''
71 0a72907b Giorgos Verigakis
                log.exception(e)
72 15137c54 Giorgos Verigakis
                return HttpResponseServerError(message)
73 c34de90f Giorgos Verigakis
            finally:
74 3d653f72 Giorgos Verigakis
                if hasattr(request, 'backend'):
75 3d653f72 Giorgos Verigakis
                    request.backend.close()
76 c34de90f Giorgos Verigakis
        return wrapper
77 c34de90f Giorgos Verigakis
    return decorator