OpenSlides/openslides/agenda/models.py

179 lines
5.0 KiB
Python
Raw Normal View History

2011-07-31 10:46:29 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
openslides.agenda.models
~~~~~~~~~~~~~~~~~~~~~~~~
Models for the agenda app.
2012-04-25 22:29:19 +02:00
:copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
2011-07-31 10:46:29 +02:00
:license: GNU GPL, see LICENSE for more details.
"""
try:
import json
except ImportError:
import simplejson as json
2011-07-31 10:46:29 +02:00
from django.db import models
from django.core.urlresolvers import reverse
2011-07-31 10:46:29 +02:00
from mptt.models import MPTTModel, TreeForeignKey
from config.models import config
2012-02-15 12:04:11 +01:00
2012-03-12 18:22:18 +01:00
from projector.projector import SlideMixin
from projector.api import register_slidemodel, get_slide_from_sid
2012-02-15 12:04:11 +01:00
2012-04-14 14:31:09 +02:00
from utils.translation_ext import ugettext as _
2011-07-31 10:46:29 +02:00
2012-04-14 11:18:47 +02:00
2012-03-03 09:11:56 +01:00
class Item(MPTTModel, SlideMixin):
2011-07-31 10:46:29 +02:00
"""
An Agenda Item
MPTT-model. See http://django-mptt.github.com/django-mptt/
2011-07-31 10:46:29 +02:00
"""
2012-02-09 02:29:38 +01:00
prefix = 'item'
title = models.CharField(null=True, max_length=256, verbose_name=_("Title"))
text = models.TextField(null=True, blank=True, verbose_name=_("Text"))
comment = models.TextField(null=True, blank=True, verbose_name=_("Comment"))
2011-07-31 10:46:29 +02:00
closed = models.BooleanField(default=False, verbose_name=_("Closed"))
weight = models.IntegerField(default=0, verbose_name=_("Weight"))
parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
releated_sid = models.CharField(null=True, blank=True, max_length=64)
def get_releated_slide(self):
return get_slide_from_sid(self.releated_sid, True)
def get_releated_type(self):
return self.get_releated_slide().prefix
2012-06-30 11:06:32 +02:00
def print_releated_type(self):
"""
Print the type of the releated item.
For use in Template
??Why does {% trans item.print_releated_type|capfirst %} not work??
"""
return _(self.get_releated_type().capitalize())
def get_title(self):
if self.releated_sid is None:
return self.title
return self.get_releated_slide().get_agenda_title()
2012-02-06 22:08:08 +01:00
def slide(self):
2011-07-31 10:46:29 +02:00
"""
2012-02-06 22:08:08 +01:00
Return a map with all Data for the Slide
2011-07-31 10:46:29 +02:00
"""
if self.releated_sid:
return self.get_releated_slide().slide()
2012-02-09 01:46:58 +01:00
data = {
'item': self,
'title': self.get_title(),
2012-02-06 22:08:08 +01:00
'template': 'projector/AgendaText.html',
}
2012-02-09 01:46:58 +01:00
return data
2011-07-31 10:46:29 +02:00
def set_closed(self, closed=True):
"""
Changes the closed-status of the item.
"""
self.closed = closed
self.save()
2012-02-09 02:29:38 +01:00
@property
def active_parent(self):
"""
Return True if the item has a active parent
"""
sid = get_active_slide(only_sid=True).split()
if len(sid) == 2 and sid[0] == self.prefix:
if self.get_ancestors().filter(pk=sid[0]).exists():
2012-02-09 02:29:38 +01:00
return True
return False
2011-07-31 10:46:29 +02:00
@property
def weight_form(self):
"""
Return the WeightForm for this item.
"""
2012-02-06 22:22:16 +01:00
from agenda.forms import ItemOrderForm
2011-07-31 10:46:29 +02:00
try:
parent = self.parent.id
except AttributeError:
parent = 0
initial = {
'weight': self.weight,
'self': self.id,
'parent': parent,
}
2012-02-06 22:22:16 +01:00
return ItemOrderForm(initial=initial, prefix="i%d" % self.id)
2011-07-31 10:46:29 +02:00
def delete(self, with_children=False):
"""
Delete the Item.
"""
if not with_children:
for child in self.get_children():
child.move_to(self.parent)
child.save()
super(Item, self).delete()
Item.objects.rebuild()
2011-07-31 10:46:29 +02:00
def get_absolute_url(self, link='view'):
"""
Return the URL to this item. By default it is the Link to its
2012-02-06 22:08:08 +01:00
slide
2011-07-31 10:46:29 +02:00
link can be:
* view
2012-06-23 10:27:58 +02:00
* edit
2011-07-31 10:46:29 +02:00
* delete
"""
if link == 'view':
if self.releated_sid:
return self.get_releated_slide().get_absolute_url(link)
return reverse('item_view', args=[str(self.id)])
if link == 'edit':
if self.releated_sid:
return self.get_releated_slide().get_absolute_url(link)
return reverse('item_edit', args=[str(self.id)])
2011-07-31 10:46:29 +02:00
if link == 'delete':
return reverse('item_delete', args=[str(self.id)])
2011-07-31 10:46:29 +02:00
def __unicode__(self):
return self.get_title()
2011-07-31 10:46:29 +02:00
class Meta:
permissions = (
('can_see_agenda', _("Can see agenda", fixstr=True)),
('can_manage_agenda', _("Can manage agenda", fixstr=True)),
2011-07-31 10:46:29 +02:00
)
class MPTTMeta:
order_insertion_by = ['weight']
2011-07-31 10:46:29 +02:00
2012-04-18 14:53:19 +02:00
register_slidemodel(Item, control_template='agenda/control_item.html')
2011-07-31 10:46:29 +02:00
2012-03-03 09:11:56 +01:00
# TODO: put this in another file
2012-02-15 12:36:50 +01:00
from projector.api import register_slidefunc
from agenda.slides import agenda_show
2012-04-15 15:46:32 +02:00
register_slidefunc('agenda', agenda_show, weight=-1, name=_('Agenda'))
from django.dispatch import receiver
from openslides.config.signals import default_config_value
@receiver(default_config_value, dispatch_uid="agenda_default_config")
def default_config(sender, key, **kwargs):
return {
'agenda_countdown_time': 60,
}.get(key)