Add request to all widgets, fixed #653

This commit is contained in:
Norman Jäckel 2013-06-13 23:38:58 +02:00
parent 9485e771b9
commit 3afc58be9e
10 changed files with 56 additions and 15 deletions

View File

@ -66,6 +66,7 @@ def get_personal_info_widget(request):
if personal_info_context: if personal_info_context:
return Widget( return Widget(
request,
name='personal_info', name='personal_info',
display_name=_('My items, motions and elections'), display_name=_('My items, motions and elections'),
template='account/personal_info_widget.html', template='account/personal_info_widget.html',

View File

@ -18,7 +18,6 @@ from django.core.urlresolvers import reverse
from django.contrib import messages from django.contrib import messages
from django.db import transaction from django.db import transaction
from django.db.models import Model from django.db.models import Model
from django.template import RequestContext
from django.utils.translation import ugettext as _, ugettext_lazy from django.utils.translation import ugettext as _, ugettext_lazy
from django.views.generic.detail import SingleObjectMixin from django.views.generic.detail import SingleObjectMixin
@ -547,6 +546,7 @@ def get_widgets(request):
""" """
return [ return [
Widget( Widget(
request,
name='agenda', name='agenda',
display_name=_('Agenda'), display_name=_('Agenda'),
template='agenda/widget.html', template='agenda/widget.html',
@ -558,8 +558,8 @@ def get_widgets(request):
permission_required='projector.can_manage_projector'), permission_required='projector.can_manage_projector'),
Widget( Widget(
request,
name='append_to_list_of_speakers', name='append_to_list_of_speakers',
display_name=_('List of speakers'), display_name=_('List of speakers'),
template='agenda/speaker_widget.html', template='agenda/speaker_widget.html',
context=RequestContext(request, {}),
permission_required='agenda.can_be_speaker')] permission_required='agenda.can_be_speaker')]

View File

@ -641,6 +641,7 @@ def register_tab(request):
def get_widgets(request): def get_widgets(request):
return [Widget( return [Widget(
request,
name='assignments', name='assignments',
display_name=_('Elections'), display_name=_('Elections'),
template='assignment/widget.html', template='assignment/widget.html',

View File

@ -760,6 +760,7 @@ def get_widgets(request):
There is only one widget. It shows all motions. There is only one widget. It shows all motions.
""" """
return [Widget( return [Widget(
request,
name='motions', name='motions',
display_name=_('Motions'), display_name=_('Motions'),
template='motion/widget.html', template='motion/widget.html',

View File

@ -556,6 +556,7 @@ def get_user_widget(request):
user slides. user slides.
""" """
return Widget( return Widget(
request,
name='user', name='user',
display_name=_('Participants'), display_name=_('Participants'),
template='participant/user_widget.html', template='participant/user_widget.html',
@ -570,6 +571,7 @@ def get_group_widget(request):
group slides. group slides.
""" """
return Widget( return Widget(
request,
name='group', name='group',
display_name=_('Groups'), display_name=_('Groups'),
template='participant/group_widget.html', template='participant/group_widget.html',

View File

@ -121,6 +121,13 @@ def register_slidefunc(key, func, control_template=None, weight=0, name=''):
def get_all_widgets(request, session=False): 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() widgets = SortedDict()
session_widgets = request.session.get('widgets', {}) session_widgets = request.session.get('widgets', {})
for app in settings.INSTALLED_APPS: for app in settings.INSTALLED_APPS:

View File

@ -16,6 +16,8 @@ from django.dispatch import receiver
from django.template.loader import render_to_string from django.template.loader import render_to_string
from openslides.config.api import config from openslides.config.api import config
from django.template import RequestContext
from openslides.utils.exceptions import OpenSlidesError
SLIDE = {} SLIDE = {}
@ -113,7 +115,7 @@ class Widget(object):
""" """
Class for a Widget for the Projector-Tab. 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): permission_required=None, display_name=None, default_column=1):
self.name = name self.name = name
if display_name is None: if display_name is None:
@ -124,7 +126,12 @@ class Widget(object):
if html is not None: if html is not None:
self.html = html self.html = html
elif template is not None: 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.permission_required = permission_required
self.default_column = default_column self.default_column = default_column

View File

@ -375,34 +375,33 @@ def get_widgets(request):
""" """
widgets = [] widgets = []
# welcome widget # Welcome widget
context = {
'welcometext': config['welcome_text']}
widgets.append(Widget( widgets.append(Widget(
request,
name='welcome', name='welcome',
display_name=config['welcome_title'], display_name=config['welcome_title'],
template='projector/welcome_widget.html', template='projector/welcome_widget.html',
context=context, context={'welcometext': config['welcome_text']},
permission_required='projector.can_see_dashboard', permission_required='projector.can_see_dashboard',
default_column=1)) default_column=1))
# Projector live view widget # Projector live view widget
widgets.append(Widget( widgets.append(Widget(
request,
name='live_view', name='live_view',
display_name=_('Projector live view'), display_name=_('Projector live view'),
template='projector/live_view_widget.html', template='projector/live_view_widget.html',
context=RequestContext(request, {}),
permission_required='projector.can_see_projector', permission_required='projector.can_see_projector',
default_column=2)) default_column=2))
# Overlay Widget # Overlay widget
overlays = [] overlays = []
for receiver, overlay in projector_overlays.send(sender='overlay_widget', request=request): for receiver, overlay in projector_overlays.send(sender='overlay_widget', request=request):
overlays.append(overlay) overlays.append(overlay)
context = {'overlays': overlays} context = {'overlays': overlays}
context.update(csrf(request)) context.update(csrf(request))
widgets.append(Widget( widgets.append(Widget(
request,
name='overlays', name='overlays',
display_name=_('Overlays'), display_name=_('Overlays'),
template='projector/overlay_widget.html', template='projector/overlay_widget.html',
@ -411,14 +410,14 @@ def get_widgets(request):
context=context)) context=context))
# Custom slide widget # Custom slide widget
context = {
'slides': ProjectorSlide.objects.all().order_by('weight'),
'welcomepage_is_active': not bool(config["presentation"])}
widgets.append(Widget( widgets.append(Widget(
request,
name='custom_slide', name='custom_slide',
display_name=_('Custom Slides'), display_name=_('Custom Slides'),
template='projector/custom_slide_widget.html', 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', permission_required='projector.can_manage_projector',
default_column=2)) default_column=2))

View File

View File

@ -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: 20112013 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')