use utils.requests in JsonCell (#15142)
This commit is contained in:
parent
0514083c58
commit
3926fa9ba8
|
@ -799,34 +799,23 @@ class JsonCell(CellBase):
|
|||
context = super(JsonCell, self).get_cell_extra_context(context)
|
||||
|
||||
url = utils.get_templated_url(self.url)
|
||||
cache_key = self.cache_key()
|
||||
json_content = cache.get(cache_key)
|
||||
|
||||
self._json_content = None
|
||||
|
||||
if not json_content:
|
||||
json_response = requests.get(url,
|
||||
headers={'Accept': 'application/json'},
|
||||
remote_service='auto')
|
||||
if json_response.status_code == 200:
|
||||
json_content = json_response.content
|
||||
try:
|
||||
self._json_content = json.loads(json_content)
|
||||
except ValueError:
|
||||
json_content = None
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.error('invalid json content')
|
||||
else:
|
||||
cache.set(cache_key, json_content, self.cache_duration)
|
||||
else:
|
||||
self._json_content = json.loads(json_content)
|
||||
json_response = utils.requests.get(url,
|
||||
headers={'Accept': 'application/json'},
|
||||
remote_service='auto',
|
||||
cache_duration=self.cache_duration)
|
||||
if json_response.status_code == 200:
|
||||
try:
|
||||
self._json_content = json.loads(json_response.content)
|
||||
except ValueError:
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.error('invalid json content')
|
||||
|
||||
context['json'] = self._json_content
|
||||
return context
|
||||
|
||||
def cache_key(self):
|
||||
return hashlib.md5(utils.get_templated_url(self.url)).hexdigest()
|
||||
|
||||
@property
|
||||
def template_name(self):
|
||||
json_content = self._json_content
|
||||
|
@ -841,8 +830,7 @@ class JsonCell(CellBase):
|
|||
return 'combo/json-cell.html'
|
||||
|
||||
def render(self, context):
|
||||
json_content = cache.get(self.cache_key())
|
||||
if not context.get('synchronous') and json_content is None:
|
||||
if not context.get('synchronous'):
|
||||
raise NothingInCacheException()
|
||||
context.update(self.get_cell_extra_context(context))
|
||||
if self.template_string:
|
||||
|
|
|
@ -150,7 +150,7 @@ def test_json_cell():
|
|||
cell._json_content = {'data': [{'foo': 'xxx', 'bar': 'xxx'}]}
|
||||
assert cell.template_name == 'combo/json-cell.html'
|
||||
|
||||
with mock.patch('combo.data.models.requests.get') as requests_get:
|
||||
with mock.patch('combo.utils.requests.get') as requests_get:
|
||||
data = {'data': [{'url': 'xxx', 'text': 'xxx'}]}
|
||||
requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200)
|
||||
context = cell.get_cell_extra_context({})
|
||||
|
@ -165,7 +165,7 @@ def test_json_cell():
|
|||
cell.url = 'http://test3'
|
||||
cell.render({})
|
||||
|
||||
with mock.patch('combo.data.models.requests.get') as requests_get:
|
||||
with mock.patch('combo.utils.requests.get') as requests_get:
|
||||
data = {'data': [{'url': 'http://a.b', 'text': 'xxx'}]}
|
||||
requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200)
|
||||
cell.url = 'http://test4'
|
||||
|
@ -176,7 +176,7 @@ def test_json_cell():
|
|||
result = cell.render(Context({'synchronous': True}))
|
||||
assert result == 'xxx'
|
||||
|
||||
with mock.patch('combo.data.models.requests.get') as requests_get:
|
||||
with mock.patch('combo.utils.requests.get') as requests_get:
|
||||
requests_get.return_value = mock.Mock(content='garbage', status_code=200)
|
||||
cell.url = 'http://test5'
|
||||
result = cell.render(Context({'synchronous': True}))
|
||||
|
|
Loading…
Reference in New Issue