832 |
832 |
varnames = None
|
833 |
833 |
force_async = False
|
834 |
834 |
actions = {}
|
|
835 |
additional_data = None
|
|
836 |
# [
|
|
837 |
# {'key': ...,
|
|
838 |
# 'url': ...,
|
|
839 |
# 'cache_duration': ... (optional)
|
|
840 |
# },
|
|
841 |
# ...
|
|
842 |
# ]
|
835 |
843 |
|
836 |
844 |
_json_content = None
|
837 |
845 |
|
... | ... | |
848 |
856 |
if varname in context['request'].GET and varname not in context:
|
849 |
857 |
context[varname] = context['request'].GET[varname]
|
850 |
858 |
self._json_content = None
|
851 |
|
extra_context['json'] = None
|
852 |
|
try:
|
853 |
|
url = utils.get_templated_url(self.url, context)
|
854 |
|
except utils.UnknownTemplateVariableError:
|
855 |
|
logger = logging.getLogger(__name__)
|
856 |
|
logger.warning('unknown variable in template URL (%s)', self.url)
|
857 |
|
return extra_context
|
858 |
|
json_response = utils.requests.get(url,
|
859 |
|
headers={'Accept': 'application/json'},
|
860 |
|
remote_service='auto',
|
861 |
|
cache_duration=self.cache_duration,
|
862 |
|
without_user=True,
|
863 |
|
raise_if_not_cached=not(context.get('synchronous')),
|
864 |
|
invalidate_cache=invalidate_cache,
|
865 |
|
)
|
866 |
|
if json_response.status_code == 200:
|
|
859 |
|
|
860 |
data_urls = [{'key': 'json', 'url': self.url, 'cache_duration': self.cache_duration}]
|
|
861 |
data_urls.extend(self.additional_data or [])
|
|
862 |
|
|
863 |
for data_url_dict in data_urls:
|
|
864 |
extra_context[data_url_dict['key']] = None
|
|
865 |
|
|
866 |
for data_url_dict in data_urls:
|
|
867 |
data_key = data_url_dict['key']
|
867 |
868 |
try:
|
868 |
|
self._json_content = json.loads(json_response.content)
|
869 |
|
except ValueError:
|
|
869 |
url = utils.get_templated_url(data_url_dict['url'], context)
|
|
870 |
except utils.UnknownTemplateVariableError:
|
870 |
871 |
logger = logging.getLogger(__name__)
|
871 |
|
logger.error('invalid json content (%s)', url)
|
872 |
|
extra_context['json'] = self._json_content
|
|
872 |
logger.warning('unknown variable in template URL (%s)', self.url)
|
|
873 |
continue
|
|
874 |
json_response = utils.requests.get(url,
|
|
875 |
headers={'Accept': 'application/json'},
|
|
876 |
remote_service='auto',
|
|
877 |
cache_duration=data_url_dict.get('cache_duration', self.cache_duration),
|
|
878 |
without_user=True,
|
|
879 |
raise_if_not_cached=not(context.get('synchronous')),
|
|
880 |
invalidate_cache=invalidate_cache,
|
|
881 |
)
|
|
882 |
if json_response.status_code == 200:
|
|
883 |
try:
|
|
884 |
extra_context[data_key] = json.loads(json_response.content)
|
|
885 |
except ValueError:
|
|
886 |
logger = logging.getLogger(__name__)
|
|
887 |
logger.error('invalid json content (%s)', url)
|
|
888 |
continue
|
|
889 |
# update context with data key so it can be used in future
|
|
890 |
# templated URLs
|
|
891 |
context[data_key] = extra_context[data_key]
|
|
892 |
|
|
893 |
# keep cache of first response as it may be used to find the
|
|
894 |
# appropriate template.
|
|
895 |
self._json_content = extra_context['json']
|
873 |
896 |
return extra_context
|
874 |
897 |
|
875 |
898 |
@property
|
... | ... | |
1008 |
1031 |
JsonCellBase.actions)
|
1009 |
1032 |
|
1010 |
1033 |
@property
|
|
1034 |
def additional_data(self):
|
|
1035 |
return settings.JSON_CELL_TYPES[self.key].get('additional-data')
|
|
1036 |
|
|
1037 |
@property
|
1011 |
1038 |
def template_name(self):
|
1012 |
1039 |
return 'combo/json/%s.html' % self.key
|
1013 |
1040 |
|