Changeset 5175

Show
Ignore:
Timestamp:
08/20/08 12:21:52 (3 months ago)
Author:
carndt
Message:

Applied second patch from #1927 with changes proposed in comment no. 5

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/turbogears/identity/tests/test_visit.py

    r4695 r5175  
    11import time 
     2 
     3from Cookie import SimpleCookie 
    24from unittest import TestCase 
     5 
    36import cherrypy 
     7 
    48from turbogears import config, controllers, expose, startup, testutil, visit 
    5 from Cookie import SimpleCookie 
    69 
    710 
     
    1518    @expose() 
    1619    def index(self): 
    17         new = None 
    18         if visit.current(): 
    19             new = visit.current().is_new 
     20        new = visit_key = None 
     21        cur_visit = visit.current() 
     22        if cur_visit: 
     23            new = cur_visit.is_new 
     24            key = cur_visit.key 
    2025        visit_on = config.get('visit.on') 
    21         return dict(new=new, visit_on=visit_on) 
     26        return dict(new=new, key=key, visit_on=visit_on) 
    2227 
    2328 
     
    2732        testutil.stop_server(tg_only = True) 
    2833        self._visit_on = config.get('visit.on', False) 
     34        self._visit_source = config.get('visit.source', 'cookie') 
    2935        config.update({'visit.on': True}) 
    3036        self._visit_timeout = config.get('visit.timeout', 20) 
    3137        config.update({'visit.timeout': 50}) 
    3238        self.cookie_name = config.get("visit.cookie.name", 'tg-visit') 
     39        self._visit_key_param = config.get("visit.form.name", 'tg_visit') 
    3340        self.app = testutil.make_app(VisitRoot) 
    3441        testutil.start_server() 
     
    5461        # first visit's cookie 
    5562        print "Headers", response.headers 
    56         print "Config", config.get('visit.on') 
     63        print "Visit on", config.get('visit.on') 
    5764        morsel = response.cookies_set[self.cookie_name] 
    5865        response = self.app.get("/", headers=cookie_header(response)) 
    5966        assert not response.raw['new'] 
     67 
     68    def test_visit_from_form(self): 
     69        """Test if the visit key is retrieved from the request params.""" 
     70        _app = self.app 
     71        try: 
     72            testutil.stop_server(tg_only = True) 
     73            config.update({'visit.source': 'cookie,form'}) 
     74            self.app = testutil.make_app(VisitRoot) 
     75            testutil.start_server() 
     76            # first visit's cookie 
     77            first_key = self.app.get("/").raw.get('key') 
     78            # second request with no cookies 
     79            self.app.cookies = {} 
     80            response = self.app.get("/", 
     81                params={self._visit_key_param: first_key}) 
     82        finally: 
     83            config.update({'visit.source': self._visit_source}) 
     84            self.app = _app 
     85        assert first_key == response.raw['key'] 
     86 
     87    def test_visit_from_form_with_cookie_fallback(self): 
     88        """Test if visit key is retrieved from cookie if not found in params.""" 
     89        _app = self.app 
     90        try: 
     91            testutil.stop_server(tg_only = True) 
     92            config.update({'visit.source': 'form,cookie'}) 
     93            _app = testutil.make_app(VisitRoot) 
     94            testutil.start_server() 
     95            # first visit's cookie 
     96            first_key = self.app.get("/").raw.get('key') 
     97            # second request with no params 
     98            response = self.app.get("/") 
     99        finally: 
     100            config.update({'visit.source': self._visit_source}) 
     101            self.app = _app 
     102        assert first_key == response.raw['key'] 
    60103 
    61104    def test_cookie_expires(self): 
     
    108151        should_expire = time.mktime(time.gmtime()) + int(morsel['max-age']) 
    109152        assert abs(should_expire - expires) < 3, (should_expire, expires, should_expire - expires) 
    110  
  • branches/1.1/turbogears/qstemplates/quickstart/+package+/config/app.cfg_tmpl

    r4815 r5175  
    9393# visit.timeout=20 
    9494 
     95# Where to look for the key of an existing visit in the request and in which 
     96# order. Comma-separated list of possible values: 'cookie', 'form'. 
     97# By default only use visit key in session cookie. 
     98# visit.source="cookie" 
     99 
    95100# The name of the cookie to transmit to the visitor's browser. 
    96101# visit.cookie.name="tg-visit" 
     102 
     103# The name of the request parameter with the session key (for when 
     104# 'visit.source' includes 'form'). Name MUST NOT contain dashes or dots! 
     105# visit.form.name="tg_visit" 
    97106 
    98107# Domain name to specify when setting the cookie (must begin with . according to 
  • branches/1.1/turbogears/visit/api.py

    r4695 r5175  
    22import sha 
    33import threading 
     4import time 
     5 
    46from random import random 
    57from datetime import timedelta, datetime 
    6 import time 
     8 
     9import cherrypy 
    710import pkg_resources 
    8 import cherrypy 
     11 
    912from cherrypy.filters.basefilter import BaseFilter 
    1013from turbogears import config 
     
    175178        log.info("Visit filter initialised") 
    176179        get = config.get 
     180        # Where to look for the session key in the request and in which order 
     181        self.source = [s.strip().lower() for s in 
     182            get("visit.source", "cookie").split(',')] 
     183        if set(self.source).difference(('cookie', 'form')): 
     184            log.warning("Unsupported 'visit.source' '%s' in configuration.") 
    177185        # Get the name to use for the identity cookie. 
    178186        self.cookie_name = get("visit.cookie.name", "tg-visit") 
     187        # and the name of the request param. MUST NOT contain dashes or dots, 
     188        # otherwise the NestedVariablesFilter will chocke on it. 
     189        self.visit_key_param = get("visit.form.name", "tg_visit") 
    179190        # TODO: The path should probably default to whatever 
    180191        # the root is masquerading as in the event of a 
     
    196207            set_current(None) 
    197208            return 
     209        cpreq = cherrypy.request 
    198210        visit = current() 
    199         cookies = cherrypy.request.simple_cookie 
    200211        if not visit: 
    201             if self.cookie_name in cookies: 
    202                 # Process visit based on cookie 
    203                 visit_key = cookies[self.cookie_name].value 
    204                 visit = _manager.visit_for_key(visit_key) 
     212            visit_key = None 
     213            for source in self.source: 
     214                if source == 'cookie': 
     215                    visit_key = cpreq.simple_cookie.get(self.cookie_name) 
     216                    if visit_key: 
     217                        visit_key = visit_key.value 
     218                        log.debug("Retrieved visit key '%s' from cookie '%s'.", 
     219                            visit_key, self.cookie_name) 
     220                elif source == 'form': 
     221                    visit_key = cpreq.params.pop(self.visit_key_param, None) 
     222                    log.debug( 
     223                        "Retrieved visit key '%s' from request param '%s'.", 
     224                        visit_key, self.visit_key_param) 
     225                if visit_key: 
     226                    visit = _manager.visit_for_key(visit_key) 
     227                    break 
    205228            if not visit: 
    206229                visit_key = self._generate_key() 
    207230                visit = _manager.new_visit_with_key(visit_key) 
     231                log.debug("Created new visit with key: %s", visit_key) 
     232            else: 
     233                log.debug("Using visit from request with key: %s", visit_key) 
    208234            self.send_cookie(visit_key) 
    209235            set_current(visit)