OpenSlides/openslides/projector/projector.py

181 lines
5.1 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.api import config
2013-06-13 23:38:58 +02:00
from django.template import RequestContext
from openslides.utils.exceptions import OpenSlidesError
2012-03-12 18:22:18 +01:00
SLIDE = {}
2012-03-12 18:22:18 +01:00
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
"""
2012-11-26 10:05:51 +01:00
if self.id is None:
return False
from openslides.projector.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
"""
from openslides.projector.api import set_active_slide
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.
"""
2013-06-13 23:38:58 +02:00
def __init__(self, request, name, html=None, template=None, context={},
permission_required=None, display_name=None, default_column=1,
default_weight=0):
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:
2013-06-13 23:38:58 +02:00
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.')
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
self.default_weight = default_weight
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):
2013-09-07 15:19:03 +02:00
return repr(self.display_name)
2012-06-11 13:43:48 +02:00
2012-11-07 21:12:52 +01:00
def __unicode__(self):
return unicode(self.display_name)
2012-06-11 13:43:48 +02:00
2013-04-15 10:40:47 +02:00
class Overlay(object):
"""
Represents an overlay which can be seen on the projector.
"""
2013-04-15 10:40:47 +02:00
def __init__(self, name, get_widget_html, get_projector_html):
self.name = name
self.widget_html_callback = get_widget_html
self.projector_html_callback = get_projector_html
2013-04-15 10:40:47 +02:00
def get_widget_html(self):
return self.widget_html_callback()
2013-04-15 10:40:47 +02:00
def get_projector_html(self):
try:
return self._projector_html
except AttributeError:
self._projector_html = self.projector_html_callback()
return self.get_projector_html()
2013-04-15 10:40:47 +02:00
def is_active(self):
2013-04-15 15:47:18 +02:00
return self.name in config['projector_active_overlays']
def show_on_projector(self):
2013-03-18 12:34:47 +01:00
return self.is_active() and self.get_projector_html() is not None