wcs: fix tracking code with unknown wcs_site (#54195)
This commit is contained in:
parent
42e11b6cef
commit
7c1821ce35
|
@ -1032,7 +1032,7 @@ class TrackingCodeInputCell(CellBase):
|
|||
extra_context = super().get_cell_extra_context(context)
|
||||
if not self.wcs_site:
|
||||
self.wcs_site = list(get_wcs_services().keys())[0]
|
||||
extra_context['url'] = get_wcs_services().get(self.wcs_site).get('url')
|
||||
extra_context['url'] = (get_wcs_services().get(self.wcs_site) or {}).get('url')
|
||||
return extra_context
|
||||
|
||||
|
||||
|
|
|
@ -21,7 +21,6 @@ from django.conf import settings
|
|||
from django.contrib import messages
|
||||
from django.core.exceptions import DisallowedRedirect, PermissionDenied
|
||||
from django.http import HttpResponseBadRequest, HttpResponseRedirect, JsonResponse
|
||||
from django.urls import reverse
|
||||
from django.utils.http import urlquote
|
||||
from django.utils.six.moves.urllib import parse as urlparse
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
@ -62,6 +61,8 @@ class TrackingCodeView(View):
|
|||
raise PermissionDenied('rate limit reached (%s)' % rate_limit)
|
||||
|
||||
for wcs_site in wcs_sites:
|
||||
if not wcs_site:
|
||||
continue
|
||||
response = requests.get('/api/code/' + urlquote(code), remote_service=wcs_site, log_errors=False)
|
||||
if response.status_code == 200 and response.json().get('err') == 0:
|
||||
return response.json().get('load_url')
|
||||
|
@ -73,7 +74,7 @@ class TrackingCodeView(View):
|
|||
cell = TrackingCodeInputCell.objects.get(id=request.POST['cell'])
|
||||
except (KeyError, ValueError, TrackingCodeInputCell.DoesNotExist):
|
||||
return HttpResponseBadRequest('Invalid cell id')
|
||||
if not 'code' in request.POST:
|
||||
if 'code' not in request.POST:
|
||||
return HttpResponseBadRequest('Missing code')
|
||||
code = request.POST['code']
|
||||
|
||||
|
|
|
@ -1904,6 +1904,19 @@ def test_tracking_code_cell(app, nocache):
|
|||
resp = resp.follow()
|
||||
assert '<li class="error">The tracking code could not been found.</li>' in resp.text
|
||||
|
||||
# unknown wcs_site
|
||||
cell.wcs_site = 'unknown'
|
||||
cell.save()
|
||||
resp = app.get('/')
|
||||
resp.form['code'] = 'CNPHNTFB'
|
||||
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
||||
mock_json = mock.Mock(status_code=200)
|
||||
requests_get.return_value = mock_json
|
||||
resp = resp.form.submit()
|
||||
assert len(requests_get.call_args_list) == 0
|
||||
resp = resp.follow()
|
||||
assert '<li class="error">The tracking code could not been found.</li>' in resp.text
|
||||
|
||||
# simulate cell being displayed on a different site
|
||||
resp = app.get('/')
|
||||
resp.form['url'] = 'http://example.org/'
|
||||
|
|
Loading…
Reference in New Issue