From 8c096f7cca0b18c497a3996557f15c70dd103035 Mon Sep 17 00:00:00 2001 From: sdc50 Date: Mon, 22 Jul 2019 16:27:51 -0500 Subject: [PATCH] Overview map hotfix (#451) * overview map control * add "set" template tag * set template tag tests --- .../test_templatetags/test_tethys_gizmos.py | 27 +++++++++++++++ .../tethys_gizmos/js/tethys_map_view.js | 9 ++++- tethys_gizmos/templatetags/tethys_gizmos.py | 33 +++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/tests/unit_tests/test_tethys_gizmos/test_templatetags/test_tethys_gizmos.py b/tests/unit_tests/test_tethys_gizmos/test_templatetags/test_tethys_gizmos.py index c6ddfd81e..b52e87b6b 100644 --- a/tests/unit_tests/test_tethys_gizmos/test_templatetags/test_tethys_gizmos.py +++ b/tests/unit_tests/test_tethys_gizmos/test_templatetags/test_tethys_gizmos.py @@ -70,6 +70,33 @@ def test_HighchartsDateEncoder_no_dt(self): # Check Result self.assertEqual('2018-01-01', result) + def test_SetVarNode_template_error(self): + node = gizmos_templatetags.SetVarNode('test', 'test') + result = node.render({}) + self.assertEqual('', result) + + @mock.patch('tethys_gizmos.templatetags.tethys_gizmos.template.Variable') + def test_SetVarNode_render(self, mock_template_var): + mock_Variable = mock.MagicMock() + mock_Variable.resolve.return_value = 'foo' + mock_template_var.return_value = mock_Variable + context = {'test1': {}} + node = gizmos_templatetags.SetVarNode('test1.test', 'test') + result = node.render(context) + self.assertDictEqual(context, {'test1': {'test': 'foo'}}) + + self.assertEqual('', result) + + @mock.patch('tethys_gizmos.templatetags.tethys_gizmos.SetVarNode') + def test_set_var(self, _): + token = mock.MagicMock() + token.split_contents.return_value = ['set', 'test', '=', 'tests'] + gizmos_templatetags.set_var(None, token) + + def test_set_var_error(self): + token = mock.MagicMock() + self.assertRaises(TemplateSyntaxError, gizmos_templatetags.set_var, None, token) + def test_isstring(self): result = gizmos_templatetags.isstring(type('string')) diff --git a/tethys_gizmos/static/tethys_gizmos/js/tethys_map_view.js b/tethys_gizmos/static/tethys_gizmos/js/tethys_map_view.js index 18b5b5fb7..1aceffd9b 100644 --- a/tethys_gizmos/static/tethys_gizmos/js/tethys_map_view.js +++ b/tethys_gizmos/static/tethys_gizmos/js/tethys_map_view.js @@ -323,7 +323,8 @@ var TETHYS_MAP_VIEW = (function() { ZOOM_EXTENT = 'ZoomToExtent', FULL_SCREEN = 'FullScreen', MOUSE_POSITION = 'MousePosition', - SCALE_LINE = 'ScaleLine'; + SCALE_LINE = 'ScaleLine', + OVERVIEW_MAP = 'OverviewMap'; var controls; @@ -356,6 +357,9 @@ var TETHYS_MAP_VIEW = (function() { else if (current_control === SCALE_LINE) { m_map.addControl(new ol.control.ScaleLine()); } + else if (current_control === OVERVIEW_MAP) { + m_map.addControl(new ol.control.OverviewMap()); + } // Handle object case } else if (typeof current_control === 'object') { @@ -374,6 +378,9 @@ var TETHYS_MAP_VIEW = (function() { else if (SCALE_LINE in current_control){ m_map.addControl(new ol.control.ScaleLine(current_control[SCALE_LINE])); } + else if (OVERVIEW_MAP in current_control){ + m_map.addControl(new ol.control.OverviewMap(current_control[OVERVIEW_MAP])); + } else if (ZOOM_EXTENT in current_control){ var control_obj = current_control[ZOOM_EXTENT]; diff --git a/tethys_gizmos/templatetags/tethys_gizmos.py b/tethys_gizmos/templatetags/tethys_gizmos.py index 179ee0d94..6e57ab8b1 100644 --- a/tethys_gizmos/templatetags/tethys_gizmos.py +++ b/tethys_gizmos/templatetags/tethys_gizmos.py @@ -77,6 +77,39 @@ def default(self, obj): return super(HighchartsDateEncoder, self).default(obj) +class SetVarNode(template.Node): + + def __init__(self, var_name, var_value): + self.var_names = var_name.split('.') + self.var_name = self.var_names.pop() + self.var_value = var_value + + def render(self, context): + try: + value = template.Variable(self.var_value).resolve(context) + except template.VariableDoesNotExist: + value = '' + + for name in self.var_names: + context = context[name] + + context[self.var_name] = value + + return '' + + +@register.tag(name='set') +def set_var(parser, token): + """ + {% set some_var = '123' %} + """ + parts = token.split_contents() + if len(parts) < 4: + raise template.TemplateSyntaxError("'set' tag must be of the form: {% set = %}") + + return SetVarNode(parts[1], parts[3]) + + @register.filter(is_safe=True) def isstring(value): """