OpenSlides/openslides/projector/projector.py

175 lines
4.9 KiB
Python
Raw Normal View History

2012-04-25 22:29:19 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
openslides.projector.projector
2012-07-07 14:01:40 +02:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2012-04-25 22:29:19 +02:00
Slide functions for the projector app.
:copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
:license: GNU GPL, see LICENSE for more details.
"""
2012-04-14 09:47:34 +02:00
from time import time
from django.dispatch import receiver
2012-06-11 13:43:48 +02:00
from django.template.loader import render_to_string
from openslides.config.models import config
2012-03-12 18:22:18 +01:00
from openslides.projector.signals import projector_overlays
2012-03-12 18:22:18 +01:00
SLIDE = {}
class SlideMixin(object):
2012-06-11 13:43:48 +02:00
"""
A Mixin for a Django-Model, for making the model a slide.
"""
2012-03-12 18:22:18 +01:00
def slide(self):
"""
2012-07-07 14:01:40 +02:00
Return a map with all Data for the Slide.
2012-03-12 18:22:18 +01:00
"""
return {
'slide': self,
'title': 'dummy-title',
'template': 'projector/default.html',
}
@property
def sid(self):
"""
Return the sid from this Slide
"""
return "%s-%d" % (self.prefix, self.id)
2012-03-12 18:22:18 +01:00
@property
def active(self):
"""
2012-07-07 14:01:40 +02:00
Return True, if the the slide is the active slide.
2012-03-12 18:22:18 +01:00
"""
from api import get_active_slide
2012-07-07 14:01:40 +02:00
return get_active_slide(only_sid=True) == self.sid
2012-03-12 18:22:18 +01:00
def set_active(self):
"""
2012-07-07 14:01:40 +02:00
Appoint this item as the active slide.
2012-03-12 18:22:18 +01:00
"""
2012-07-07 14:01:40 +02:00
set_active_slide(self.sid)
2012-03-12 18:22:18 +01:00
def save(self, *args, **kwargs):
if self.active:
from api import clear_projector_cache
clear_projector_cache()
return super(SlideMixin, self).save(*args, **kwargs)
2012-03-12 18:22:18 +01:00
class Slide(object):
2012-07-07 14:01:40 +02:00
"""
Represents a Slide for the projector. Can be a modelinstanz, or a function.
"""
2012-04-14 20:10:49 +02:00
def __init__(self, model_slide=False, func=None, model=None, category=None,
2012-04-15 15:46:32 +02:00
key=None, model_name='', control_template='', weight=0, name=''):
2012-03-12 18:22:18 +01:00
"""
model_slide: Boolean if the value is a Model.
func: The function to call. Only if modelslide is False.
model: The model. Only if modelslide is True.
model_name: The name shown for the model.
category: The category to show this Slide.
2012-04-14 20:10:49 +02:00
key: the key in the slide object to find itself.
2012-03-12 18:22:18 +01:00
"""
self.model_slide = model_slide
self.func = func
self.model = model
self.model_name = model_name
self.category = category
self.key = key
2012-04-14 20:10:49 +02:00
self.control_template = control_template
self.weight = weight
2012-04-15 15:46:32 +02:00
self.name = name
2012-03-12 18:22:18 +01:00
2012-04-18 18:07:24 +02:00
@property
def active(self):
2012-07-07 14:01:40 +02:00
"""
Return True if the Slide is active, else: False.
"""
2012-04-18 18:07:24 +02:00
from api import get_active_slide
return get_active_slide(True) == self.key
2012-03-12 18:22:18 +01:00
def get_items(self):
2012-07-07 14:01:40 +02:00
"""
If the Slide is a Slide from a Model, return all Objects.
"""
2012-04-14 20:10:49 +02:00
try:
return self.model.objects.all()
except AttributeError:
return 'No Model'
2012-06-11 13:43:48 +02:00
class Widget(object):
2012-07-07 14:01:40 +02:00
"""
Class for a Widget for the Projector-Tab.
"""
2012-08-15 10:58:29 +02:00
def __init__(self, name, html=None, template=None, context={},
2012-08-15 11:56:43 +02:00
permission_required=None, display_name=None, default_column=1):
2012-06-11 13:43:48 +02:00
self.name = name
2012-08-15 11:56:43 +02:00
if display_name is None:
self.display_name = name.capitalize()
else:
self.display_name = display_name
2012-06-11 13:43:48 +02:00
if html is not None:
self.html = html
elif template is not None:
self.html = render_to_string(template, context)
2012-08-15 11:56:43 +02:00
2012-08-15 10:58:29 +02:00
self.permission_required = permission_required
2012-08-15 11:56:43 +02:00
self.default_column = default_column
2012-06-11 13:43:48 +02:00
def get_name(self):
2012-06-24 21:55:46 +02:00
return self.name.lower()
2012-06-11 13:43:48 +02:00
def get_html(self):
return self.html
def get_title(self):
2012-08-15 11:56:43 +02:00
return self.display_name
def __repr__(self):
2012-08-15 13:42:25 +02:00
return unicode(self.display_name)
2012-06-11 13:43:48 +02:00
@receiver(projector_overlays, dispatch_uid="projector_countdown")
def countdown(sender, **kwargs):
name = 'Countdown'
2012-04-14 09:47:34 +02:00
if kwargs['register']:
return name
if name in kwargs['call']:
if config['countdown_state'] == 'active':
seconds = max(0, int(config['countdown_start_stamp'] + config['countdown_time'] - time()))
elif config['countdown_state'] == 'paused':
seconds = max(0, int(config['countdown_start_stamp'] + config['countdown_time'] - config['countdown_pause_stamp']))
elif config['countdown_state'] == 'inactive':
seconds = max(0, int(config['countdown_time']))
else:
seconds = 0
if seconds == 0:
config['countdown_state'] = 'expired'
return (name, '%02d:%02d' % (seconds / 60, seconds % 60))
return None
@receiver(projector_overlays, dispatch_uid="projector_message")
def projector_message(sender, **kwargs):
name = 'Message'
if kwargs['register']:
return name
if name in kwargs['call']:
message = config['projector_message']
if message != '':
return (name, message)
return None