OpenSlides/openslides/projector/api.py

125 lines
3.2 KiB
Python
Raw Normal View History

2012-04-25 22:29:19 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
openslides.projector.api
~~~~~~~~~~~~~~~~~~~~~~~~
Useful functions for the projector app.
:copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
:license: GNU GPL, see LICENSE for more details.
"""
2012-04-14 20:10:49 +02:00
from django.template.loader import render_to_string
from openslides.config.models import config
from openslides.projector.projector import SLIDE, Slide, Widget
2012-06-11 13:43:48 +02:00
def split_sid(sid):
2012-04-17 12:07:32 +02:00
try:
data = sid.split('-')
except AttributeError:
return None
2012-02-09 01:46:58 +01:00
if len(data) == 2:
model = data[0]
id = data[1]
return (model, id)
2012-02-09 01:46:58 +01:00
if len(data) == 1:
try:
2012-04-18 15:47:51 +02:00
return (SLIDE[data[0]].key, None)
2012-02-09 01:46:58 +01:00
except KeyError:
return None
return None
def get_slide_from_sid(sid, element=False):
2012-04-17 12:07:32 +02:00
try:
key, id = split_sid(sid)
except TypeError:
return None
if id is not None:
try:
object = SLIDE[key].model.objects.get(pk=id)
except SLIDE[key].model.DoesNotExist:
return None
if element:
return object
return object.slide()
try:
return SLIDE[key].func()
except KeyError:
return None
2012-02-06 22:22:16 +01:00
def get_active_slide(only_sid=False):
"""
2012-02-06 22:22:16 +01:00
Returns the active slide. If no slide is active, or it can not find an Item,
it raise an error
2012-02-06 22:22:16 +01:00
if only_sid is True, returns only the id of this item. Returns None if not Item
is active. Does not Raise Item.DoesNotExist
"""
2012-02-15 12:04:11 +01:00
sid = config["presentation"]
2012-02-06 22:22:16 +01:00
if only_sid:
return sid
return get_slide_from_sid(sid)
def set_active_slide(sid, argument=None):
2012-02-15 12:04:11 +01:00
config["presentation"] = sid
config['presentation_argument'] = argument
2012-02-09 02:29:38 +01:00
2012-04-14 20:10:49 +02:00
def register_slidemodel(model, model_name=None, control_template=None, weight=0):
2012-03-03 09:11:56 +01:00
#TODO: Warn if there already is a slide with this prefix
if model_name is None:
model_name = model.prefix
2012-04-14 11:18:47 +02:00
2012-04-14 20:10:49 +02:00
if control_template is None:
control_template = 'projector/default_control_slidemodel.html'
2012-04-14 11:18:47 +02:00
category = model.__module__.split('.')[0]
2012-04-14 20:10:49 +02:00
SLIDE[model_name] = Slide(
2012-03-03 09:11:56 +01:00
model_slide=True,
model=model,
category=category,
key=model.prefix,
2012-03-03 11:16:10 +01:00
model_name=model_name,
2012-04-14 20:10:49 +02:00
control_template=control_template,
weight=weight,
2012-03-03 09:11:56 +01:00
)
2012-04-15 15:46:32 +02:00
def register_slidefunc(key, func, control_template=None, weight=0, name=''):
2012-03-03 09:11:56 +01:00
#TODO: Warn if there already is a slide with this prefix
2012-04-14 20:10:49 +02:00
if control_template is None:
control_template = 'projector/default_control_slidefunc.html'
2012-04-14 11:18:47 +02:00
category = func.__module__.split('.')[0]
2012-03-03 09:11:56 +01:00
SLIDE[key] = Slide(
model_slide=False,
func=func,
category=category,
key=key,
2012-04-14 20:10:49 +02:00
control_template=control_template,
weight=weight,
2012-04-15 15:46:32 +02:00
name=name,
2012-03-03 09:11:56 +01:00
)
def projector_message_set(message, sid=None):
from models import ProjectorOverlay
config['projector_message'] = message
try:
overlay = ProjectorOverlay.objects.get(def_name='Message')
except ProjectorOverlay.DoesNotExist:
overlay = ProjectorOverlay(def_name='Message', active=True)
overlay.sid=sid
overlay.save()
def projector_message_delete():
config['projector_message'] = ''