OpenSlides/openslides/projector/views.py

365 lines
12 KiB
Python
Raw Normal View History

2012-02-03 20:49:16 +01:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
2012-02-06 22:08:08 +01:00
openslides.projector.views
2012-07-07 14:01:40 +02:00
~~~~~~~~~~~~~~~~~~~~~~~~~~
2012-02-03 20:49:16 +01:00
2012-02-06 22:08:08 +01:00
Views for the projector app.
2012-02-03 20:49:16 +01:00
:copyright: 20112013 by OpenSlides team, see AUTHORS.
2012-02-03 20:49:16 +01:00
:license: GNU GPL, see LICENSE for more details.
"""
2012-04-25 22:29:19 +02:00
2012-02-03 20:49:16 +01:00
from datetime import datetime
2012-04-14 09:47:34 +02:00
from time import time
2012-02-03 20:49:16 +01:00
2012-05-18 23:07:09 +02:00
from django.contrib import messages
from django.core.cache import cache
2012-08-15 18:00:22 +02:00
from django.core.context_processors import csrf
2012-02-03 20:49:16 +01:00
from django.core.urlresolvers import reverse
2012-08-15 13:42:25 +02:00
from django.db import transaction
2012-07-07 14:01:40 +02:00
from django.db.models import Q
from django.shortcuts import redirect
from django.template import RequestContext
from django.utils.translation import ugettext as _
2012-02-03 20:49:16 +01:00
2012-07-07 14:01:40 +02:00
from openslides.utils.template import render_block_to_string, Tab
from openslides.utils.views import (
TemplateView, RedirectView, CreateView, UpdateView, DeleteView, AjaxMixin)
from openslides.config.api import config
from .api import (
get_projector_content, get_projector_overlays, get_all_widgets,
set_active_slide, update_projector, get_active_slide, update_projector_overlay,
get_overlays, get_projector_overlays_js)
from .forms import SelectWidgetsForm
2013-04-15 10:40:47 +02:00
from .models import ProjectorSlide
from .projector import Widget
from .signals import projector_overlays
2012-03-03 09:11:56 +01:00
class DashboardView(AjaxMixin, TemplateView):
2012-07-07 14:01:40 +02:00
"""
Overview over all possible slides, the overlays and a liveview.
"""
2012-08-15 10:58:29 +02:00
template_name = 'projector/dashboard.html'
permission_required = 'projector.can_see_dashboard'
2012-03-03 09:11:56 +01:00
def get_context_data(self, **kwargs):
2012-08-15 10:58:29 +02:00
context = super(DashboardView, self).get_context_data(**kwargs)
2012-06-11 13:43:48 +02:00
2012-08-15 13:42:25 +02:00
context['widgets'] = get_all_widgets(self.request, session=True)
2012-03-03 09:11:56 +01:00
return context
class Projector(TemplateView):
2012-07-07 14:01:40 +02:00
"""
The Projector-Page.
"""
permission_required = 'projector.can_see_projector'
template_name = 'projector.html'
def get_context_data(self, **kwargs):
slide_dict = dict(self.request.GET.items())
callback = self.kwargs.get('callback', None)
if callback:
slide_dict['callback'] = callback
if not slide_dict:
kwargs.update({
'content': get_projector_content(),
'overlays': get_projector_overlays(),
'overlay_js': get_projector_overlays_js(),
'reload': True})
# For the Preview
else:
kwargs.update({
'content': get_projector_content(slide_dict),
'reload': False})
return super(Projector, self).get_context_data(**kwargs)
2012-02-03 20:49:16 +01:00
2012-07-07 14:01:40 +02:00
class ActivateView(RedirectView):
"""
Activate a Slide.
"""
permission_required = 'projector.can_manage_projector'
2013-02-02 00:26:08 +01:00
url_name = 'dashboard'
2012-07-07 14:01:40 +02:00
allow_ajax = True
def pre_redirect(self, request, *args, **kwargs):
set_active_slide(kwargs['callback'], kwargs=dict(request.GET.items()))
2012-07-07 14:01:40 +02:00
config['up'] = 0
config['bigger'] = 100
2012-02-03 20:49:16 +01:00
2012-08-15 13:42:25 +02:00
class SelectWidgetsView(TemplateView):
"""
Show a Form to Select the widgets.
"""
permission_required = 'projector.can_see_dashboard'
template_name = 'projector/select_widgets.html'
def get_context_data(self, **kwargs):
context = super(SelectWidgetsView, self). get_context_data(**kwargs)
widgets = get_all_widgets(self.request)
activated_widgets = self.request.session.get('widgets', {})
for name, widget in widgets.items():
initial = {'widget': activated_widgets.get(name, True)}
if self.request.method == 'POST':
widget.form = SelectWidgetsForm(self.request.POST, prefix=name,
initial=initial)
else:
widget.form = SelectWidgetsForm(prefix=name, initial=initial)
context['widgets'] = widgets
return context
@transaction.commit_manually
def post(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)
activated_widgets = self.request.session.get('widgets', {})
transaction.commit()
for name, widget in context['widgets'].items():
if widget.form.is_valid():
activated_widgets[name] = widget.form.cleaned_data['widget']
else:
transaction.rollback()
messages.error(request, _('Errors in the form'))
break
else:
transaction.commit()
self.request.session['widgets'] = activated_widgets
return redirect(reverse('dashboard'))
2012-08-15 13:42:25 +02:00
class ProjectorEdit(RedirectView):
2012-07-07 14:01:40 +02:00
"""
Scale or scroll the projector.
"""
permission_required = 'projector.can_manage_projector'
2013-02-02 00:26:08 +01:00
url_name = 'dashboard'
allow_ajax = True
2012-02-03 20:49:16 +01:00
def pre_redirect(self, request, *args, **kwargs):
direction = kwargs['direction']
if direction == 'bigger':
config['bigger'] = int(config['bigger']) + 20
elif direction == 'smaller':
config['bigger'] = int(config['bigger']) - 20
elif direction == 'down':
config['up'] = int(config['up']) - 5
elif direction == 'up':
if config['up'] < 0:
config['up'] = int(config['up']) + 5
elif direction == 'clean':
config['up'] = config.get_default('up')
config['bigger'] = config.get_default('bigger')
2012-02-03 20:49:16 +01:00
class CountdownEdit(RedirectView):
2012-07-07 14:01:40 +02:00
"""
Start, stop or reset the countdown.
"""
permission_required = 'projector.can_manage_projector'
2013-02-02 00:26:08 +01:00
url_name = 'dashboard'
allow_ajax = True
def pre_redirect(self, request, *args, **kwargs):
command = kwargs['command']
2012-07-04 02:43:26 +02:00
# countdown_state is one of 'inactive', 'paused' and 'active', 'expired'
if command in ['reset', 'start', 'stop']:
config['countdown_time'] = config['countdown_time']
if command == 'reset':
config['countdown_start_stamp'] = time()
config['countdown_pause_stamp'] = 0
config['countdown_state'] = 'inactive'
elif command == 'start':
# if we had stopped the countdown resume were we left of
if config['countdown_state'] == 'paused':
start_stamp = config['countdown_start_stamp']
pause_stamp = config['countdown_pause_stamp']
now = time()
2012-07-07 14:01:40 +02:00
config['countdown_start_stamp'] = now - \
(pause_stamp - start_stamp)
else:
config['countdown_start_stamp'] = time()
config['countdown_state'] = 'active'
config['countdown_pause_stamp'] = 0
elif command == 'stop':
if config['countdown_state'] == 'active':
config['countdown_pause_stamp'] = time()
config['countdown_state'] = 'paused'
elif command == 'set-default':
try:
2012-07-07 14:01:40 +02:00
config['countdown_time'] = \
int(self.request.GET['countdown_time'])
except ValueError:
pass
except AttributeError:
pass
update_projector_overlay('projector_countdown')
2012-03-18 17:11:58 +01:00
2012-07-04 02:43:26 +02:00
def get_ajax_context(self, **kwargs):
return {
'state': config['countdown_state'],
'countdown_time': config['countdown_time'],
}
2012-03-18 17:11:58 +01:00
2012-08-15 18:00:22 +02:00
class OverlayMessageView(RedirectView):
"""
Sets or clears the overlay message
"""
2013-02-02 00:26:08 +01:00
url_name = 'dashboard'
2012-08-15 18:00:22 +02:00
allow_ajax = True
permission_required = 'projector.can_manage_projector'
def pre_post_redirect(self, request, *args, **kwargs):
if 'message' in request.POST:
2013-04-15 10:40:47 +02:00
config['projector_message'] = request.POST['message_text']
2012-08-15 18:00:22 +02:00
elif 'message-clean' in request.POST:
2013-04-15 10:40:47 +02:00
config['projector_message'] = ''
update_projector_overlay('projector_message')
2012-08-15 18:00:22 +02:00
def get_ajax_context(self, **kwargs):
return {
'overlay_message': config['projector_message'],
}
2012-07-07 14:01:40 +02:00
class ActivateOverlay(RedirectView):
"""
Activate or deactivate an overlay.
"""
2013-02-02 00:26:08 +01:00
url_name = 'dashboard'
2012-07-07 14:01:40 +02:00
allow_ajax = True
permission_required = 'projector.can_manage_projector'
def pre_redirect(self, request, *args, **kwargs):
overlay = get_overlays()[kwargs['name']]
self.name = overlay.name
2012-07-07 14:01:40 +02:00
if kwargs['activate']:
if not overlay.is_active():
overlay.set_active(True)
update_projector_overlay(overlay)
2013-04-15 10:40:47 +02:00
self.active = True
else:
if overlay.is_active():
overlay.set_active(False)
update_projector_overlay(overlay)
2013-04-15 10:40:47 +02:00
self.active = False
2012-07-07 14:01:40 +02:00
def get_ajax_context(self, **kwargs):
2013-04-15 10:40:47 +02:00
return {'active': self.active, 'name': self.name}
2012-07-07 14:01:40 +02:00
class CustomSlideCreateView(CreateView):
"""
Create a custom slide.
"""
permission_required = 'agenda.can_manage_agenda'
template_name = 'projector/new.html'
model = ProjectorSlide
context_object_name = 'customslide'
success_url_name = 'dashboard'
2012-07-07 14:01:40 +02:00
class CustomSlideUpdateView(UpdateView):
"""
Update a custom slide.
"""
permission_required = 'projector.can_manage_projector'
template_name = 'projector/new.html'
model = ProjectorSlide
context_object_name = 'customslide'
success_url_name = 'dashboard'
2012-07-07 14:01:40 +02:00
class CustomSlideDeleteView(DeleteView):
"""
Delete a custom slide.
"""
permission_required = 'projector.can_manage_projector'
model = ProjectorSlide
success_url_name = 'dashboard'
2012-07-07 14:01:40 +02:00
2012-03-18 17:11:58 +01:00
def register_tab(request):
2012-07-07 14:01:40 +02:00
"""
Register the projector tab.
"""
selected = request.path.startswith('/projector/')
2012-03-18 17:11:58 +01:00
return Tab(
2012-08-15 10:58:29 +02:00
title=_('Dashboard'),
app='dashboard',
2012-08-15 10:58:29 +02:00
url=reverse('dashboard'),
permission=request.user.has_perm('projector.can_see_dashboard'),
2012-03-18 17:11:58 +01:00
selected=selected,
)
2012-04-14 20:10:49 +02:00
2012-06-11 13:43:48 +02:00
def get_widgets(request):
2012-07-07 14:01:40 +02:00
"""
2012-08-15 11:56:43 +02:00
Return the widgets of the projector app
2012-07-07 14:01:40 +02:00
"""
2012-08-15 11:56:43 +02:00
widgets = []
2013-06-13 23:38:58 +02:00
# Welcome widget
2012-09-20 20:45:37 +02:00
widgets.append(Widget(
2013-06-13 23:38:58 +02:00
request,
2012-09-20 20:45:37 +02:00
name='welcome',
display_name=config['welcome_title'],
2012-09-20 20:45:37 +02:00
template='projector/welcome_widget.html',
2013-06-13 23:38:58 +02:00
context={'welcometext': config['welcome_text']},
2012-09-20 20:45:37 +02:00
permission_required='projector.can_see_dashboard',
default_column=1,
default_weight=10))
2012-09-20 20:45:37 +02:00
2012-08-15 11:56:43 +02:00
# Projector live view widget
widgets.append(Widget(
2013-06-13 23:38:58 +02:00
request,
2012-08-15 11:56:43 +02:00
name='live_view',
display_name=_('Projector live view'),
template='projector/live_view_widget.html',
permission_required='projector.can_see_projector',
default_column=2,
default_weight=10))
2012-08-15 11:56:43 +02:00
2013-06-13 23:38:58 +02:00
# Overlay widget
2012-08-15 11:56:43 +02:00
overlays = []
2013-04-15 10:40:47 +02:00
for receiver, overlay in projector_overlays.send(sender='overlay_widget', request=request):
if overlay.widget_html_callback is not None:
overlays.append(overlay)
2013-04-15 10:40:47 +02:00
context = {'overlays': overlays}
2012-08-15 18:00:22 +02:00
context.update(csrf(request))
2012-08-15 11:56:43 +02:00
widgets.append(Widget(
2013-06-13 23:38:58 +02:00
request,
2012-08-15 11:56:43 +02:00
name='overlays',
2012-09-11 20:50:53 +02:00
display_name=_('Overlays'),
2012-08-15 11:56:43 +02:00
template='projector/overlay_widget.html',
permission_required='projector.can_manage_projector',
default_column=2,
default_weight=20,
2012-08-15 18:00:22 +02:00
context=context))
2012-08-15 11:56:43 +02:00
# Custom slide widget
welcomepage_is_active = get_active_slide().get('callback', 'default') == 'default'
2012-08-15 11:56:43 +02:00
widgets.append(Widget(
2013-06-13 23:38:58 +02:00
request,
2012-08-15 11:56:43 +02:00
name='custom_slide',
display_name=_('Custom Slides'),
2012-08-15 11:56:43 +02:00
template='projector/custom_slide_widget.html',
2013-06-13 23:38:58 +02:00
context={
'slides': ProjectorSlide.objects.all().order_by('weight'),
'welcomepage_is_active': welcomepage_is_active},
2012-08-15 11:56:43 +02:00
permission_required='projector.can_manage_projector',
default_column=2,
default_weight=30))
2012-08-15 11:56:43 +02:00
return widgets