From 3afc58be9ea7327b5f6f73907cc0695ce1a708b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norman=20J=C3=A4ckel?= Date: Thu, 13 Jun 2013 23:38:58 +0200 Subject: [PATCH] Add request to all widgets, fixed #653 --- openslides/account/views.py | 1 + openslides/agenda/views.py | 4 ++-- openslides/assignment/views.py | 1 + openslides/motion/views.py | 1 + openslides/participant/views.py | 2 ++ openslides/projector/api.py | 7 +++++++ openslides/projector/projector.py | 11 +++++++++-- openslides/projector/views.py | 21 ++++++++++----------- tests/projector/__init__.py | 0 tests/projector/test_widgets.py | 23 +++++++++++++++++++++++ 10 files changed, 56 insertions(+), 15 deletions(-) create mode 100644 tests/projector/__init__.py create mode 100644 tests/projector/test_widgets.py diff --git a/openslides/account/views.py b/openslides/account/views.py index 9d83be68a..0406d18bc 100644 --- a/openslides/account/views.py +++ b/openslides/account/views.py @@ -66,6 +66,7 @@ def get_personal_info_widget(request): if personal_info_context: return Widget( + request, name='personal_info', display_name=_('My items, motions and elections'), template='account/personal_info_widget.html', diff --git a/openslides/agenda/views.py b/openslides/agenda/views.py index 9f6e63d95..e839aaa47 100644 --- a/openslides/agenda/views.py +++ b/openslides/agenda/views.py @@ -18,7 +18,6 @@ from django.core.urlresolvers import reverse from django.contrib import messages from django.db import transaction from django.db.models import Model -from django.template import RequestContext from django.utils.translation import ugettext as _, ugettext_lazy from django.views.generic.detail import SingleObjectMixin @@ -547,6 +546,7 @@ def get_widgets(request): """ return [ Widget( + request, name='agenda', display_name=_('Agenda'), template='agenda/widget.html', @@ -558,8 +558,8 @@ def get_widgets(request): permission_required='projector.can_manage_projector'), Widget( + request, name='append_to_list_of_speakers', display_name=_('List of speakers'), template='agenda/speaker_widget.html', - context=RequestContext(request, {}), permission_required='agenda.can_be_speaker')] diff --git a/openslides/assignment/views.py b/openslides/assignment/views.py index 9cd64144d..d2660ebc2 100644 --- a/openslides/assignment/views.py +++ b/openslides/assignment/views.py @@ -641,6 +641,7 @@ def register_tab(request): def get_widgets(request): return [Widget( + request, name='assignments', display_name=_('Elections'), template='assignment/widget.html', diff --git a/openslides/motion/views.py b/openslides/motion/views.py index 7ecee0c1f..c82e6024f 100644 --- a/openslides/motion/views.py +++ b/openslides/motion/views.py @@ -760,6 +760,7 @@ def get_widgets(request): There is only one widget. It shows all motions. """ return [Widget( + request, name='motions', display_name=_('Motions'), template='motion/widget.html', diff --git a/openslides/participant/views.py b/openslides/participant/views.py index c59c66ac7..ae156191e 100644 --- a/openslides/participant/views.py +++ b/openslides/participant/views.py @@ -556,6 +556,7 @@ def get_user_widget(request): user slides. """ return Widget( + request, name='user', display_name=_('Participants'), template='participant/user_widget.html', @@ -570,6 +571,7 @@ def get_group_widget(request): group slides. """ return Widget( + request, name='group', display_name=_('Groups'), template='participant/group_widget.html', diff --git a/openslides/projector/api.py b/openslides/projector/api.py index 9e634a076..0a990a6ba 100644 --- a/openslides/projector/api.py +++ b/openslides/projector/api.py @@ -121,6 +121,13 @@ def register_slidefunc(key, func, control_template=None, weight=0, name=''): def get_all_widgets(request, session=False): + """ + Collects the widgets from all apps and returns the Widget objects as sorted + dictionary. + + The session flag decides whether to return only the widgets which are + active, that means that they are mentioned in the session. + """ widgets = SortedDict() session_widgets = request.session.get('widgets', {}) for app in settings.INSTALLED_APPS: diff --git a/openslides/projector/projector.py b/openslides/projector/projector.py index ab495db51..d9823b44f 100644 --- a/openslides/projector/projector.py +++ b/openslides/projector/projector.py @@ -16,6 +16,8 @@ from django.dispatch import receiver from django.template.loader import render_to_string from openslides.config.api import config +from django.template import RequestContext +from openslides.utils.exceptions import OpenSlidesError SLIDE = {} @@ -113,7 +115,7 @@ class Widget(object): """ Class for a Widget for the Projector-Tab. """ - def __init__(self, name, html=None, template=None, context={}, + def __init__(self, request, name, html=None, template=None, context={}, permission_required=None, display_name=None, default_column=1): self.name = name if display_name is None: @@ -124,7 +126,12 @@ class Widget(object): if html is not None: self.html = html elif template is not None: - self.html = render_to_string(template, context) + self.html = render_to_string( + template_name=template, + dictionary=context, + context_instance=RequestContext(request)) + else: + raise OpenSlidesError('A Widget must have either a html or a template argument.') self.permission_required = permission_required self.default_column = default_column diff --git a/openslides/projector/views.py b/openslides/projector/views.py index 82a417dd2..9a7e9b5fe 100644 --- a/openslides/projector/views.py +++ b/openslides/projector/views.py @@ -375,34 +375,33 @@ def get_widgets(request): """ widgets = [] - # welcome widget - context = { - 'welcometext': config['welcome_text']} + # Welcome widget widgets.append(Widget( + request, name='welcome', display_name=config['welcome_title'], template='projector/welcome_widget.html', - context=context, + context={'welcometext': config['welcome_text']}, permission_required='projector.can_see_dashboard', default_column=1)) # Projector live view widget widgets.append(Widget( + request, name='live_view', display_name=_('Projector live view'), template='projector/live_view_widget.html', - context=RequestContext(request, {}), permission_required='projector.can_see_projector', default_column=2)) - # Overlay Widget + # Overlay widget overlays = [] for receiver, overlay in projector_overlays.send(sender='overlay_widget', request=request): overlays.append(overlay) - context = {'overlays': overlays} context.update(csrf(request)) widgets.append(Widget( + request, name='overlays', display_name=_('Overlays'), template='projector/overlay_widget.html', @@ -411,14 +410,14 @@ def get_widgets(request): context=context)) # Custom slide widget - context = { - 'slides': ProjectorSlide.objects.all().order_by('weight'), - 'welcomepage_is_active': not bool(config["presentation"])} widgets.append(Widget( + request, name='custom_slide', display_name=_('Custom Slides'), template='projector/custom_slide_widget.html', - context=context, + context={ + 'slides': ProjectorSlide.objects.all().order_by('weight'), + 'welcomepage_is_active': not bool(config["presentation"])}, permission_required='projector.can_manage_projector', default_column=2)) diff --git a/tests/projector/__init__.py b/tests/projector/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/projector/test_widgets.py b/tests/projector/test_widgets.py new file mode 100644 index 000000000..30f5100f0 --- /dev/null +++ b/tests/projector/test_widgets.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" + Tests for openslides widgets + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + TODO: Move this test to the correct place when the projector app is cleaned up. + + :copyright: 2011–2013 by OpenSlides team, see AUTHORS. + :license: GNU GPL, see LICENSE for more details. +""" + +from django.http import HttpRequest + +from openslides.projector.projector import Widget +from openslides.utils.exceptions import OpenSlidesError +from openslides.utils.test import TestCase + + +class WidgetObject(TestCase): + def test_error(self): + with self.assertRaises(OpenSlidesError): + w = Widget(HttpRequest(), name='chahghuyeim8ie0Noong')