0001-misc-use-legacy-urls-to-call-up-to-date-urls-64344.patch
chrono/utils/publik_urls.py | ||
---|---|---|
19 | 19 |
from django.conf import settings |
20 | 20 | |
21 | 21 | |
22 |
def translate_from_legacy_urls(url): |
|
23 |
if not url: |
|
24 |
return '' |
|
25 |
legacy_urls_mapping = getattr(settings, 'LEGACY_URLS_MAPPING', None) |
|
26 |
if not legacy_urls_mapping: |
|
27 |
return url |
|
28 |
splitted_url = urllib.parse.urlparse(url) |
|
29 |
hostname = splitted_url.netloc |
|
30 |
if hostname not in legacy_urls_mapping: |
|
31 |
return url |
|
32 |
return splitted_url._replace(netloc=legacy_urls_mapping[hostname]).geturl() |
|
33 | ||
34 | ||
22 | 35 |
def translate_from_publik_url(url): |
23 | 36 |
if not url: |
24 | 37 |
return '' |
chrono/utils/requests_wrapper.py | ||
---|---|---|
27 | 27 |
from requests import Session as RequestsSession |
28 | 28 |
from requests.auth import AuthBase |
29 | 29 | |
30 |
from .publik_urls import translate_from_publik_url |
|
30 |
from .publik_urls import translate_from_legacy_urls, translate_from_publik_url
|
|
31 | 31 |
from .signature import sign_url |
32 | 32 | |
33 | 33 | |
... | ... | |
47 | 47 |
class Requests(RequestsSession): |
48 | 48 |
def request(self, method, url, **kwargs): |
49 | 49 |
url = translate_from_publik_url(url) |
50 |
url = translate_from_legacy_urls(url) |
|
50 | 51 |
remote_service = kwargs.pop('remote_service', None) |
51 | 52 |
cache_duration = kwargs.pop('cache_duration', 15) |
52 | 53 |
invalidate_cache = kwargs.pop('invalidate_cache', False) |
tests/settings.py | ||
---|---|---|
29 | 29 |
}, |
30 | 30 |
} |
31 | 31 | |
32 |
LEGACY_URLS_MAPPING = {'old.org': 'new.org'} |
|
33 | ||
32 | 34 |
EXCEPTIONS_SOURCES = {} |
33 | 35 | |
34 | 36 |
SITE_BASE_URL = 'https://example.com' |
tests/test_requests.py | ||
---|---|---|
145 | 145 | |
146 | 146 |
# check with unicode url |
147 | 147 |
assert requests.get('http://cache.example.org/éléphant').content == b'hello second world' |
148 | ||
149 | ||
150 |
def test_requests_to_legacy_urls(): |
|
151 |
with mock.patch('chrono.utils.requests_wrapper.RequestsSession.request') as requests_get: |
|
152 |
requests_get.return_value = mock.Mock(content=b'hello world', status_code=200) |
|
153 |
assert requests.get('http://old.org/').content == b'hello world' |
|
154 |
assert requests_get.call_args.args[1] == 'http://new.org/' |
|
148 |
- |