2015-01-21 12:58:46 +01:00
|
|
|
from cgi import escape
|
2012-02-21 13:17:42 +01:00
|
|
|
|
2015-05-26 18:21:30 +02:00
|
|
|
from django.contrib.auth import get_user_model
|
2015-12-19 00:29:20 +01:00
|
|
|
from django.db import transaction
|
2013-09-25 10:01:01 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from django.utils.translation import ugettext_lazy
|
|
|
|
from reportlab.platypus import Paragraph
|
2012-02-20 17:46:45 +01:00
|
|
|
|
2015-11-25 10:48:33 +01:00
|
|
|
from openslides.core.config import config
|
2015-05-26 18:21:30 +02:00
|
|
|
from openslides.utils.exceptions import OpenSlidesError
|
2013-09-25 10:01:01 +02:00
|
|
|
from openslides.utils.pdf import stylesheet
|
2015-05-26 18:21:30 +02:00
|
|
|
from openslides.utils.rest_api import (
|
2015-10-24 19:02:43 +02:00
|
|
|
GenericViewSet,
|
|
|
|
ListModelMixin,
|
2015-05-26 18:21:30 +02:00
|
|
|
Response,
|
2015-10-24 19:02:43 +02:00
|
|
|
RetrieveModelMixin,
|
|
|
|
UpdateModelMixin,
|
2015-05-26 18:21:30 +02:00
|
|
|
ValidationError,
|
|
|
|
detail_route,
|
|
|
|
list_route,
|
|
|
|
)
|
2015-06-16 10:37:23 +02:00
|
|
|
from openslides.utils.views import PDFView
|
2014-03-27 20:30:15 +01:00
|
|
|
|
2015-05-26 18:21:30 +02:00
|
|
|
from .models import Item, Speaker
|
2015-01-06 00:14:49 +01:00
|
|
|
from .serializers import ItemSerializer
|
2012-02-20 17:46:45 +01:00
|
|
|
|
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
# Viewsets for the REST API
|
2013-02-16 10:41:22 +01:00
|
|
|
|
2015-10-24 19:02:43 +02:00
|
|
|
class ItemViewSet(ListModelMixin, RetrieveModelMixin, UpdateModelMixin, GenericViewSet):
|
2015-01-06 00:14:49 +01:00
|
|
|
"""
|
2015-07-01 23:18:48 +02:00
|
|
|
API endpoint for agenda items.
|
|
|
|
|
2015-08-31 14:07:24 +02:00
|
|
|
There are the following views: metadata, list, retrieve, create,
|
|
|
|
partial_update, update, destroy, manage_speaker, speak and tree.
|
2015-01-06 00:14:49 +01:00
|
|
|
"""
|
2015-02-12 18:48:14 +01:00
|
|
|
queryset = Item.objects.all()
|
2015-01-06 00:14:49 +01:00
|
|
|
serializer_class = ItemSerializer
|
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
def check_view_permissions(self):
|
2015-01-06 00:14:49 +01:00
|
|
|
"""
|
2015-07-01 23:18:48 +02:00
|
|
|
Returns True if the user has required permissions.
|
2015-01-06 00:14:49 +01:00
|
|
|
"""
|
2015-08-31 14:07:24 +02:00
|
|
|
if self.action in ('metadata', 'list', 'retrieve', 'manage_speaker', 'tree'):
|
2015-07-01 23:18:48 +02:00
|
|
|
result = self.request.user.has_perm('agenda.can_see')
|
|
|
|
# For manage_speaker and tree requests the rest of the check is
|
|
|
|
# done in the specific method. See below.
|
2015-10-24 19:02:43 +02:00
|
|
|
elif self.action in ('partial_update', 'update'):
|
2015-07-01 23:18:48 +02:00
|
|
|
result = (self.request.user.has_perm('agenda.can_see') and
|
2015-10-24 19:02:43 +02:00
|
|
|
self.request.user.has_perm('agenda.can_see_hidden_items') and
|
2015-07-01 23:18:48 +02:00
|
|
|
self.request.user.has_perm('agenda.can_manage'))
|
2016-01-08 23:32:29 +01:00
|
|
|
elif self.action in ('speak', 'sort_speakers', 'numbering'):
|
2015-07-01 23:18:48 +02:00
|
|
|
result = (self.request.user.has_perm('agenda.can_see') and
|
|
|
|
self.request.user.has_perm('agenda.can_manage'))
|
|
|
|
else:
|
|
|
|
result = False
|
|
|
|
return result
|
2015-01-06 00:14:49 +01:00
|
|
|
|
|
|
|
def check_object_permissions(self, request, obj):
|
|
|
|
"""
|
|
|
|
Checks if the requesting user has permission to see also an
|
|
|
|
organizational item if it is one.
|
|
|
|
"""
|
2015-10-24 19:02:43 +02:00
|
|
|
if obj.is_hidden() and not request.user.has_perm('agenda.can_see_hidden_items'):
|
2015-01-06 00:14:49 +01:00
|
|
|
self.permission_denied(request)
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
"""
|
2015-01-24 16:35:50 +01:00
|
|
|
Filters organizational items if the user has no permission to see them.
|
2015-01-06 00:14:49 +01:00
|
|
|
"""
|
2015-02-12 18:48:14 +01:00
|
|
|
queryset = super().get_queryset()
|
2015-10-24 19:02:43 +02:00
|
|
|
if self.request.user.has_perm('agenda.can_see_hidden_items'):
|
|
|
|
return queryset
|
|
|
|
else:
|
|
|
|
return Item.objects.get_only_agenda_items(queryset)
|
2015-03-29 14:42:27 +02:00
|
|
|
|
2015-05-26 18:21:30 +02:00
|
|
|
@detail_route(methods=['POST', 'DELETE'])
|
|
|
|
def manage_speaker(self, request, pk=None):
|
|
|
|
"""
|
|
|
|
Special view endpoint to add users to the list of speakers or remove
|
|
|
|
them. Send POST {'user': <user_id>} to add a new speaker. Omit
|
|
|
|
data to add yourself. Send DELETE {'speaker': <speaker_id>} to remove
|
|
|
|
someone from the list of speakers. Omit data to remove yourself.
|
|
|
|
|
|
|
|
Checks also whether the requesting user can do this. He needs at
|
|
|
|
least the permissions 'agenda.can_see' (see
|
2015-07-01 23:18:48 +02:00
|
|
|
self.check_view_permissions()). In case of adding himself the
|
|
|
|
permission 'agenda.can_be_speaker' is required. In case of adding
|
|
|
|
someone else the permission 'agenda.can_manage' is required. In
|
|
|
|
case of removing someone else 'agenda.can_manage' is required. In
|
|
|
|
case of removing himself no other permission is required.
|
2015-05-26 18:21:30 +02:00
|
|
|
"""
|
|
|
|
# Retrieve item.
|
|
|
|
item = self.get_object()
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
# Retrieve user_id
|
|
|
|
user_id = request.data.get('user')
|
|
|
|
|
|
|
|
# Check permissions and other conditions. Get user instance.
|
|
|
|
if user_id is None:
|
|
|
|
# Add oneself
|
|
|
|
if not self.request.user.has_perm('agenda.can_be_speaker'):
|
|
|
|
self.permission_denied(request)
|
|
|
|
if item.speaker_list_closed:
|
|
|
|
raise ValidationError({'detail': _('The list of speakers is closed.')})
|
|
|
|
user = self.request.user
|
|
|
|
else:
|
|
|
|
# Add someone else.
|
|
|
|
if not self.request.user.has_perm('agenda.can_manage'):
|
|
|
|
self.permission_denied(request)
|
|
|
|
try:
|
|
|
|
user = get_user_model().objects.get(pk=int(user_id))
|
|
|
|
except (ValueError, get_user_model().DoesNotExist):
|
|
|
|
raise ValidationError({'detail': _('User does not exist.')})
|
|
|
|
|
|
|
|
# Try to add the user. This ensurse that a user is not twice in the
|
|
|
|
# list of coming speakers.
|
|
|
|
try:
|
|
|
|
Speaker.objects.add(user, item)
|
|
|
|
except OpenSlidesError as e:
|
2015-08-31 14:07:24 +02:00
|
|
|
raise ValidationError({'detail': str(e)})
|
2015-05-27 15:42:32 +02:00
|
|
|
message = _('User %s was successfully added to the list of speakers.') % user
|
2015-05-26 18:21:30 +02:00
|
|
|
|
|
|
|
else:
|
|
|
|
# request.method == 'DELETE'
|
|
|
|
# Retrieve speaker_id
|
|
|
|
speaker_id = request.data.get('speaker')
|
|
|
|
|
|
|
|
# Check permissions and other conditions. Get speaker instance.
|
|
|
|
if speaker_id is None:
|
|
|
|
# Remove oneself
|
|
|
|
queryset = Speaker.objects.filter(
|
|
|
|
item=item, user=self.request.user).exclude(weight=None)
|
|
|
|
try:
|
|
|
|
# We assume that there aren't multiple entries because this
|
|
|
|
# is forbidden by the Manager's add method. We assume that
|
|
|
|
# there is only one speaker instance or none.
|
|
|
|
speaker = queryset.get()
|
|
|
|
except Speaker.DoesNotExist:
|
|
|
|
raise ValidationError({'detail': _('You are not on the list of speakers.')})
|
|
|
|
else:
|
|
|
|
# Remove someone else.
|
|
|
|
if not self.request.user.has_perm('agenda.can_manage'):
|
|
|
|
self.permission_denied(request)
|
|
|
|
try:
|
2015-05-27 15:42:32 +02:00
|
|
|
speaker = Speaker.objects.get(pk=int(speaker_id))
|
|
|
|
except (ValueError, Speaker.DoesNotExist):
|
2015-05-26 18:21:30 +02:00
|
|
|
raise ValidationError({'detail': _('Speaker does not exist.')})
|
|
|
|
|
|
|
|
# Delete the speaker.
|
|
|
|
speaker.delete()
|
2015-05-27 15:42:32 +02:00
|
|
|
message = _('Speaker %s was successfully removed from the list of speakers.') % speaker
|
|
|
|
|
|
|
|
# Initiate response.
|
|
|
|
return Response({'detail': message})
|
|
|
|
|
|
|
|
@detail_route(methods=['PUT', 'DELETE'])
|
|
|
|
def speak(self, request, pk=None):
|
|
|
|
"""
|
2015-09-04 18:24:41 +02:00
|
|
|
Special view endpoint to begin and end speech of speakers. Send PUT
|
|
|
|
{'speaker': <speaker_id>} to begin speech. Omit data to begin speech of
|
|
|
|
the next speaker. Send DELETE to end speech of current speaker.
|
2015-05-27 15:42:32 +02:00
|
|
|
"""
|
|
|
|
# Retrieve item.
|
|
|
|
item = self.get_object()
|
|
|
|
|
|
|
|
if request.method == 'PUT':
|
|
|
|
# Retrieve speaker_id
|
|
|
|
speaker_id = request.data.get('speaker')
|
|
|
|
if speaker_id is None:
|
|
|
|
speaker = item.get_next_speaker()
|
|
|
|
if speaker is None:
|
|
|
|
raise ValidationError({'detail': _('The list of speakers is empty.')})
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
speaker = Speaker.objects.get(pk=int(speaker_id))
|
|
|
|
except (ValueError, Speaker.DoesNotExist):
|
|
|
|
raise ValidationError({'detail': _('Speaker does not exist.')})
|
2015-09-04 18:24:41 +02:00
|
|
|
speaker.begin_speech()
|
2015-05-27 15:42:32 +02:00
|
|
|
message = _('User is now speaking.')
|
|
|
|
|
|
|
|
else:
|
|
|
|
# request.method == 'DELETE'
|
|
|
|
try:
|
|
|
|
# We assume that there aren't multiple entries because this
|
2015-09-04 18:24:41 +02:00
|
|
|
# is forbidden by the Model's begin_speech method. We assume that
|
2015-05-27 15:42:32 +02:00
|
|
|
# there is only one speaker instance or none.
|
|
|
|
current_speaker = Speaker.objects.filter(item=item, end_time=None).exclude(begin_time=None).get()
|
|
|
|
except Speaker.DoesNotExist:
|
|
|
|
raise ValidationError(
|
|
|
|
{'detail': _('There is no one speaking at the moment according to %(item)s.') % {'item': item}})
|
2015-09-04 18:24:41 +02:00
|
|
|
current_speaker.end_speech()
|
|
|
|
message = _('The speech is finished now.')
|
2015-05-26 18:21:30 +02:00
|
|
|
|
|
|
|
# Initiate response.
|
|
|
|
return Response({'detail': message})
|
|
|
|
|
2016-01-08 23:32:29 +01:00
|
|
|
@detail_route(methods=['POST'])
|
|
|
|
def sort_speakers(self, request, pk=None):
|
|
|
|
"""
|
|
|
|
Special view endpoint to sort the list of speakers.
|
|
|
|
|
|
|
|
Expects a list of IDs of the speakers.
|
|
|
|
"""
|
|
|
|
# Retrieve item.
|
|
|
|
item = self.get_object()
|
|
|
|
|
|
|
|
# Check data
|
|
|
|
speaker_ids = request.data.get('speakers')
|
|
|
|
if not isinstance(speaker_ids, list):
|
|
|
|
raise ValidationError(
|
2015-12-19 00:29:20 +01:00
|
|
|
{'detail': _('Invalid data.')})
|
2016-01-08 23:32:29 +01:00
|
|
|
|
|
|
|
# Get all speakers
|
|
|
|
speakers = {}
|
|
|
|
for speaker in item.speakers.filter(begin_time=None):
|
|
|
|
speakers[speaker.pk] = speaker
|
|
|
|
|
2015-12-19 00:29:20 +01:00
|
|
|
# Check and sort speakers
|
|
|
|
valid_speakers = []
|
2016-01-08 23:32:29 +01:00
|
|
|
for speaker_id in speaker_ids:
|
2015-12-19 00:29:20 +01:00
|
|
|
if not isinstance(speaker_id, int) or speakers.get(speaker_id) is None:
|
2016-01-08 23:32:29 +01:00
|
|
|
raise ValidationError(
|
|
|
|
{'detail': _('Invalid data.')})
|
2015-12-19 00:29:20 +01:00
|
|
|
valid_speakers.append(speakers[speaker_id])
|
|
|
|
weight = 0
|
|
|
|
with transaction.atomic():
|
|
|
|
for speaker in valid_speakers:
|
|
|
|
speaker.weight = weight
|
|
|
|
speaker.save()
|
|
|
|
weight += 1
|
2016-01-08 23:32:29 +01:00
|
|
|
|
|
|
|
# Initiate response.
|
|
|
|
return Response({'detail': _('List of speakers successfully sorted.')})
|
|
|
|
|
2015-03-29 14:42:27 +02:00
|
|
|
@list_route(methods=['get', 'put'])
|
|
|
|
def tree(self, request):
|
|
|
|
"""
|
|
|
|
Returns or sets the agenda tree.
|
|
|
|
"""
|
|
|
|
if request.method == 'PUT':
|
|
|
|
if not (request.user.has_perm('agenda.can_manage') and
|
2015-10-24 19:02:43 +02:00
|
|
|
request.user.has_perm('agenda.can_see_hidden_items')):
|
2015-03-29 14:42:27 +02:00
|
|
|
self.permission_denied(request)
|
2015-06-25 09:53:55 +02:00
|
|
|
try:
|
2015-09-14 22:18:55 +02:00
|
|
|
tree = request.data['tree']
|
|
|
|
except KeyError as error:
|
|
|
|
response = Response({'detail': 'Agenda tree is missing.'}, status=400)
|
2015-06-25 09:53:55 +02:00
|
|
|
else:
|
2015-09-14 22:18:55 +02:00
|
|
|
try:
|
|
|
|
Item.objects.set_tree(tree)
|
|
|
|
except ValueError as error:
|
|
|
|
response = Response({'detail': str(error)}, status=400)
|
|
|
|
else:
|
|
|
|
response = Response({'detail': 'Agenda tree successfully updated.'})
|
|
|
|
else:
|
|
|
|
# request.method == 'GET'
|
|
|
|
response = Response(Item.objects.get_tree())
|
|
|
|
return response
|
2015-07-01 23:18:48 +02:00
|
|
|
|
2015-11-23 17:21:32 +01:00
|
|
|
@list_route(methods=['post'])
|
|
|
|
def numbering(self, request):
|
|
|
|
"""
|
|
|
|
Auto numbering of the agenda according to the config. Manually added
|
|
|
|
item numbers will be overwritten.
|
|
|
|
"""
|
2015-11-25 10:48:33 +01:00
|
|
|
Item.objects.number_all(numeral_system=config['agenda_numeral_system'])
|
2015-11-23 17:21:32 +01:00
|
|
|
return Response({'detail': _('The agenda has been numbered.')})
|
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
|
|
|
|
# Views to generate PDFs
|
|
|
|
|
|
|
|
class AgendaPDF(PDFView):
|
|
|
|
"""
|
|
|
|
Create a full agenda-PDF.
|
|
|
|
"""
|
|
|
|
required_permission = 'agenda.can_see'
|
|
|
|
filename = ugettext_lazy('Agenda')
|
|
|
|
document_title = ugettext_lazy('Agenda')
|
|
|
|
|
|
|
|
def append_to_pdf(self, story):
|
|
|
|
tree = Item.objects.get_tree(only_agenda_items=True, include_content=True)
|
|
|
|
|
|
|
|
def walk_tree(tree, ancestors=0):
|
|
|
|
"""
|
|
|
|
Generator that yields a two-element-tuple. The first element is an
|
|
|
|
agenda-item and the second a number for steps to the root element.
|
|
|
|
"""
|
|
|
|
for element in tree:
|
|
|
|
yield element['item'], ancestors
|
|
|
|
yield from walk_tree(element['children'], ancestors + 1)
|
|
|
|
|
|
|
|
for item, ancestors in walk_tree(tree):
|
|
|
|
if ancestors:
|
|
|
|
space = " " * 6 * ancestors
|
|
|
|
story.append(Paragraph(
|
2015-10-24 19:02:43 +02:00
|
|
|
"%s%s" % (space, escape(item.title)),
|
2015-07-01 23:18:48 +02:00
|
|
|
stylesheet['Subitem']))
|
|
|
|
else:
|
2015-10-24 19:02:43 +02:00
|
|
|
story.append(Paragraph(escape(item.title), stylesheet['Item']))
|