OpenSlides/openslides/projector/views.py

289 lines
9.8 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-04-25 22:29:19 +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-05-18 23:07:09 +02:00
from django.contrib import messages
2012-02-03 20:49:16 +01:00
from django.shortcuts import render_to_response, redirect
from django.template import RequestContext
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _
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-04-14 20:10:49 +02:00
from django.dispatch import receiver
from django.template.loader import render_to_string
from django.db.models import Q
2012-02-03 20:49:16 +01:00
2012-05-18 23:24:54 +02:00
from utils.views import TemplateView, RedirectView, CreateView, UpdateView, DeleteView
2012-06-10 10:43:50 +02:00
from utils.utils import (template, permission_required, del_confirm_form,
2012-06-11 13:43:48 +02:00
ajax_request)
from utils.template import render_block_to_string, Tab
from openslides.utils.signals import template_manipulation
2012-02-03 20:49:16 +01:00
from config.models import config
2012-06-11 13:43:48 +02:00
import settings
2012-02-03 20:49:16 +01:00
2012-04-18 15:47:51 +02:00
from api import get_active_slide, set_active_slide, projector_message_set, projector_message_delete, get_slide_from_sid
2012-06-11 13:43:48 +02:00
from projector import SLIDE, Widget
2012-05-18 23:07:09 +02:00
from models import ProjectorOverlay, ProjectorSlide
from openslides.projector.signals import projector_overlays, projector_control_box
2012-03-03 09:11:56 +01:00
2012-03-06 19:55:19 +01:00
class ControlView(TemplateView):
template_name = 'projector/control.html'
2012-03-18 14:35:01 +01:00
permission_required = 'projector.can_manage_projector'
2012-03-03 09:11:56 +01:00
def get_projector_overlays(self):
overlays = []
for receiver, name in projector_overlays.send(sender='registerer', register=True):
if name is not None:
try:
projector_overlay = ProjectorOverlay.objects.get(def_name=name)
except ProjectorOverlay.DoesNotExist:
active = name == 'Message'
projector_overlay = ProjectorOverlay(def_name=name, active=active)
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()
else:
for overlay in self.get_projector_overlays():
if overlay.def_name in request.POST:
overlay.active = True
else:
overlay.active = False
overlay.save()
return self.get(request, *args, **kwargs)
2012-03-03 09:11:56 +01:00
def get_context_data(self, **kwargs):
2012-03-06 19:55:19 +01:00
context = super(ControlView, 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:
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 ActivateView(RedirectView):
url = 'projector_control'
2012-03-03 09:11:56 +01:00
allow_ajax = True
def pre_redirect(self, request, *args, **kwargs):
set_active_slide(kwargs['sid'])
config['up'] = 0
config['bigger'] = 100
2012-02-03 20:57:08 +01:00
2012-05-18 23:24:54 +02:00
2012-05-18 23:07:09 +02:00
class CustomSlideCreateView(CreateView):
permission_required = 'agenda.can_manage_agenda'
template_name = 'projector/new.html'
model = ProjectorSlide
context_object_name = 'customslide'
success_url = 'projector_control'
apply_url = 'customslide_edit'
def get_success_url(self):
messages.success(self.request, _("Custom slide <b>%s</b> was successfully created.") % self.request.POST['title'])
if 'apply' in self.request.POST:
return reverse(self.get_apply_url(), args=[self.object.id])
return reverse(super(CreateView, self).get_success_url())
2012-05-18 23:24:54 +02:00
2012-05-18 23:07:09 +02:00
class CustomSlideUpdateView(UpdateView):
permission_required = 'projector.can_manage_projector'
template_name = 'projector/new.html'
model = ProjectorSlide
context_object_name = 'customslide'
success_url = 'projector_control'
apply_url = 'customslide_edit'
def get_success_url(self):
messages.success(self.request, _("Custom slide <b>%s</b> was successfully modified.") % self.request.POST['title'])
if 'apply' in self.request.POST:
return ''
return reverse(super(UpdateView, self).get_success_url())
2012-02-03 20:49:16 +01:00
2012-05-18 23:24:54 +02:00
class CustomSlideDeleteView(DeleteView):
permission_required = 'projector.can_manage_projector'
model = ProjectorSlide
url = 'projector_control'
def pre_post_redirect(self, request, *args, **kwargs):
self.object = self.get_object()
self.object.delete()
messages.success(request, _("Custom slide <b>%s</b> was successfully deleted.") % self.object)
2012-03-12 18:22:18 +01:00
@permission_required('projector.can_see_projector')
2012-04-18 15:47:51 +02:00
def active_slide(request, sid=None):
2012-02-03 20:49:16 +01:00
"""
2012-02-03 20:57:08 +01:00
Shows the active Slide.
2012-02-03 20:49:16 +01:00
"""
2012-04-18 15:47:51 +02:00
if sid is None:
try:
data = get_active_slide()
except AttributeError: #TODO: It has to be an Slide.DoesNotExist
data = None
ajax = 'on'
2012-04-18 15:47:51 +02:00
else:
data = get_slide_from_sid(sid)
ajax = 'off'
2012-03-03 09:11:56 +01:00
if data is None:
2012-02-09 01:46:58 +01:00
data = {
2012-02-15 12:04:11 +01:00
'title': config['event_name'],
2012-02-09 01:46:58 +01:00
'template': 'projector/default.html',
}
data['overlays'] = []
data['overlay'] = ''
data['ajax'] = ajax
2012-04-14 11:37:12 +02:00
# Projector Overlays
sid = get_active_slide(True)
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)
template_manipulation.send(sender='projector', request=request, context=data)
2012-02-03 20:49:16 +01:00
if request.is_ajax():
content = render_block_to_string(data['template'], 'content', data)
scrollcontent = render_block_to_string(data['template'], 'scrollcontent', data)
jsondata = {
'content': content,
'scrollcontent': scrollcontent,
'overlays': data['overlays'],
'title': data['title'],
'time': datetime.now().strftime('%H:%M'),
2012-02-15 12:04:11 +01:00
'bigger': config['bigger'],
'up': config['up'],
'overlay': data['overlay']
}
2012-02-03 20:49:16 +01:00
return ajax_request(jsondata)
else:
return render_to_response(
data['template'],
data,
context_instance=RequestContext(request)
)
2012-02-03 20:49:16 +01:00
class ProjectorEdit(RedirectView):
permission_required = 'projector.can_manage_projector'
url = 'projector_control'
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):
permission_required = 'projector.can_manage_projector'
url = 'projector_control'
allow_ajax = True
def pre_redirect(self, request, *args, **kwargs):
command = kwargs['command']
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()
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:
config['countdown_time'] = int(self.request.GET['countdown_time'])
except ValueError:
pass
except AttributeError:
pass
2012-03-18 17:11:58 +01:00
def register_tab(request):
selected = True if request.path.startswith('/projector/') else False
return Tab(
title=_('Projector'),
url=reverse('projector_control'),
2012-05-20 19:43:38 +02:00
permission=request.user.has_perm('projector.can_manage_projector'),
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):
return [
Widget(
name='projector',
template='projector/widget.html',
context={
'slides': ProjectorSlide.objects.all(),
}
),
]