1. hideki nara
  2. django-bpmobile

Commits

hideki nara  committed 981a5ba

check if request has the session or not

  • Participants
  • Parent commits a82480d
  • Branches default

Comments (0)

Files changed (1)

File bpmobile/middleware.py

View file
 from django.utils.functional import SimpleLazyObject
 from django.contrib.auth import get_user
 from django.contrib.auth.middleware import AuthenticationMiddleware
+from django.contrib.sessions.middleware import  SessionMiddleware
 
 import logging,traceback
 log = logging.getLogger(__name__)
 
         request.GET._mutable = True
         request.POST._mutable = True
-#        _convert_dict(request.GET, request.agent)      
+#        _convert_dict(request.GET, request.agent)
 #        _convert_dict(request.POST, request.agent)
         request.GET._mutable = False
         request.POST._mutable = False
                     c = utils.RE_UNI_EMOJI_DOCOMO.sub(lambda m:utils.uni_docomo2softbank(m.group()), c)
                     response.content = c.encode('utf8','replace')
                 response['content-type'] = 'application/xhtml+xml; charset=%s' % encoding
-        
+
         return response
 
 class BPMobileSessionMiddleware(object):
     def get_agent(self, request):
         agent= getattr(request, 'agent', uamobile.detect(request.META))
         if agent:
-            setattr(agent,'device_class', 'pc' if agent.is_nonmobile() else 'fp' ) 
+            setattr(agent,'device_class', 'pc' if agent.is_nonmobile() else 'fp' )
         return agent
 
     def get_cache_key(self, guid):
                 session_key = None
         else:
             session_key = request.COOKIES.get(settings.SESSION_COOKIE_NAME, None)
-            
+
         request.session = engine.SessionStore(session_key)
 
     def process_response(self, request, response):
                 if not agent.is_docomo():
 
                     response.set_cookie(settings.SESSION_COOKIE_NAME,
-                            request.session.session_key, 
+                            request.session.session_key,
                             max_age=max_age,
-                            expires=expires, 
+                            expires=expires,
                             domain=settings.SESSION_COOKIE_DOMAIN,
                             path=settings.SESSION_COOKIE_PATH,
                             secure=settings.SESSION_COOKIE_SECURE or None)
 
 ####################################################################
 
-from django.contrib.sessions.middleware import  SessionMiddleware
 class UrlBasedMoobileSessionMiddleware(SessionMiddleware):
     cache_key_name = 'session_key_%s'
 
     def get_agent(self, request):
         agent= getattr(request, 'agent', uamobile.detect(request.META))
         if agent:
-            setattr(agent,'device_class', 'pc' if agent.is_nonmobile() else 'fp' ) 
+            setattr(agent,'device_class', 'pc' if agent.is_nonmobile() else 'fp' )
         return agent
 
     def get_cache_key(self, guid):
         if all([ getattr(request,'user',None),
                   getattr(request.user, 'session', None) ]):
             #:すでに別のみどるウエアでセッションとユーザーが復元されていたら何もしない
-            return 
+            return
 
         engine = import_module(settings.SESSION_ENGINE)
         params = utils.from_url(request.path_info,request.path)
         session_key=None
         if params :
             #: URLにセッションキーがあるので
-            session_key = params['session_key']   
+            session_key = params['session_key']
             request.path_info = params['path_info']  #:rewrite
             request.path =  params['path']           #:rewrite
             request.session = engine.SessionStore(session_key)
 
-        elif agent.is_docomo() or agent.is_ezweb() : 
+        elif agent.is_docomo() or agent.is_ezweb() :
             #:セッションがないはずなので新規
             request.session = engine.SessionStore()
 
         request.session.save() #:jus in case
 
-        #: CSRF 
+        #: CSRF
         request.COOKIES[global_settings.CSRF_COOKIE_NAME ] =  request.session.get('last_csrf_cookie',
                                                                     request.POST.get('csrfmiddlewaretoken',''))
 
         if request.is_secure() and not request.META.get('HTTP_REFERER'):
             #:リファラを送らないクソ端末があります。
-            request.META['HTTP_REFERER'] = 'https://%s/' % request.get_host() 
+            request.META['HTTP_REFERER'] = 'https://%s/' % request.get_host()
 
         delattr(request, '_cached_user' ) #:ログインユーザーをチャラパーにする
         AuthenticationMiddleware().process_request(request)     #:request.user が用意されます
 
     def process_response(self, request, response):
-        request.session['last_csrf_cookie'] = request.META.get("CSRF_COOKIE",'')
+        if hasattr(request, 'session'):
+            request.session['last_csrf_cookie'] = request.META.get("CSRF_COOKIE", '')
         return super(UrlBasedMoobileSessionMiddleware, self).process_response(request, response)