2011-07-31 10:46:29 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
openslides.agenda.views
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Views for the agenda app.
|
|
|
|
|
2012-04-25 22:29:19 +02:00
|
|
|
:copyright: 2011, 2012 by the OpenSlides team, see AUTHORS.
|
2011-07-31 10:46:29 +02:00
|
|
|
:license: GNU GPL, see LICENSE for more details.
|
|
|
|
"""
|
2012-02-21 13:17:42 +01:00
|
|
|
from reportlab.platypus import Paragraph
|
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from django.contrib import messages
|
2012-07-04 12:50:33 +02:00
|
|
|
from django.db import transaction
|
|
|
|
from django.db.models import Model
|
2012-07-23 12:57:47 +02:00
|
|
|
from django.utils.translation import ugettext as _, ugettext_lazy
|
2012-02-20 19:39:26 +01:00
|
|
|
from django.views.generic.detail import SingleObjectMixin
|
2012-02-20 17:46:45 +01:00
|
|
|
|
2012-07-01 15:35:05 +02:00
|
|
|
from openslides.utils.pdf import stylesheet
|
2012-11-24 14:01:21 +01:00
|
|
|
from openslides.utils.views import (
|
|
|
|
TemplateView, RedirectView, UpdateView, CreateView, DeleteView, PDFView,
|
|
|
|
DetailView)
|
2012-07-01 15:35:05 +02:00
|
|
|
from openslides.utils.template import Tab
|
2012-07-04 12:50:33 +02:00
|
|
|
from openslides.utils.utils import html_strong
|
|
|
|
from openslides.projector.api import get_active_slide
|
2012-07-01 15:35:05 +02:00
|
|
|
from openslides.projector.projector import Widget, SLIDE
|
2012-11-24 14:01:21 +01:00
|
|
|
from .models import Item
|
2012-11-24 15:00:17 +01:00
|
|
|
from .forms import ItemOrderForm, ItemForm
|
2012-02-20 17:46:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Overview(TemplateView):
|
2012-07-04 12:50:33 +02:00
|
|
|
"""
|
|
|
|
Show all agenda items, and update there range via post.
|
|
|
|
"""
|
2012-02-20 17:46:45 +01:00
|
|
|
permission_required = 'agenda.can_see_agenda'
|
|
|
|
template_name = 'agenda/overview.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2012-03-18 17:11:58 +01:00
|
|
|
context = super(Overview, self).get_context_data(**kwargs)
|
2012-02-20 17:46:45 +01:00
|
|
|
context.update({
|
|
|
|
'items': Item.objects.all(),
|
2012-07-04 12:50:33 +02:00
|
|
|
'active_sid': get_active_slide(only_sid=True),
|
2012-02-20 17:46:45 +01:00
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
2012-07-04 12:50:33 +02:00
|
|
|
@transaction.commit_manually
|
2012-02-20 17:46:45 +01:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
context = self.get_context_data(**kwargs)
|
2012-07-04 12:50:33 +02:00
|
|
|
if not request.user.has_perm('agenda.can_manage_agenda'):
|
2012-11-24 14:01:21 +01:00
|
|
|
messages.error(
|
|
|
|
request,
|
2012-08-05 22:31:20 +02:00
|
|
|
_('You are not authorized to manage the agenda.'))
|
2012-07-04 12:50:33 +02:00
|
|
|
return self.render_to_response(context)
|
|
|
|
transaction.commit()
|
2011-07-31 10:46:29 +02:00
|
|
|
for item in Item.objects.all():
|
2012-02-06 22:22:16 +01:00
|
|
|
form = ItemOrderForm(request.POST, prefix="i%d" % item.id)
|
2011-07-31 10:46:29 +02:00
|
|
|
if form.is_valid():
|
|
|
|
try:
|
2012-04-18 14:36:03 +02:00
|
|
|
parent = Item.objects.get(id=form.cleaned_data['parent'])
|
2011-07-31 10:46:29 +02:00
|
|
|
except Item.DoesNotExist:
|
2012-04-18 14:36:03 +02:00
|
|
|
parent = None
|
2011-07-31 10:46:29 +02:00
|
|
|
item.weight = form.cleaned_data['weight']
|
2012-04-18 14:36:03 +02:00
|
|
|
item.parent = parent
|
|
|
|
Model.save(item)
|
2012-07-04 12:50:33 +02:00
|
|
|
else:
|
|
|
|
transaction.rollback()
|
2012-11-24 14:01:21 +01:00
|
|
|
messages.error(
|
|
|
|
request, _('Errors when reordering of the agenda'))
|
2012-07-04 12:50:33 +02:00
|
|
|
return self.render_to_response(context)
|
2012-04-18 14:36:03 +02:00
|
|
|
Item.objects.rebuild()
|
2012-07-04 12:50:33 +02:00
|
|
|
# TODO: assure, that it is a valid tree
|
|
|
|
transaction.commit()
|
2012-02-20 17:46:45 +01:00
|
|
|
return self.render_to_response(context)
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
|
|
|
2012-04-15 09:55:21 +02:00
|
|
|
class View(DetailView):
|
2012-07-04 12:50:33 +02:00
|
|
|
"""
|
|
|
|
Show an agenda item.
|
|
|
|
"""
|
2012-04-15 09:55:21 +02:00
|
|
|
permission_required = 'agenda.can_see_agenda'
|
|
|
|
template_name = 'agenda/view.html'
|
|
|
|
model = Item
|
|
|
|
context_object_name = 'item'
|
|
|
|
|
|
|
|
|
2012-02-20 19:39:26 +01:00
|
|
|
class SetClosed(RedirectView, SingleObjectMixin):
|
2011-07-31 10:46:29 +02:00
|
|
|
"""
|
2012-07-04 12:50:33 +02:00
|
|
|
Close or open an item.
|
2011-07-31 10:46:29 +02:00
|
|
|
"""
|
2012-02-20 17:46:45 +01:00
|
|
|
permission_required = 'agenda.can_manage_agenda'
|
|
|
|
allow_ajax = True
|
|
|
|
url = 'item_overview'
|
2012-02-20 19:39:26 +01:00
|
|
|
model = Item
|
2011-09-02 20:46:24 +02:00
|
|
|
|
2012-02-20 17:46:45 +01:00
|
|
|
def get_ajax_context(self, **kwargs):
|
|
|
|
context = super(SetClosed, self).get_ajax_context(**kwargs)
|
|
|
|
closed = kwargs['closed']
|
2011-08-31 23:47:31 +02:00
|
|
|
if closed:
|
2012-02-20 19:39:26 +01:00
|
|
|
link = reverse('item_open', args=[self.object.id])
|
2011-08-31 23:47:31 +02:00
|
|
|
else:
|
2012-02-20 19:39:26 +01:00
|
|
|
link = reverse('item_close', args=[self.object.id])
|
2012-02-20 17:46:45 +01:00
|
|
|
context.update({
|
|
|
|
'closed': kwargs['closed'],
|
|
|
|
'link': link,
|
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
|
|
|
def pre_redirect(self, request, *args, **kwargs):
|
2012-02-20 19:39:26 +01:00
|
|
|
self.object = self.get_object()
|
2012-02-20 17:46:45 +01:00
|
|
|
closed = kwargs['closed']
|
2012-02-20 19:39:26 +01:00
|
|
|
self.object.set_closed(closed)
|
2012-02-20 17:46:45 +01:00
|
|
|
return super(SetClosed, self).pre_redirect(request, *args, **kwargs)
|
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2012-02-20 17:46:45 +01:00
|
|
|
class ItemUpdate(UpdateView):
|
2012-07-04 12:50:33 +02:00
|
|
|
"""
|
|
|
|
Update an existing item.
|
|
|
|
"""
|
2012-02-20 17:46:45 +01:00
|
|
|
permission_required = 'agenda.can_manage_agenda'
|
|
|
|
template_name = 'agenda/edit.html'
|
|
|
|
model = Item
|
|
|
|
context_object_name = 'item'
|
|
|
|
form_class = ItemForm
|
|
|
|
success_url = 'item_overview'
|
2012-04-27 21:36:12 +02:00
|
|
|
apply_url = 'item_edit'
|
|
|
|
|
|
|
|
def get_success_url(self):
|
2012-11-24 14:01:21 +01:00
|
|
|
messages.success(
|
|
|
|
self.request, _("Item %s was successfully modified.")
|
2012-07-04 12:50:33 +02:00
|
|
|
% html_strong(self.request.POST['title']))
|
2012-04-27 21:36:12 +02:00
|
|
|
if 'apply' in self.request.POST:
|
|
|
|
return ''
|
|
|
|
return reverse(super(UpdateView, self).get_success_url())
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2012-02-20 17:46:45 +01:00
|
|
|
|
|
|
|
class ItemCreate(CreateView):
|
2012-07-04 12:50:33 +02:00
|
|
|
"""
|
|
|
|
Create a new item.
|
|
|
|
"""
|
2012-02-20 17:46:45 +01:00
|
|
|
permission_required = 'agenda.can_manage_agenda'
|
|
|
|
template_name = 'agenda/edit.html'
|
|
|
|
model = Item
|
|
|
|
context_object_name = 'item'
|
|
|
|
form_class = ItemForm
|
|
|
|
success_url = 'item_overview'
|
2012-04-27 21:36:12 +02:00
|
|
|
apply_url = 'item_edit'
|
|
|
|
|
|
|
|
def get_success_url(self):
|
2012-11-24 14:01:21 +01:00
|
|
|
messages.success(
|
|
|
|
self.request, _("Item %s was successfully created.")
|
2012-07-04 12:50:33 +02:00
|
|
|
% html_strong(self.request.POST['title']))
|
2012-04-27 21:36:12 +02:00
|
|
|
if 'apply' in self.request.POST:
|
|
|
|
return reverse(self.get_apply_url(), args=[self.object.id])
|
|
|
|
return reverse(super(CreateView, self).get_success_url())
|
2012-02-20 17:46:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ItemDelete(DeleteView):
|
2011-07-31 10:46:29 +02:00
|
|
|
"""
|
2012-07-04 12:50:33 +02:00
|
|
|
Delete an item.
|
2011-07-31 10:46:29 +02:00
|
|
|
"""
|
2012-02-20 17:46:45 +01:00
|
|
|
permission_required = 'agenda.can_manage_agenda'
|
|
|
|
model = Item
|
|
|
|
url = 'item_overview'
|
|
|
|
|
2012-08-10 21:00:13 +02:00
|
|
|
def get_answer_options(self):
|
|
|
|
if self.object.children.exists():
|
|
|
|
return [('all', _("Yes, with all child items."))] + self.answer_options
|
|
|
|
else:
|
|
|
|
return self.answer_options
|
2012-02-09 02:29:38 +01:00
|
|
|
|
2012-08-10 21:00:13 +02:00
|
|
|
def pre_post_redirect(self, request, *args, **kwargs):
|
|
|
|
if self.get_answer() == 'all':
|
2012-06-23 11:41:32 +02:00
|
|
|
self.object.delete(with_children=True)
|
2012-11-24 14:01:21 +01:00
|
|
|
messages.success(
|
|
|
|
request, _("Item %s and his children were successfully deleted.")
|
2012-07-04 12:50:33 +02:00
|
|
|
% html_strong(self.object))
|
2012-08-10 21:00:13 +02:00
|
|
|
elif self.get_answer() == 'yes':
|
2012-06-23 11:41:32 +02:00
|
|
|
self.object.delete(with_children=False)
|
2012-11-24 14:01:21 +01:00
|
|
|
messages.success(
|
|
|
|
request, _("Item %s was successfully deleted.")
|
2012-07-04 12:50:33 +02:00
|
|
|
% html_strong(self.object))
|
2012-02-20 17:46:45 +01:00
|
|
|
|
2012-02-21 13:17:42 +01:00
|
|
|
|
2012-04-14 10:54:22 +02:00
|
|
|
class AgendaPDF(PDFView):
|
2012-07-04 12:50:33 +02:00
|
|
|
"""
|
|
|
|
Create a full agenda-PDF.
|
|
|
|
"""
|
2012-02-21 13:17:42 +01:00
|
|
|
permission_required = 'agenda.can_see_agenda'
|
2012-07-23 12:57:47 +02:00
|
|
|
filename = ugettext_lazy('Agenda')
|
|
|
|
document_title = ugettext_lazy('Agenda')
|
2012-02-21 13:17:42 +01:00
|
|
|
|
|
|
|
def append_to_pdf(self, story):
|
|
|
|
for item in Item.objects.all():
|
|
|
|
ancestors = item.get_ancestors()
|
|
|
|
if ancestors:
|
2012-07-04 12:50:33 +02:00
|
|
|
space = " " * 6 * ancestors.count()
|
2012-11-24 14:01:21 +01:00
|
|
|
story.append(Paragraph(
|
|
|
|
"%s%s" % (space, item.get_title()),
|
2012-07-04 12:50:33 +02:00
|
|
|
stylesheet['Subitem']))
|
2012-02-21 13:17:42 +01:00
|
|
|
else:
|
2012-04-19 12:46:04 +02:00
|
|
|
story.append(Paragraph(item.get_title(), stylesheet['Item']))
|
2012-03-16 14:31:59 +01:00
|
|
|
|
|
|
|
|
2012-03-18 17:11:58 +01:00
|
|
|
def register_tab(request):
|
2012-07-04 12:50:33 +02:00
|
|
|
"""
|
|
|
|
register the agenda tab.
|
|
|
|
"""
|
2012-07-10 12:11:07 +02:00
|
|
|
selected = request.path.startswith('/agenda/')
|
2012-03-18 17:11:58 +01:00
|
|
|
return Tab(
|
|
|
|
title=_('Agenda'),
|
2012-09-05 00:20:28 +02:00
|
|
|
app='agenda',
|
2012-03-18 17:11:58 +01:00
|
|
|
url=reverse('item_overview'),
|
2012-11-24 14:01:21 +01:00
|
|
|
permission=(request.user.has_perm('agenda.can_see_agenda') or
|
|
|
|
request.user.has_perm('agenda.can_manage_agenda')),
|
|
|
|
selected=selected)
|
2012-06-11 13:43:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_widgets(request):
|
2012-07-04 12:50:33 +02:00
|
|
|
"""
|
|
|
|
return the agenda widget for the projector-tab.
|
|
|
|
"""
|
2012-06-11 13:43:48 +02:00
|
|
|
return [
|
|
|
|
Widget(
|
|
|
|
name='agenda',
|
2012-11-08 09:43:04 +01:00
|
|
|
display_name=_('Agenda'),
|
2012-06-11 13:43:48 +02:00
|
|
|
template='agenda/widget.html',
|
|
|
|
context={
|
|
|
|
'agenda': SLIDE['agenda'],
|
2012-08-15 11:56:43 +02:00
|
|
|
'items': Item.objects.all()},
|
2012-11-21 15:14:38 +01:00
|
|
|
permission_required='projector.can_manage_projector')]
|