Re: Question about gae-sessions

25 views
Skip to first unread message

David Underhill

unread,
Jul 14, 2011, 4:24:22 PM7/14/11
to Alexander Marquardt, gae-se...@googlegroups.com
Thanks Alex; I'm glad gae-sessions has come in handy; good luck with your project!

~ David


On Thu, Jul 14, 2011 at 11:51, Alexander Marquardt <alexander...@gmail.com> wrote:
Looks good, I think I'll probably do it the way you have suggested. By the way your gae-sessions code is awesome !  Very logical, concise, and easy to understand .

-Alex


On Thu, Jul 14, 2011 at 8:43 PM, David Underhill <dou...@gmail.com> wrote:
Awesome, thanks for tracking that down Alexander.

I'll patch gae-sessions.  I think I'll tweak the one line which access request.session like this (should be functionally equivalent to your patch though):

        if hasattr(request, 'session') and request.session.is_accessed():

~ David


On Wed, Jul 13, 2011 at 04:53, Alexander Marquardt <alexander...@gmail.com> wrote:
Hi David,

Here is a fix for people who have multiple middleware calls. 

For example, I have middleware that will re-map URLs depending on the users language setting - this will set the response to be an Http-redirect before the DjangoSessionMiddleware process_request() gets called - therefore the session is not set, but it doesn't actually need to be set since it will be set on when the re-direct is called (can therefore safely be ignored)

The following implementation is how they have fixed this problem in the standard django session middleware. :
        
    def process_response(self, request, response):
           
        try:
            session = request.session
        except AttributeError:
            pass
        else:
            if self.response_handler:
                session_headers = self.response_handler(None, [], None)
                for k,v in session_headers:
                    response[k] = v
                self.response_handler = None
            if request.session.is_accessed():
                from django.utils.cache import patch_vary_headers
                logging.debug("Varying")
                patch_vary_headers(response, ('Cookie',))
                
        return response

On Wed, Jul 13, 2011 at 12:06 PM, Alexander Marquardt <alexander...@gmail.com> wrote:
Hi David,

I figure it out .. basically, if you have a process_request() in any middleware before the DjangoSessionMiddleware, then the  process_request() for the Session middleware (which is where you add the session onto the request) will not execute.

So, this problem had nothing to do with you code --  if someone has already implemented a process request() in another middleware package, the DjanoSessionMiddleware process_request() will not be called (maybe this is obvious and I just didn't understand the Django framework well enough to see it earlier)

-Alex


On Tue, Jul 12, 2011 at 9:40 PM, Alexander Marquardt <alexander...@gmail.com> wrote:
These are the relevant settings in the settings file .. even after commenting out all the other settings references, the same error occurs .. 


MIDDLEWARE_CLASSES = (
    # keep the following line before CommonMiddleware, or APPEND_SLASH will not work
    'localeurl.middleware.LocaleURLMiddleware',
    
    'django.middleware.common.CommonMiddleware',
    #  'django.contrib.sessions.middleware.SessionMiddleware',
    'gaesessions.DjangoSessionMiddleware',

)

INSTALLED_APPS = (
    'appengine_django',
    'localeurl',
#    'django.contrib.sessions',
    'django.contrib.admin',
    'rs',
)

# SESSION_ENGINE = ('django.contrib.sessions.backends.cached_db')
# SESSION_ENGINE = ('django.contrib.sessions.backends.db')
# SESSION_ENGINE = ('appengine_django.sessions.backends.db')
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
SESSION_SAVE_EVERY_REQUEST = False


On Tue, Jul 12, 2011 at 9:25 PM, Alexander Marquardt <alexander...@gmail.com> wrote:
Hi David, thanks for your response.

I have incorporated the DjangoSessionMiddleware into the MIDDLEWARE_CLASSES .. so I am not sure where I am going wrong ...

Also (just for your information), there are other references to sessions (from my previous implementation of sessions that used the appengine_django helper sessions functionality), that I am not sure about enabling or disabling with your software

INSTALLED_APPS has 'django.contrib.sessions' enabled, and SESSION_ENGINE can be set to appenging_django.sessions.backends.db (or it can be removed)..

With all the different options and combinations, I thought it would be useful if there were a django example available. Let me know if you have any suggestions, or might have a small example that I could try out ..

PS. Up until now, I have been using the built-in appengine_django helper sessions which work fine - but I wanted to try out your program since it looks cleaner and easier to understand. 

It could very well just be a conflict between the old sessions implementation and your session handler.. I'll let you know if I figure out the problem, or look forward to any suggestions that you might have

Thanks and kind regards
Alexander

On Tue, Jul 12, 2011 at 9:07 PM, David Underhill <dou...@gmail.com> wrote:
Hi Alexander,

Have you tried incorporating the DjangoSessionMiddleware?  This middleware class should be installable just like any other Django middleware, and once used it should take care of initializing the session object for you on each request.

~ David


On Tue, Jul 12, 2011 at 10:33, Alexander Marquardt <alexander...@gmail.com> wrote:
Hi David,

I have been experimenting with gae-sessions, and it looks really great .. however, I was curious if you know of a Django example implementation to help me understand where I am going wrong. I am running a website that uses fully custom logins (ie. no google accounts and no OpenID)

On line 490 in __init__.py (if request.session.is_accessed():), the program exits with the following error when trying to load the main page (ie. before a session or login even exists):

AttributeError: 'WSGIRequest' object has no attribute 'session' 

Which makes sense considering that I have not yet assigned the session to the request object -- my problem is that I don't really know the best way (where or how) to add the session to the request object. For this reason, it would be useful to see an example Django implementation if one is available, or alternatively if you have any idea about how I should add the session to the request object (or perhaps ignore the session if it has not yet been assigned).. 

Kind regards
Alexander Marquardt








Reply all
Reply to author
Forward
0 new messages