commit a421e0b9fdc8e4e9d98ae08bef0bd9c66f015a20
Author: Sofia Papagiannaki <spapagian@grnet.gr>
Date:   Thu Jul 18 19:58:24 2013 +0300

    pithos: Update view_method decorator
    
    * If no token found in the cookie redirect to astakos login
    with next parameter pointing to the request path
    * Extend the list of the valid response statuses
    * If no expected response status is received, pass to the raised
    exception the response status instead of the whole response.
    
    Conflicts:
    	snf-pithos-app/pithos/api/settings.py
    	snf-pithos-app/pithos/api/util.py

diff --git a/snf-pithos-app/pithos/api/settings.py b/snf-pithos-app/pithos/api/settings.py
index ba42dd6..8cd2ba0 100644
--- a/snf-pithos-app/pithos/api/settings.py
+++ b/snf-pithos-app/pithos/api/settings.py
@@ -1,7 +1,7 @@
 #coding=utf8
 from django.conf import settings
 from synnefo.lib import parse_base_url, join_urls
-from synnefo.lib.services import fill_endpoints
+from synnefo.lib.services import fill_endpoints, get_public_endpoint
 from synnefo.util.keypath import get_path, set_path
 from pithos.api.services import pithos_services as vanilla_pithos_services
 from astakosclient import astakos_services as vanilla_astakos_services
@@ -103,5 +103,9 @@ PUBLIC_URL_SECURITY =  getattr(settings, 'PITHOS_PUBLIC_URL_SECURITY', 16)
 PUBLIC_URL_ALPHABET =  getattr(
     settings,
     'PITHOS_PUBLIC_URL_ALPHABET',
-    '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
-)
+    '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
+
+ASTAKOS_UI_URL = get_public_endpoint(astakos_services, 'astakos_ui', '').rstrip('/')
+
+# Astakos login URL to redirect if the user information is missing
+LOGIN_URL = join_urls(ASTAKOS_UI_URL, 'login')
diff --git a/snf-pithos-app/pithos/api/util.py b/snf-pithos-app/pithos/api/util.py
index ba254f2..6159d14 100644
--- a/snf-pithos-app/pithos/api/util.py
+++ b/snf-pithos-app/pithos/api/util.py
@@ -35,7 +35,8 @@ from functools import wraps
 from datetime import datetime
 from urllib import quote, unquote
 
-from django.http import HttpResponse, Http404, HttpResponseForbidden
+from django.http import (HttpResponse, HttpResponseRedirect, Http404,
+                         HttpResponseForbidden)
 from django.template.loader import render_to_string
 from django.utils import simplejson as json
 from django.utils.http import http_date, parse_etags
@@ -63,7 +64,7 @@ from pithos.api.settings import (BACKEND_DB_MODULE, BACKEND_DB_CONNECTION,
                                  RADOS_POOL_MAPS, TRANSLATE_UUIDS,
                                  PUBLIC_URL_SECURITY,
                                  PUBLIC_URL_ALPHABET,
-                                 COOKIE_NAME, BASE_HOST)
+                                 COOKIE_NAME, BASE_HOST, LOGIN_URL)
 from pithos.api.resources import resources
 from pithos.backends.base import (NotAllowedError, QuotaError, ItemNotExists,
                                   VersionNotExists)
@@ -1113,16 +1114,21 @@ def view_method():
     def decorator(func):
         @wraps(func)
         def wrapper(request, *args, **kwargs):
-            request.META['HTTP_X_AUTH_TOKEN'] = get_token_from_cookie(request)
+            token = get_token_from_cookie(request)
+            if token is None:
+                return HttpResponseRedirect('%s?next=%s' % (LOGIN_URL,
+                                                            request.path))
+            request.META['HTTP_X_AUTH_TOKEN'] = token
             # Get the response object
             response = func(request, *args, **kwargs)
-            if response.status_code == 200:
+            if response.status_code in [200, 206, 304, 412, 416]:
                 return response
             elif response.status_code == 404:
                 raise Http404()
             elif response.status_code in [401, 403]:
                 return HttpResponseForbidden()
             else:
-                raise Exception(response)
+                # unexpected response status
+                raise Exception(response.status_code)
         return wrapper
     return decorator
