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-08-15 13:42:25 +02:00
|
|
|
from django.conf import settings
|
2012-10-25 15:17:25 +02:00
|
|
|
from django.core.cache import cache
|
2012-08-15 13:42:25 +02:00
|
|
|
from django.utils.datastructures import SortedDict
|
|
|
|
from django.utils.importlib import import_module
|
2012-02-03 23:12:28 +01:00
|
|
|
|
2013-03-01 17:13:12 +01:00
|
|
|
from openslides.config.api import config
|
2012-11-24 14:01:21 +01:00
|
|
|
from openslides.projector.projector import SLIDE, Slide
|
2012-06-11 13:43:48 +02:00
|
|
|
|
2012-02-03 23:12:28 +01:00
|
|
|
|
2012-04-16 16:35:30 +02:00
|
|
|
def split_sid(sid):
|
2012-07-07 14:01:40 +02:00
|
|
|
"""
|
|
|
|
Slit a SID in the model-part and in the model-id
|
|
|
|
"""
|
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]
|
2012-04-16 16:35:30 +02:00
|
|
|
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
|
2012-02-03 23:12:28 +01:00
|
|
|
|
|
|
|
|
2012-04-19 12:46:04 +02:00
|
|
|
def get_slide_from_sid(sid, element=False):
|
2012-07-07 14:01:40 +02:00
|
|
|
"""
|
|
|
|
Return the Slide for an given sid.
|
|
|
|
If element== False, return the slide-dict,
|
|
|
|
else, return the object.
|
|
|
|
"""
|
2012-04-17 12:07:32 +02:00
|
|
|
try:
|
|
|
|
key, id = split_sid(sid)
|
|
|
|
except TypeError:
|
|
|
|
return None
|
|
|
|
|
2012-04-16 16:35:30 +02:00
|
|
|
if id is not None:
|
2012-06-03 09:43:28 +02:00
|
|
|
try:
|
|
|
|
object = SLIDE[key].model.objects.get(pk=id)
|
2012-06-03 10:19:08 +02:00
|
|
|
except SLIDE[key].model.DoesNotExist:
|
2012-06-03 09:43:28 +02:00
|
|
|
return None
|
2012-04-19 12:46:04 +02:00
|
|
|
if element:
|
|
|
|
return object
|
|
|
|
return object.slide()
|
2012-04-16 16:35:30 +02:00
|
|
|
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-03 23:12:28 +01:00
|
|
|
"""
|
2012-02-06 22:22:16 +01:00
|
|
|
Returns the active slide. If no slide is active, or it can not find an Item,
|
2012-07-07 14:01:40 +02:00
|
|
|
return None
|
2012-02-03 23:12:28 +01:00
|
|
|
|
2012-07-07 14:01:40 +02:00
|
|
|
if only_sid is True, returns only the id of this item. Returns None if not
|
|
|
|
Item is active.
|
2012-02-03 23:12:28 +01:00
|
|
|
"""
|
2012-02-15 12:04:11 +01:00
|
|
|
sid = config["presentation"]
|
2012-02-03 23:12:28 +01:00
|
|
|
|
2012-02-06 22:22:16 +01:00
|
|
|
if only_sid:
|
|
|
|
return sid
|
|
|
|
return get_slide_from_sid(sid)
|
2012-02-03 23:12:28 +01:00
|
|
|
|
|
|
|
|
2012-07-01 16:10:26 +02:00
|
|
|
def set_active_slide(sid, argument=None):
|
2012-07-07 14:01:40 +02:00
|
|
|
"""
|
|
|
|
Set the active Slide.
|
|
|
|
"""
|
2012-02-15 12:04:11 +01:00
|
|
|
config["presentation"] = sid
|
2012-07-01 16:10:26 +02:00
|
|
|
config['presentation_argument'] = argument
|
2012-10-29 23:26:10 +01:00
|
|
|
clear_projector_cache()
|
|
|
|
|
|
|
|
|
|
|
|
def clear_projector_cache():
|
2012-10-25 15:17:25 +02:00
|
|
|
cache.delete('projector_content')
|
|
|
|
cache.delete('projector_scrollcontent')
|
2012-10-29 23:26:10 +01:00
|
|
|
cache.delete('projector_data')
|
2012-02-09 02:29:38 +01:00
|
|
|
|
|
|
|
|
2012-11-24 14:01:21 +01:00
|
|
|
def register_slidemodel(model, model_name=None, control_template=None, weight=0):
|
2012-07-07 14:01:40 +02:00
|
|
|
"""
|
|
|
|
Register a Model as a slide.
|
|
|
|
"""
|
2012-11-24 14:01:21 +01:00
|
|
|
# TODO: control_template should never be None
|
2012-03-06 12:16:03 +01:00
|
|
|
if model_name is None:
|
|
|
|
model_name = model.prefix
|
2012-04-14 11:18:47 +02:00
|
|
|
|
|
|
|
category = model.__module__.split('.')[0]
|
2012-11-24 14:01:21 +01:00
|
|
|
SLIDE[model_name] = Slide(model_slide=True, model=model, category=category,
|
|
|
|
key=model.prefix, model_name=model_name,
|
|
|
|
control_template=control_template, weight=weight)
|
2012-02-03 23:12:28 +01:00
|
|
|
|
|
|
|
|
2012-04-15 15:46:32 +02:00
|
|
|
def register_slidefunc(key, func, control_template=None, weight=0, name=''):
|
2012-07-07 14:01:40 +02:00
|
|
|
"""
|
|
|
|
Register a function for as a slide.
|
|
|
|
"""
|
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-11-24 14:01:21 +01:00
|
|
|
SLIDE[key] = Slide(model_slide=False, func=func, category=category,
|
|
|
|
key=key, control_template=control_template, weight=weight,
|
|
|
|
name=name,)
|
2012-04-16 16:35:30 +02:00
|
|
|
|
|
|
|
|
2012-08-15 13:42:25 +02:00
|
|
|
def get_all_widgets(request, session=False):
|
2013-06-13 23:38:58 +02:00
|
|
|
"""
|
|
|
|
Collects the widgets from all apps and returns the Widget objects as sorted
|
|
|
|
dictionary.
|
|
|
|
|
|
|
|
The session flag decides whether to return only the widgets which are
|
|
|
|
active, that means that they are mentioned in the session.
|
|
|
|
"""
|
2013-09-07 15:09:37 +02:00
|
|
|
all_module_widgets = []
|
2012-08-15 13:42:25 +02:00
|
|
|
for app in settings.INSTALLED_APPS:
|
|
|
|
try:
|
|
|
|
mod = import_module(app + '.views')
|
|
|
|
except ImportError:
|
|
|
|
continue
|
|
|
|
try:
|
2013-09-07 15:09:37 +02:00
|
|
|
module_widgets = mod.get_widgets(request)
|
2012-08-15 13:42:25 +02:00
|
|
|
except AttributeError:
|
|
|
|
continue
|
2013-09-07 15:09:37 +02:00
|
|
|
all_module_widgets.extend(module_widgets)
|
|
|
|
all_module_widgets.sort(key=lambda widget: widget.default_weight)
|
|
|
|
session_widgets = request.session.get('widgets', {})
|
|
|
|
widgets = SortedDict()
|
|
|
|
for widget in all_module_widgets:
|
|
|
|
if (widget.permission_required is None or
|
|
|
|
request.user.has_perm(widget.permission_required)):
|
|
|
|
if not session or session_widgets.get(widget.get_name(), True):
|
|
|
|
widgets[widget.get_name()] = widget
|
2012-08-15 13:42:25 +02:00
|
|
|
return widgets
|