Warning:
Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.
| File visit-from-params.diff,
6.2 KB
(added by Chris Arndt, 4 years ago) |
|
|
-
|
|
|
|
| 14 | 14 | |
| 15 | 15 | @expose() |
| 16 | 16 | 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 |
| 20 | 22 | 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) |
| 22 | 24 | |
| 23 | 25 | |
| 24 | 26 | class TestVisit(TestCase): |
| … |
… |
|
| 26 | 28 | def setUp(self): |
| 27 | 29 | testutil.stop_server(tg_only = True) |
| 28 | 30 | self._visit_on = config.get('visit.on', False) |
| | 31 | self._visit_source = config.get('visit.source', 'cookie') |
| 29 | 32 | config.update({'visit.on': True}) |
| 30 | 33 | self._visit_timeout = config.get('visit.timeout', 20) |
| 31 | 34 | config.update({'visit.timeout': 50}) |
| 32 | 35 | self.cookie_name = config.get("visit.cookie.name", 'tg-visit') |
| | 36 | self._visit_key_param = config.get("visit.param.visit_key", 'tg_visit') |
| 33 | 37 | self.app = testutil.make_app(VisitRoot) |
| 34 | 38 | testutil.start_server() |
| 35 | 39 | |
| … |
… |
|
| 53 | 57 | response = self.app.get("/") |
| 54 | 58 | # first visit's cookie |
| 55 | 59 | print "Headers", response.headers |
| 56 | | print "Config", config.get('visit.on') |
| | 60 | print "Visit on", config.get('visit.on') |
| 57 | 61 | morsel = response.cookies_set[self.cookie_name] |
| 58 | 62 | response = self.app.get("/", headers=cookie_header(response)) |
| 59 | 63 | assert not response.raw['new'] |
| 60 | 64 | |
| | 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 | |
| 61 | 86 | def test_cookie_expires(self): |
| 62 | 87 | """Test if the visit timeout mechanism works.""" |
| 63 | 88 | timeout = config.get('visit.timeout', 50) |
| … |
… |
|
| 107 | 132 | '%a, %d-%b-%Y %H:%M:%S GMT')[:8] + (0,)) |
| 108 | 133 | should_expire = time.mktime(time.gmtime()) + int(morsel['max-age']) |
| 109 | 134 | assert abs(should_expire - expires) < 3, (should_expire, expires, should_expire - expires) |
| 110 | | |
-
|
|
|
|
| 174 | 174 | def __init__(self): |
| 175 | 175 | log.info("Visit filter initialised") |
| 176 | 176 | 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(',')] |
| 177 | 180 | # Get the name to use for the identity cookie. |
| 178 | 181 | 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.") |
| 179 | 185 | # TODO: The path should probably default to whatever |
| 180 | 186 | # the root is masquerading as in the event of a |
| 181 | 187 | # virtual path filter. |
| … |
… |
|
| 196 | 202 | set_current(None) |
| 197 | 203 | return |
| 198 | 204 | visit = current() |
| 199 | | cookies = cherrypy.request.simple_cookie |
| 200 | 205 | 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: |
| 204 | 223 | visit = _manager.visit_for_key(visit_key) |
| 205 | 224 | if not visit: |
| 206 | 225 | visit_key = self._generate_key() |
-
|
|
|
|
| 92 | 92 | # Number of minutes a visit may be idle before it expires. |
| 93 | 93 | # visit.timeout=20 |
| 94 | 94 | |
| | 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 | |
| 95 | 100 | # The name of the cookie to transmit to the visitor's browser. |
| 96 | 101 | # visit.cookie.name="tg-visit" |
| 97 | 102 | |
| | 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 | |
| 98 | 107 | # Domain name to specify when setting the cookie (must begin with . according to |
| 99 | 108 | # RFC 2109). The default (None) should work for most cases and will default to |
| 100 | 109 | # the machine to which the request was made. NOTE: localhost is NEVER a valid |
Download in other formats: