Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #1927: visit-from-params.diff

File visit-from-params.diff, 6.2 KB (added by Chris Arndt, 4 years ago)
  • turbogears/identity/tests/test_visit.py

     
    1414 
    1515    @expose() 
    1616    def index(self): 
    17         new = None 
    18         if visit.current(): 
    19             new = visit.current().is_new 
     17        new = visit_key = None 
     18        cur_visit = visit.current() 
     19        if cur_visit: 
     20            new = cur_visit.is_new 
     21            key = cur_visit.key 
    2022        visit_on = config.get('visit.on') 
    21         return dict(new=new, visit_on=visit_on) 
     23        return dict(new=new, key=key, visit_on=visit_on) 
    2224 
    2325 
    2426class TestVisit(TestCase): 
     
    2628    def setUp(self): 
    2729        testutil.stop_server(tg_only = True) 
    2830        self._visit_on = config.get('visit.on', False) 
     31        self._visit_source = config.get('visit.source', 'cookie') 
    2932        config.update({'visit.on': True}) 
    3033        self._visit_timeout = config.get('visit.timeout', 20) 
    3134        config.update({'visit.timeout': 50}) 
    3235        self.cookie_name = config.get("visit.cookie.name", 'tg-visit') 
     36        self._visit_key_param = config.get("visit.param.visit_key", 'tg_visit') 
    3337        self.app = testutil.make_app(VisitRoot) 
    3438        testutil.start_server() 
    3539 
     
    5357        response = self.app.get("/") 
    5458        # first visit's cookie 
    5559        print "Headers", response.headers 
    56         print "Config", config.get('visit.on') 
     60        print "Visit on", config.get('visit.on') 
    5761        morsel = response.cookies_set[self.cookie_name] 
    5862        response = self.app.get("/", headers=cookie_header(response)) 
    5963        assert not response.raw['new'] 
    6064 
     65    def test_visit_from_params(self): 
     66        """Test if the visit key is retrieved from the request params.""" 
     67        _app = self.app 
     68        try: 
     69            testutil.stop_server(tg_only = True) 
     70            config.update({'visit.source': 'cookie,param'}) 
     71            self.app = testutil.make_app(VisitRoot) 
     72            testutil.start_server() 
     73            response = self.app.get("/") 
     74            # first visit's cookie 
     75            first_key = response.raw.get('key') 
     76            # give the visit extension time to set up its model tables 
     77            time.sleep(2) 
     78            self.app.cookies = {} 
     79            response = self.app.get("/", 
     80                params={self._visit_key_param: first_key}) 
     81        finally: 
     82            config.update({'visit.source': self._visit_source}) 
     83            self.app = _app 
     84        assert first_key == response.raw['key'] 
     85 
    6186    def test_cookie_expires(self): 
    6287        """Test if the visit timeout mechanism works.""" 
    6388        timeout = config.get('visit.timeout', 50) 
     
    107132            '%a, %d-%b-%Y %H:%M:%S GMT')[:8] + (0,)) 
    108133        should_expire = time.mktime(time.gmtime()) + int(morsel['max-age']) 
    109134        assert abs(should_expire - expires) < 3, (should_expire, expires, should_expire - expires) 
    110  
  • turbogears/visit/api.py

     
    174174    def __init__(self): 
    175175        log.info("Visit filter initialised") 
    176176        get = config.get 
     177        # Where to look for the session key in the reqeust and in which order 
     178        self.source = [s.strip().lower() for s in 
     179            get("visit.source", "cookie").split(',')] 
    177180        # Get the name to use for the identity cookie. 
    178181        self.cookie_name = get("visit.cookie.name", "tg-visit") 
     182        self.visit_key_param = get("visit.param.visit_key", 'tg_visit') 
     183        if set(self.source).difference(('cookie', 'param')): 
     184            log.warning("Unsupported 'visit.source' '%s' in configuration.") 
    179185        # TODO: The path should probably default to whatever 
    180186        # the root is masquerading as in the event of a 
    181187        # virtual path filter. 
     
    196202            set_current(None) 
    197203            return 
    198204        visit = current() 
    199         cookies = cherrypy.request.simple_cookie 
    200205        if not visit: 
    201             if self.cookie_name in cookies: 
    202                 # Process visit based on cookie 
    203                 visit_key = cookies[self.cookie_name].value 
     206            visit_key = None 
     207            for source in self.source: 
     208                if source == 'cookie': 
     209                    cookies = cherrypy.request.simple_cookie 
     210                    if self.cookie_name in cookies: 
     211                        # Get visit key from cookie 
     212                        visit_key = cookies[self.cookie_name].value 
     213                        break 
     214                elif source == 'param': 
     215                    # Get visit key from request parameters (GET _or_ POST) 
     216                    try: 
     217                        visit_key = cherrypy.request.params[self.visit_key_param] 
     218                        del cherrypy.request.params[self.visit_key_param] 
     219                    except KeyError: 
     220                        pass 
     221                    break 
     222            if visit_key: 
    204223                visit = _manager.visit_for_key(visit_key) 
    205224            if not visit: 
    206225                visit_key = self._generate_key() 
  • turbogears/qstemplates/quickstart/+package+/config/app.cfg_tmpl

     
    9292# Number of minutes a visit may be idle before it expires. 
    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', 'param'. 
     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" 
    97102 
     103# The name of the request parameter with the session key (for when 
     104# 'visit.source' includes 'param'). Name MUST NOT contain dashes or dots! 
     105# visit.param.visit_key="tg_visit" 
     106 
    98107# Domain name to specify when setting the cookie (must begin with . according to 
    99108# RFC 2109). The default (None) should work for most cases and will default to 
    100109# the machine to which the request was made. NOTE: localhost is NEVER a valid