1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
Adapting Requests to Countries
------------------------------
Adapting a request to a country allows you to see where the request came from.
Here's a dummy request. Zope adds the REMOTE_ADDR CGI environment variable
for us. Upstream proxy servers (and tinkering users!) may also add
X-Forwarded-For: headers. The X-Forwarded-For: header takes precidence
if it is set.
>>> from canonical.launchpad.webapp.servers import LaunchpadTestRequest
>>> request = LaunchpadTestRequest(environ={
... 'HTTP_X_FORWARDED_FOR': '127.0.0.1,82.211.81.179',
... 'REMOTE_ADDR': '127.0.0.1',
... })
Here's us converting it to a country.
>>> from lp.services.worlddata.interfaces.country import ICountry
>>> country = ICountry(request)
>>> country.name
u'United Kingdom'
Sometimes we don't know where the request came from.
>>> request = LaunchpadTestRequest(
... environ={'REMOTE_ADDR': '255.255.255.255'})
>>> ICountry(request)
Traceback (most recent call last):
...
TypeError: ('Could not adapt', ...
|