2012-04-25 22:29:19 +02:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
openslides.projector.models
|
2012-07-07 14:01:40 +02:00
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2012-04-25 22:29:19 +02:00
|
|
|
|
|
|
|
|
|
Models for the projector app.
|
|
|
|
|
|
2013-03-01 17:13:12 +01:00
|
|
|
|
:copyright: 2011–2013 by OpenSlides team, see AUTHORS.
|
2012-04-25 22:29:19 +02:00
|
|
|
|
:license: GNU GPL, see LICENSE for more details.
|
|
|
|
|
"""
|
2012-04-18 18:55:33 +02:00
|
|
|
|
|
2012-02-03 20:49:16 +01:00
|
|
|
|
from django.db import models
|
2012-04-16 16:35:30 +02:00
|
|
|
|
from django.dispatch import receiver
|
2013-04-22 19:59:05 +02:00
|
|
|
|
from django.utils.translation import ugettext_lazy, ugettext_noop
|
2013-08-04 12:59:11 +02:00
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
|
from django.core.urlresolvers import reverse
|
2012-04-16 16:35:30 +02:00
|
|
|
|
|
2013-08-04 12:59:11 +02:00
|
|
|
|
from openslides.utils.utils import int_or_none
|
2012-07-07 14:01:40 +02:00
|
|
|
|
|
2012-04-14 12:52:56 +02:00
|
|
|
|
|
2013-10-03 21:49:51 +02:00
|
|
|
|
class RelatedModelMixin(object):
|
|
|
|
|
"""
|
|
|
|
|
Mixin for motion related models, that appear on the motion slide.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
Saves the model and updates the projector, if the motion in on it.
|
|
|
|
|
"""
|
|
|
|
|
from .api import update_projector
|
|
|
|
|
value = super(RelatedModelMixin, self).save(*args, **kwargs)
|
|
|
|
|
if self.get_related_model().is_active_slide():
|
|
|
|
|
update_projector()
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
Deletes the model and updates the projector, if the motion in on it.
|
|
|
|
|
"""
|
|
|
|
|
from .api import update_projector
|
|
|
|
|
value = super(RelatedModelMixin, self).delete(*args, **kwargs)
|
|
|
|
|
if self.get_related_model().is_active_slide():
|
|
|
|
|
update_projector()
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
def get_related_model(self):
|
|
|
|
|
"""
|
|
|
|
|
Return the pk of the related model.
|
|
|
|
|
"""
|
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
|
'%s has to have a method "get_related_model_pk"' % type(self))
|
|
|
|
|
|
|
|
|
|
|
2013-08-04 12:59:11 +02:00
|
|
|
|
class SlideMixin(object):
|
|
|
|
|
"""
|
|
|
|
|
A Mixin for a Django-Model, for making the model a slide.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
slide_callback_name = None
|
|
|
|
|
"""
|
|
|
|
|
Name of the callback to render the model as slide.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
Updates the projector, if 'self' is the active slide.
|
|
|
|
|
"""
|
|
|
|
|
from openslides.projector.api import update_projector
|
2013-10-03 21:49:51 +02:00
|
|
|
|
value = super(SlideMixin, self).save(*args, **kwargs)
|
2013-08-04 12:59:11 +02:00
|
|
|
|
if self.is_active_slide():
|
|
|
|
|
update_projector()
|
2013-10-03 21:49:51 +02:00
|
|
|
|
return value
|
2013-08-04 12:59:11 +02:00
|
|
|
|
|
|
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
|
from openslides.projector.api import update_projector
|
2013-10-03 21:49:51 +02:00
|
|
|
|
value = super(SlideMixin, self).delete(*args, **kwargs)
|
2013-08-04 12:59:11 +02:00
|
|
|
|
if self.is_active_slide():
|
|
|
|
|
update_projector()
|
2013-10-03 21:49:51 +02:00
|
|
|
|
return value
|
2013-08-04 12:59:11 +02:00
|
|
|
|
|
|
|
|
|
def get_absolute_url(self, link='projector'):
|
|
|
|
|
"""
|
2013-10-03 21:49:51 +02:00
|
|
|
|
Return the url to activate the slide, if link == 'projector'.
|
2013-08-04 12:59:11 +02:00
|
|
|
|
"""
|
|
|
|
|
if link in ('projector', 'projector_preview'):
|
2013-10-03 21:49:51 +02:00
|
|
|
|
url_name = {'projector': 'projector_activate_slide',
|
|
|
|
|
'projector_preview': 'projector_preview'}[link]
|
|
|
|
|
value = '%s?pk=%d' % (
|
2013-08-04 12:59:11 +02:00
|
|
|
|
reverse(url_name,
|
2013-10-03 21:49:51 +02:00
|
|
|
|
args=[self.slide_callback_name]),
|
2013-08-04 12:59:11 +02:00
|
|
|
|
self.pk)
|
2013-10-03 21:49:51 +02:00
|
|
|
|
else:
|
|
|
|
|
value = super(SlideMixin, self).get_absolute_url(link)
|
|
|
|
|
return value
|
2013-08-04 12:59:11 +02:00
|
|
|
|
|
|
|
|
|
def is_active_slide(self):
|
|
|
|
|
"""
|
|
|
|
|
Return True, if the the slide is the active slide.
|
|
|
|
|
"""
|
|
|
|
|
from openslides.projector.api import get_active_slide
|
|
|
|
|
active_slide = get_active_slide()
|
|
|
|
|
pk = int_or_none(active_slide.get('pk', None))
|
2013-10-03 21:49:51 +02:00
|
|
|
|
return (active_slide['callback'] == self.slide_callback_name and
|
2013-08-04 12:59:11 +02:00
|
|
|
|
self.pk == pk)
|
|
|
|
|
|
2013-10-03 21:49:51 +02:00
|
|
|
|
def get_slide_context(self, **context):
|
|
|
|
|
"""
|
|
|
|
|
Returns the context for the template which renders the slide.
|
|
|
|
|
"""
|
|
|
|
|
slide_name = self._meta.object_name.lower()
|
|
|
|
|
context.update({'slide': self,
|
|
|
|
|
slide_name: self})
|
|
|
|
|
return context
|
|
|
|
|
|
2013-08-04 12:59:11 +02:00
|
|
|
|
|
|
|
|
|
class ProjectorSlide(SlideMixin, models.Model):
|
2012-07-07 14:01:40 +02:00
|
|
|
|
"""
|
|
|
|
|
Model for Slides, only for the projector. Also called custom slides.
|
|
|
|
|
"""
|
2013-10-03 21:49:51 +02:00
|
|
|
|
# TODO: Rename it to CustomSlide
|
2013-08-04 12:59:11 +02:00
|
|
|
|
slide_callback_name = 'projector_slide'
|
2012-03-12 18:22:18 +01:00
|
|
|
|
|
2013-04-22 19:59:05 +02:00
|
|
|
|
title = models.CharField(max_length=256, verbose_name=ugettext_lazy("Title"))
|
|
|
|
|
text = models.TextField(null=True, blank=True, verbose_name=ugettext_lazy("Text"))
|
|
|
|
|
weight = models.IntegerField(default=0, verbose_name=ugettext_lazy("Weight"))
|
2012-03-12 18:22:18 +01:00
|
|
|
|
|
2013-08-04 12:59:11 +02:00
|
|
|
|
def get_absolute_url(self, link='update'):
|
|
|
|
|
if link == 'edit' or link == 'update':
|
|
|
|
|
return reverse('customslide_edit', args=[str(self.pk)])
|
2012-05-18 23:24:54 +02:00
|
|
|
|
if link == 'delete':
|
2013-08-04 12:59:11 +02:00
|
|
|
|
return reverse('customslide_delete', args=[str(self.pk)])
|
|
|
|
|
return super(ProjectorSlide, self).get_absolute_url(link)
|
2012-05-18 23:24:54 +02:00
|
|
|
|
|
2012-03-12 18:22:18 +01:00
|
|
|
|
def __unicode__(self):
|
|
|
|
|
return self.title
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
permissions = (
|
2012-07-07 14:01:40 +02:00
|
|
|
|
('can_manage_projector', ugettext_noop("Can manage the projector")),
|
2012-08-15 10:58:29 +02:00
|
|
|
|
('can_see_projector', ugettext_noop("Can see the projector")),
|
|
|
|
|
('can_see_dashboard', ugettext_noop("Can see the dashboard")),
|
2012-03-12 18:22:18 +01:00
|
|
|
|
)
|