OpenSlides/openslides/projector/views.py

365 lines
11 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
2012-04-25 22:29:19 +02:00
:copyright: 2011, 2012 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-07-07 14:01:40 +02:00
from django.conf import settings
2012-05-18 23:07:09 +02:00
from django.contrib import messages
2012-02-03 20:49:16 +01:00
from django.core.urlresolvers import reverse
2012-07-07 14:01:40 +02:00
from django.db.models import Q
from django.dispatch import receiver
2012-04-14 11:18:47 +02:00
from django.utils.datastructures import SortedDict
2012-06-11 13:43:48 +02:00
from django.utils.importlib import import_module
2012-07-10 01:33:03 +02:00
from django.utils.translation import ugettext_lazy 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.utils import html_strong
from openslides.utils.views import (TemplateView, RedirectView, CreateView,
UpdateView, DeleteView, AjaxMixin)
2012-02-03 20:49:16 +01:00
from openslides.config.models import config
2012-02-03 20:49:16 +01:00
from openslides.projector.api import (get_active_slide, set_active_slide,
projector_message_set, projector_message_delete, get_slide_from_sid)
from openslides.projector.models import ProjectorOverlay, ProjectorSlide
2012-07-07 14:01:40 +02:00
from openslides.projector.projector import SLIDE, Widget
from openslides.projector.signals import projector_overlays
2012-03-03 09:11:56 +01:00
2012-08-15 10:58:29 +02:00
class DashboardView(TemplateView, AjaxMixin):
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_projector_overlays(self):
overlays = []
2012-07-07 14:01:40 +02:00
for receiver, name in projector_overlays.send(sender='registerer',
register=True):
if name is not None:
try:
2012-07-07 14:01:40 +02:00
projector_overlay = ProjectorOverlay.objects.get(
def_name=name)
except ProjectorOverlay.DoesNotExist:
2012-07-07 14:01:40 +02:00
projector_overlay = ProjectorOverlay(def_name=name,
active=False)
projector_overlay.save()
overlays.append(projector_overlay)
return overlays
def post(self, request, *args, **kwargs):
if 'message' in request.POST:
projector_message_set(request.POST['message_text'])
2012-04-16 16:38:47 +02:00
elif 'message-clean' in request.POST:
projector_message_delete()
2012-07-04 02:43:26 +02:00
if request.is_ajax():
2012-07-04 03:21:03 +02:00
return self.ajax_get(request, *args, **kwargs)
return self.get(request, *args, **kwargs)
2012-07-04 03:21:03 +02:00
def get_ajax_context(self, **kwargs):
return {
'overlay_message': config['projector_message'],
}
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
widgets = SortedDict()
2012-04-14 11:18:47 +02:00
for app in settings.INSTALLED_APPS:
2012-06-11 13:43:48 +02:00
try:
mod = import_module(app + '.views')
except ImportError:
continue
appname = mod.__name__.split('.')[0]
try:
modul_widgets = mod.get_widgets(self.request)
except AttributeError:
continue
2012-04-14 20:10:49 +02:00
2012-06-11 13:43:48 +02:00
for widget in modul_widgets:
2012-08-15 10:58:29 +02:00
if self.request.user.has_perm(widget.permission_required):
widgets[widget.get_name()] = widget
2012-04-14 20:10:49 +02:00
2012-03-03 09:11:56 +01:00
context.update({
'countdown_time': config['countdown_time'],
'countdown_state' : config['countdown_state'],
'overlays': self.get_projector_overlays(),
2012-06-11 13:43:48 +02:00
'widgets': widgets,
2012-03-03 09:11:56 +01:00
})
return context
class Projector(TemplateView, AjaxMixin):
2012-07-07 14:01:40 +02:00
"""
The Projector-Page.
"""
permission_required = 'projector.can_see_projector'
@property
def data(self):
2012-04-18 15:47:51 +02:00
try:
return self._data
except AttributeError:
pass
sid = self.kwargs['sid']
if sid is None:
try:
data = get_active_slide()
except AttributeError: #TODO: It has to be an Slide.DoesNotExist
data = None
ajax = 'on'
else:
data = get_slide_from_sid(sid)
ajax = 'off'
if data is None:
data = {
'title': config['event_name'],
'template': 'projector/default.html',
}
data['overlays'] = []
data['ajax'] = ajax
# Projector Overlays
if self.kwargs['sid'] is None:
2012-07-07 14:01:40 +02:00
active_defs = ProjectorOverlay.objects.filter(active=True) \
.filter(Q(sid=sid) | Q(sid=None)).values_list('def_name',
flat=True)
for receiver, response in projector_overlays.send(sender=sid,
register=False, call=active_defs):
if response is not None:
data['overlays'].append(response)
self._data = data
return data
def get_template_names(self):
return [self.data['template']]
def get_context_data(self, **kwargs):
context = super(Projector, self).get_context_data(**kwargs)
context.update(self.data)
return context
def get_ajax_context(self, **kwargs):
2012-07-07 14:01:40 +02:00
content = render_block_to_string(self.get_template_names()[0],
'content', self.data)
scrollcontent = render_block_to_string(self.get_template_names()[0],
'scrollcontent', self.data)
context = super(Projector, self).get_ajax_context(**kwargs)
content_hash = hash(content)
context.update({
'content': content,
'scrollcontent': scrollcontent,
'time': datetime.now().strftime('%H:%M'),
'overlays': self.data['overlays'],
'title': self.data['title'],
2012-02-15 12:04:11 +01:00
'bigger': config['bigger'],
'up': config['up'],
'content_hash': content_hash,
})
return context
def get(self, request, *args, **kwargs):
if request.is_ajax():
return self.ajax_get(request, *args, **kwargs)
return super(Projector, self).get(request, *args, **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'
2012-08-15 10:58:29 +02:00
url = 'dashboard'
2012-07-07 14:01:40 +02:00
allow_ajax = True
def pre_redirect(self, request, *args, **kwargs):
try:
set_active_slide(kwargs['sid'], kwargs['argument'])
except KeyError:
set_active_slide(kwargs['sid'])
config['up'] = 0
config['bigger'] = 100
2012-02-03 20:49:16 +01:00
class ProjectorEdit(RedirectView):
2012-07-07 14:01:40 +02:00
"""
Scale or scroll the projector.
"""
permission_required = 'projector.can_manage_projector'
2012-08-15 10:58:29 +02:00
url = '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 == 'up':
config['up'] = int(config['up']) - 10
elif direction == 'down':
if config['up'] < 0:
config['up'] = int(config['up']) + 10
elif direction == 'clean':
config['up'] = 0
config['bigger'] = 100
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'
2012-08-15 10:58:29 +02:00
url = '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
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-07-07 14:01:40 +02:00
class ActivateOverlay(RedirectView):
"""
Activate or deactivate an overlay.
"""
2012-08-15 10:58:29 +02:00
url = 'dashboard'
2012-07-07 14:01:40 +02:00
allow_ajax = True
permission_required = 'projector.can_manage_projector'
@property
def overlay(self):
try:
return self._overlay
except AttributeError:
self._overlay = ProjectorOverlay.objects.get(
def_name=self.kwargs['name'])
return self._overlay
def pre_redirect(self, request, *args, **kwargs):
if kwargs['activate']:
self.overlay.active = True
else:
self.overlay.active = False
self.overlay.save()
def get_ajax_context(self, **kwargs):
return {
'active': self.overlay.active,
'def_name': self.overlay.def_name,
}
class CustomSlideCreateView(CreateView):
"""
Create a custom slide.
"""
permission_required = 'agenda.can_manage_agenda'
template_name = 'projector/new.html'
model = ProjectorSlide
context_object_name = 'customslide'
2012-08-15 10:58:29 +02:00
success_url = 'dashboard'
2012-07-07 14:01:40 +02:00
apply_url = 'customslide_edit'
class CustomSlideUpdateView(UpdateView):
"""
Update a custom slide.
"""
permission_required = 'projector.can_manage_projector'
template_name = 'projector/new.html'
model = ProjectorSlide
context_object_name = 'customslide'
2012-08-15 10:58:29 +02:00
success_url = 'dashboard'
2012-07-07 14:01:40 +02:00
apply_url = 'customslide_edit'
class CustomSlideDeleteView(DeleteView):
"""
Delete a custom slide.
"""
permission_required = 'projector.can_manage_projector'
model = ProjectorSlide
2012-08-15 10:58:29 +02:00
url = '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.
"""
2012-03-18 17:11:58 +01:00
selected = True if request.path.startswith('/projector/') else False
return Tab(
2012-08-15 10:58:29 +02:00
title=_('Dashboard'),
url=reverse('dashboard'),
permission=request.user.has_perm('projector.can_manage_projector') or
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
"""
Return the custom slide widget.
"""
2012-06-11 13:43:48 +02:00
return [
Widget(
name='projector',
template='projector/widget.html',
context={
'slides': ProjectorSlide.objects.all(),
2012-08-15 10:58:29 +02:00
'welcomepage_is_active': not bool(config["presentation"])},
permission_required = 'projector.can_manage_projector',
2012-06-11 13:43:48 +02:00
),
]