Simplify overlay api methods.

This commit is contained in:
Norman Jäckel 2014-04-10 14:23:08 +02:00
parent 1583907571
commit f8cdad711c
6 changed files with 19 additions and 40 deletions

View File

@ -104,29 +104,22 @@ def default_slide():
return render_to_string('projector/default_slide.html') return render_to_string('projector/default_slide.html')
def get_overlays(): def get_overlays(only_active=False):
""" """
Returns all overlay objects. Returns all overlay objects.
If only_active is True, returns only active overlays.
The returned value is a dictonary with the name of the overlay as key, and The returned value is a dictonary with the name of the overlay as key, and
the overlay object as value. the overlay object as value.
""" """
overlays = {} overlays = {}
for receiver, overlay in projector_overlays.send(sender='get_overlays'): for receiver, overlay in projector_overlays.send(sender='get_overlays'):
if not only_active or overlay.is_active():
overlays[overlay.name] = overlay overlays[overlay.name] = overlay
return overlays return overlays
def get_projector_overlays():
"""
Returns the HTML code for all active overlays.
"""
overlays = [{'name': key, 'html': overlay.get_projector_html()}
for key, overlay in get_overlays().items()
if overlay.is_active()]
return render_to_string('projector/all_overlays.html', {'overlays': overlays})
def get_projector_overlays_js(as_json=False): def get_projector_overlays_js(as_json=False):
""" """
Returns JS-Code for the active overlays. Returns JS-Code for the active overlays.

View File

@ -59,7 +59,9 @@
</div> </div>
<div id="overlays"> <div id="overlays">
{{ overlays }} {% for overlay in overlays.values %}
{{ overlay.get_projector_html|safe }}
{% endfor %}
</div> </div>
<div id="content" {% if reload %}class="reload"{% endif %}> <div id="content" {% if reload %}class="reload"{% endif %}>

View File

@ -1,3 +0,0 @@
{% for overlay in overlays %}
{{ overlay.html|safe }}
{% endfor %}

View File

@ -6,7 +6,7 @@ from openslides.utils.tornado_webserver import ProjectorSocketHandler
from openslides.utils.views import RedirectView, TemplateView from openslides.utils.views import RedirectView, TemplateView
from .api import (call_on_projector, get_active_slide, from .api import (call_on_projector, get_active_slide,
get_overlays, get_projector_content, get_projector_overlays, get_overlays, get_projector_content,
get_projector_overlays_js, reset_countdown, set_active_slide, get_projector_overlays_js, reset_countdown, set_active_slide,
start_countdown, stop_countdown, update_projector_overlay) start_countdown, stop_countdown, update_projector_overlay)
@ -24,7 +24,7 @@ class ProjectorView(TemplateView):
if callback is None: if callback is None:
kwargs.update({ kwargs.update({
'content': get_projector_content(), 'content': get_projector_content(),
'overlays': get_projector_overlays(), 'overlays': get_overlays(only_active=True),
'overlay_js': get_projector_overlays_js(as_json=True), 'overlay_js': get_projector_overlays_js(as_json=True),
'reload': True, 'reload': True,
'calls': config['projector_js_cache']}) 'calls': config['projector_js_cache']})

View File

@ -91,30 +91,17 @@ class ApiFunctions(TestCase):
mock_overlay = MagicMock() mock_overlay = MagicMock()
mock_overlay.name = 'mock_overlay' mock_overlay.name = 'mock_overlay'
mock_projector_overlays.send.return_value = ((None, mock_overlay), ) mock_projector_overlays.send.return_value = ((None, mock_overlay), )
value = projector_api.get_overlays() value = projector_api.get_overlays()
self.assertEqual(value, {'mock_overlay': mock_overlay}) self.assertEqual(value, {'mock_overlay': mock_overlay})
@patch('openslides.projector.api.render_to_string') @patch('openslides.projector.api.projector_overlays')
@patch('openslides.projector.api.get_overlays') def test_get_overlays_inactive(self, mock_projector_overlays):
def test_get_projector_overlays(self, mock_get_overlays, mock_render_to_string):
mock_overlay = MagicMock() mock_overlay = MagicMock()
mock_overlay.get_projector_html.return_value = 'some html' mock_overlay.name = 'mock_overlay_2'
mock_get_overlays.return_value = {'overlay_name': mock_overlay}
# Test with inactive overlay
mock_overlay.is_active.return_value = False mock_overlay.is_active.return_value = False
projector_api.get_projector_overlays() mock_projector_overlays.send.return_value = ((None, mock_overlay), )
mock_render_to_string.assert_called_with( value = projector_api.get_overlays(only_active=True)
'projector/all_overlays.html', self.assertNotEqual(value, {'mock_overlay_2': mock_overlay})
{'overlays': []})
# Test with active overlay
mock_overlay.is_active.return_value = True
projector_api.get_projector_overlays()
mock_render_to_string.assert_Called_with(
'projector/all_overlays.html',
{'overlays': [{'name': 'overlay_name', 'html': 'some html'}]})
@patch('openslides.projector.api.get_overlays') @patch('openslides.projector.api.get_overlays')
def test_get_projector_overlays_js(self, mock_get_overlays): def test_get_projector_overlays_js(self, mock_get_overlays):

View File

@ -13,9 +13,9 @@ class ProjectorViewTest(TestCase):
rf = RequestFactory() rf = RequestFactory()
@patch('openslides.projector.views.get_projector_overlays_js') @patch('openslides.projector.views.get_projector_overlays_js')
@patch('openslides.projector.views.get_projector_overlays') @patch('openslides.projector.views.get_overlays')
@patch('openslides.projector.views.get_projector_content') @patch('openslides.projector.views.get_projector_content')
def test_get(self, mock_get_projector_content, mock_get_projector_overlays, def test_get(self, mock_get_projector_content, mock_get_overlays,
mock_get_projector_overlays_js): mock_get_projector_overlays_js):
view = views.ProjectorView() view = views.ProjectorView()
view.request = self.rf.get('/') view.request = self.rf.get('/')
@ -34,7 +34,7 @@ class ProjectorViewTest(TestCase):
with patch('openslides.projector.views.config', mock_config): with patch('openslides.projector.views.config', mock_config):
context = view.get_context_data() context = view.get_context_data()
mock_get_projector_content.assert_called_with() mock_get_projector_content.assert_called_with()
mock_get_projector_overlays.assert_called_with() mock_get_overlays.assert_called_with(only_active=True)
mock_get_projector_overlays_js.assert_called_with(as_json=True) mock_get_projector_overlays_js.assert_called_with(as_json=True)
self.assertTrue(context['reload']) self.assertTrue(context['reload'])
self.assertEqual(context['calls'], 'js_cache') self.assertEqual(context['calls'], 'js_cache')