2015-04-30 19:13:28 +02:00
|
|
|
from django.http import Http404
|
2015-06-16 10:37:23 +02:00
|
|
|
from django.shortcuts import get_object_or_404
|
2013-06-14 20:24:48 +02:00
|
|
|
from django.utils.text import slugify
|
2013-09-25 10:01:01 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
2015-04-30 19:13:28 +02:00
|
|
|
from django.utils.translation import ugettext_noop
|
2015-03-09 15:40:54 +01:00
|
|
|
from reportlab.platypus import SimpleDocTemplate
|
2015-04-30 19:13:28 +02:00
|
|
|
from rest_framework import status
|
2013-04-24 15:07:39 +02:00
|
|
|
|
2015-06-21 23:23:40 +02:00
|
|
|
from openslides.agenda.models import Item
|
2015-04-30 19:13:28 +02:00
|
|
|
from openslides.config.api import config
|
2015-06-16 10:37:23 +02:00
|
|
|
from openslides.utils.rest_api import (
|
|
|
|
ModelViewSet,
|
|
|
|
Response,
|
|
|
|
ValidationError,
|
|
|
|
detail_route,
|
|
|
|
)
|
|
|
|
from openslides.utils.views import PDFView, SingleObjectMixin
|
|
|
|
|
|
|
|
from .models import Category, Motion, MotionPoll, MotionVersion, Workflow
|
2013-09-25 10:01:01 +02:00
|
|
|
from .pdf import motion_poll_to_pdf, motion_to_pdf, motions_to_pdf
|
2015-06-16 10:37:23 +02:00
|
|
|
from .serializers import (
|
|
|
|
CategorySerializer,
|
|
|
|
MotionSerializer,
|
|
|
|
WorkflowSerializer,
|
|
|
|
)
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2013-02-02 00:37:43 +01:00
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class MotionViewSet(ModelViewSet):
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
|
|
|
API endpoint to list, retrieve, create, update and destroy motions.
|
|
|
|
"""
|
|
|
|
queryset = Motion.objects.all()
|
|
|
|
serializer_class = MotionSerializer
|
|
|
|
|
|
|
|
def check_permissions(self, request):
|
|
|
|
"""
|
|
|
|
Calls self.permission_denied() if the requesting user has not the
|
2015-04-30 19:13:28 +02:00
|
|
|
permission to see motions and in case of destroy requests the
|
|
|
|
permission to manage motions.
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
2015-03-26 05:36:10 +01:00
|
|
|
if (not request.user.has_perm('motions.can_see') or
|
2015-04-30 19:13:28 +02:00
|
|
|
(self.action == 'destroy' and not request.user.has_perm('motions.can_manage'))):
|
2015-01-24 16:35:50 +01:00
|
|
|
self.permission_denied(request)
|
|
|
|
|
2015-04-30 19:13:28 +02:00
|
|
|
def create(self, request, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Customized view endpoint to create a new motion.
|
|
|
|
|
|
|
|
Checks also whether the requesting user can submit a new motion. He
|
|
|
|
needs at least the permissions 'motions.can_see' (see
|
|
|
|
self.check_permission()) and 'motions.can_create'. If the
|
|
|
|
submitting of new motions by non-staff users is stopped via config
|
2015-06-16 18:12:59 +02:00
|
|
|
variable 'motions_stop_submitting', the requesting user needs also
|
2015-04-30 19:13:28 +02:00
|
|
|
to have the permission 'motions.can_manage'.
|
|
|
|
"""
|
|
|
|
# Check permissions.
|
|
|
|
if (not request.user.has_perm('motions.can_create') or
|
2015-06-16 18:12:59 +02:00
|
|
|
(not config['motions_stop_submitting'] and
|
2015-04-30 19:13:28 +02:00
|
|
|
not request.user.has_perm('motions.can_manage'))):
|
|
|
|
self.permission_denied(request)
|
|
|
|
|
|
|
|
# Check permission to send submitter and supporter data.
|
|
|
|
if (not request.user.has_perm('motions.can_manage') and
|
|
|
|
(request.data.getlist('submitters') or request.data.getlist('supporters'))):
|
|
|
|
# Non-staff users are not allowed to send submitter or supporter data.
|
|
|
|
self.permission_denied(request)
|
|
|
|
|
|
|
|
# Validate data and create motion.
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
motion = serializer.save(request_user=request.user)
|
|
|
|
|
|
|
|
# Write the log message and initiate response.
|
|
|
|
motion.write_log([ugettext_noop('Motion created')], request.user)
|
|
|
|
headers = self.get_success_headers(serializer.data)
|
|
|
|
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
|
|
|
|
|
|
|
def update(self, request, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Customized view endpoint to update a motion.
|
|
|
|
|
|
|
|
Checks also whether the requesting user can update the motion. He
|
|
|
|
needs at least the permissions 'motions.can_see' (see
|
|
|
|
self.check_permission()). Also the instance method
|
|
|
|
get_allowed_actions() is evaluated.
|
|
|
|
"""
|
|
|
|
# Get motion.
|
|
|
|
motion = self.get_object()
|
|
|
|
|
|
|
|
# Check permissions.
|
|
|
|
if not motion.get_allowed_actions(request.user)['update']:
|
|
|
|
self.permission_denied(request)
|
|
|
|
|
|
|
|
# Check permission to send submitter and supporter data.
|
|
|
|
if (not request.user.has_perm('motions.can_manage') and
|
|
|
|
(request.data.getlist('submitters') or request.data.getlist('supporters'))):
|
|
|
|
# Non-staff users are not allowed to send submitter or supporter data.
|
|
|
|
self.permission_denied(request)
|
|
|
|
|
|
|
|
# Validate data and update motion.
|
|
|
|
serializer = self.get_serializer(
|
|
|
|
motion,
|
|
|
|
data=request.data,
|
|
|
|
partial=kwargs.get('partial', False))
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
updated_motion = serializer.save()
|
|
|
|
|
|
|
|
# Write the log message, check removal of supporters and initiate response.
|
|
|
|
# TODO: Log if a version was updated.
|
|
|
|
updated_motion.write_log([ugettext_noop('Motion updated')], request.user)
|
2015-06-16 18:12:59 +02:00
|
|
|
if (config['motions_remove_supporters'] and updated_motion.state.allow_support and
|
2015-04-30 19:13:28 +02:00
|
|
|
not request.user.has_perm('motions.can_manage')):
|
|
|
|
updated_motion.supporters.clear()
|
|
|
|
updated_motion.write_log([ugettext_noop('All supporters removed')], request.user)
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
|
|
@detail_route(methods=['put', 'delete'])
|
|
|
|
def manage_version(self, request, pk=None):
|
|
|
|
"""
|
|
|
|
Special view endpoint to permit and delete a version of a motion.
|
|
|
|
|
|
|
|
Send PUT {'version_number': <number>} to permit and DELETE
|
|
|
|
{'version_number': <number>} to delete a version. Deleting the
|
|
|
|
active version is not allowed. Only managers can use this view.
|
|
|
|
"""
|
|
|
|
# Check permission.
|
|
|
|
if not request.user.has_perm('motions.can_manage'):
|
|
|
|
self.permission_denied(request)
|
|
|
|
|
|
|
|
# Retrieve motion and version.
|
|
|
|
motion = self.get_object()
|
|
|
|
version_number = request.data.get('version_number')
|
|
|
|
try:
|
|
|
|
version = motion.versions.get(version_number=version_number)
|
|
|
|
except MotionVersion.DoesNotExist:
|
|
|
|
raise Http404('Version %s not found.' % version_number)
|
|
|
|
|
|
|
|
# Permit or delete version.
|
|
|
|
if request.method == 'PUT':
|
|
|
|
# Permit version.
|
|
|
|
motion.active_version = version
|
|
|
|
motion.save(update_fields=['active_version'])
|
|
|
|
motion.write_log(
|
|
|
|
message_list=[ugettext_noop('Version'),
|
|
|
|
' %d ' % version.version_number,
|
|
|
|
ugettext_noop('permitted')],
|
|
|
|
person=self.request.user)
|
|
|
|
message = _('Version %d permitted successfully.') % version.version_number
|
|
|
|
else:
|
|
|
|
# Delete version.
|
|
|
|
# request.method == 'DELETE'
|
|
|
|
if version == motion.active_version:
|
|
|
|
raise ValidationError({'detail': _('You can not delete the active version of a motion.')})
|
|
|
|
version.delete()
|
|
|
|
motion.write_log(
|
|
|
|
message_list=[ugettext_noop('Version'),
|
|
|
|
' %d ' % version.version_number,
|
|
|
|
ugettext_noop('deleted')],
|
|
|
|
person=self.request.user)
|
|
|
|
message = _('Version %d deleted successfully.') % version.version_number
|
|
|
|
|
|
|
|
# Initiate response.
|
|
|
|
return Response({'detail': message})
|
|
|
|
|
|
|
|
@detail_route(methods=['post', 'delete'])
|
|
|
|
def support(self, request, pk=None):
|
|
|
|
"""
|
|
|
|
Special view endpoint to support a motion or withdraw support
|
|
|
|
(unsupport).
|
|
|
|
|
|
|
|
Send POST to support and DELETE to unsupport.
|
|
|
|
|
|
|
|
Checks also whether the requesting user can do this. He needs at
|
|
|
|
least the permissions 'motions.can_see' (see
|
|
|
|
self.check_permission()). Also the the permission
|
|
|
|
'motions.can_support' is required and the instance method
|
|
|
|
get_allowed_actions() is evaluated.
|
|
|
|
"""
|
|
|
|
# Check permission.
|
|
|
|
if not request.user.has_perm('motions.can_support'):
|
|
|
|
self.permission_denied(request)
|
|
|
|
|
|
|
|
# Retrieve motion and allowed actions.
|
|
|
|
motion = self.get_object()
|
|
|
|
allowed_actions = motion.get_allowed_actions(request.user)
|
|
|
|
|
|
|
|
# Support or unsupport motion.
|
|
|
|
if request.method == 'POST':
|
|
|
|
# Support motion.
|
|
|
|
if not allowed_actions['support']:
|
|
|
|
raise ValidationError({'detail': _('You can not support this motion.')})
|
|
|
|
motion.supporters.add(request.user)
|
|
|
|
motion.write_log([ugettext_noop('Motion supported')], request.user)
|
|
|
|
message = _('You have supported this motion successfully.')
|
|
|
|
else:
|
|
|
|
# Unsupport motion.
|
|
|
|
# request.method == 'DELETE'
|
|
|
|
if not allowed_actions['unsupport']:
|
|
|
|
raise ValidationError({'detail': _('You can not unsupport this motion.')})
|
|
|
|
motion.supporters.remove(request.user)
|
|
|
|
motion.write_log([ugettext_noop('Motion unsupported')], request.user)
|
|
|
|
message = _('You have unsupported this motion successfully.')
|
|
|
|
|
|
|
|
# Initiate response.
|
|
|
|
return Response({'detail': message})
|
|
|
|
|
|
|
|
@detail_route(methods=['put'])
|
|
|
|
def set_state(self, request, pk=None):
|
|
|
|
"""
|
|
|
|
Special view endpoint to set and reset a state of a motion.
|
|
|
|
|
|
|
|
Send PUT {'state': <state_id>} to set and just PUT {} to reset the
|
|
|
|
state. Only managers can use this view.
|
|
|
|
"""
|
|
|
|
# Check permission.
|
|
|
|
if not request.user.has_perm('motions.can_manage'):
|
|
|
|
self.permission_denied(request)
|
|
|
|
|
|
|
|
# Retrieve motion and state.
|
|
|
|
motion = self.get_object()
|
|
|
|
state = request.data.get('state')
|
|
|
|
|
|
|
|
# Set or reset state.
|
|
|
|
if state is not None:
|
|
|
|
# Check data and set state.
|
|
|
|
try:
|
|
|
|
state_id = int(state)
|
|
|
|
except ValueError:
|
|
|
|
raise ValidationError({'detail': _('Invalid data. State must be an integer.')})
|
|
|
|
if state_id not in [item.id for item in motion.state.next_states.all()]:
|
|
|
|
raise ValidationError(
|
|
|
|
{'detail': _('You can not set the state to %(state_id)d.') % {'state_id': state_id}})
|
|
|
|
motion.set_state(state_id)
|
|
|
|
else:
|
|
|
|
# Reset state.
|
|
|
|
motion.reset_state()
|
|
|
|
|
|
|
|
# Save motion.
|
|
|
|
motion.save(update_fields=['state', 'identifier'])
|
|
|
|
message = _('The state of the motion was set to %s.') % motion.state.name
|
|
|
|
|
|
|
|
# Write the log message and initiate response.
|
|
|
|
motion.write_log(
|
|
|
|
message_list=[ugettext_noop('State set to'), ' ', motion.state.name],
|
|
|
|
person=request.user)
|
|
|
|
return Response({'detail': message})
|
|
|
|
|
2015-06-21 23:23:40 +02:00
|
|
|
@detail_route(methods=['post'])
|
|
|
|
def create_agenda_item(self, request, pk=None):
|
|
|
|
"""
|
|
|
|
Speacial view endpoint to create an agenda item that is related to
|
|
|
|
this motion.
|
|
|
|
"""
|
|
|
|
# Check permission.
|
|
|
|
if not request.user.has_perm('motions.can_manage'):
|
|
|
|
self.permission_denied(request)
|
|
|
|
|
|
|
|
# Retrieve motion.
|
|
|
|
motion = self.get_object()
|
|
|
|
|
|
|
|
# Create agenda item.
|
|
|
|
Item.objects.create(content_object=motion)
|
|
|
|
|
|
|
|
# Write the log message and initiate response.
|
|
|
|
motion.write_log(
|
|
|
|
message_list=[ugettext_noop('Agenda item created')],
|
|
|
|
person=request.user)
|
|
|
|
return Response({'detail': _('Agenda item successfully created.')})
|
|
|
|
|
2015-01-24 16:35:50 +01:00
|
|
|
|
2015-06-16 10:37:23 +02:00
|
|
|
class PollPDFView(PDFView):
|
2013-04-21 19:12:50 +02:00
|
|
|
"""
|
2015-06-16 10:37:23 +02:00
|
|
|
Generates a ballotpaper.
|
2013-04-21 19:12:50 +02:00
|
|
|
"""
|
|
|
|
|
2015-03-26 05:36:10 +01:00
|
|
|
required_permission = 'motions.can_manage'
|
2015-06-16 10:37:23 +02:00
|
|
|
top_space = 0
|
2013-02-01 12:51:54 +01:00
|
|
|
|
|
|
|
def get_object(self):
|
2013-04-21 19:12:50 +02:00
|
|
|
"""
|
|
|
|
Return a MotionPoll object.
|
2013-02-05 18:46:46 +01:00
|
|
|
|
|
|
|
Use the motion id and the poll_number from the url kwargs to get the
|
|
|
|
object.
|
|
|
|
"""
|
2014-12-22 18:09:05 +01:00
|
|
|
try:
|
|
|
|
obj = self._object
|
|
|
|
except AttributeError:
|
|
|
|
queryset = MotionPoll.objects.filter(
|
|
|
|
motion=self.kwargs['pk'],
|
|
|
|
poll_number=self.kwargs['poll_number'])
|
|
|
|
obj = get_object_or_404(queryset)
|
|
|
|
self._object = obj
|
|
|
|
return obj
|
2013-02-01 12:51:54 +01:00
|
|
|
|
2013-04-21 19:12:50 +02:00
|
|
|
def get_filename(self):
|
|
|
|
"""
|
|
|
|
Return the filename for the PDF.
|
|
|
|
"""
|
2014-12-22 18:09:05 +01:00
|
|
|
return u'%s%s_%s' % (_("Motion"), str(self.get_object().poll_number), _("Poll"))
|
2013-04-21 19:12:50 +02:00
|
|
|
|
2013-04-24 15:07:39 +02:00
|
|
|
def get_template(self, buffer):
|
|
|
|
return SimpleDocTemplate(
|
|
|
|
buffer, topMargin=-6, bottomMargin=-6, leftMargin=0, rightMargin=0,
|
|
|
|
showBoundary=False)
|
|
|
|
|
|
|
|
def build_document(self, pdf_document, story):
|
|
|
|
pdf_document.build(story)
|
|
|
|
|
2013-04-21 19:12:50 +02:00
|
|
|
def append_to_pdf(self, pdf):
|
|
|
|
"""
|
|
|
|
Append PDF objects.
|
|
|
|
"""
|
2014-12-22 18:09:05 +01:00
|
|
|
motion_poll_to_pdf(pdf, self.get_object())
|
2013-04-21 19:12:50 +02:00
|
|
|
|
|
|
|
|
2013-02-03 18:18:29 +01:00
|
|
|
class MotionPDFView(SingleObjectMixin, PDFView):
|
2013-06-01 12:36:42 +02:00
|
|
|
"""
|
2014-12-20 20:13:30 +01:00
|
|
|
Create the PDF for one or for all motions.
|
2013-02-05 18:46:46 +01:00
|
|
|
|
|
|
|
If self.print_all_motions is True, the view returns a PDF with all motions.
|
|
|
|
|
|
|
|
If self.print_all_motions is False, the view returns a PDF with only one
|
2013-06-01 12:36:42 +02:00
|
|
|
motion.
|
|
|
|
"""
|
2013-02-03 18:18:29 +01:00
|
|
|
model = Motion
|
|
|
|
top_space = 0
|
|
|
|
print_all_motions = False
|
|
|
|
|
2014-12-20 20:13:30 +01:00
|
|
|
def check_permission(self, request, *args, **kwargs):
|
2013-06-01 12:36:42 +02:00
|
|
|
"""
|
2014-12-20 20:13:30 +01:00
|
|
|
Checks if the requesting user has the permission to see the motion as
|
|
|
|
PDF.
|
2013-06-01 12:36:42 +02:00
|
|
|
"""
|
2014-12-20 20:13:30 +01:00
|
|
|
if self.print_all_motions:
|
2015-03-26 05:36:10 +01:00
|
|
|
is_allowed = request.user.has_perm('motions.can_see')
|
2014-12-20 20:13:30 +01:00
|
|
|
else:
|
|
|
|
is_allowed = self.get_object().get_allowed_actions(request.user)['see']
|
|
|
|
return is_allowed
|
|
|
|
|
2014-12-22 18:09:05 +01:00
|
|
|
def get_object(self, *args, **kwargs):
|
|
|
|
if self.print_all_motions:
|
|
|
|
obj = None
|
|
|
|
else:
|
2015-01-05 17:14:29 +01:00
|
|
|
obj = super().get_object(*args, **kwargs)
|
2014-12-22 18:09:05 +01:00
|
|
|
return obj
|
2013-02-03 18:18:29 +01:00
|
|
|
|
|
|
|
def get_filename(self):
|
2013-06-01 12:36:42 +02:00
|
|
|
"""
|
|
|
|
Return the filename for the PDF.
|
|
|
|
"""
|
2013-02-03 18:18:29 +01:00
|
|
|
if self.print_all_motions:
|
|
|
|
return _("Motions")
|
|
|
|
else:
|
2014-12-22 18:09:05 +01:00
|
|
|
if self.get_object().identifier:
|
|
|
|
suffix = self.get_object().identifier.replace(' ', '')
|
2013-06-14 20:24:48 +02:00
|
|
|
else:
|
2014-12-22 18:09:05 +01:00
|
|
|
suffix = self.get_object().title.replace(' ', '_')
|
2013-06-14 20:24:48 +02:00
|
|
|
suffix = slugify(suffix)
|
|
|
|
return '%s-%s' % (_("Motion"), suffix)
|
2013-02-03 18:18:29 +01:00
|
|
|
|
|
|
|
def append_to_pdf(self, pdf):
|
2013-06-01 12:36:42 +02:00
|
|
|
"""
|
|
|
|
Append PDF objects.
|
|
|
|
"""
|
2013-02-03 18:18:29 +01:00
|
|
|
if self.print_all_motions:
|
2014-12-20 20:13:30 +01:00
|
|
|
motions = []
|
|
|
|
for motion in Motion.objects.all():
|
|
|
|
if (not motion.state.required_permission_to_see or
|
|
|
|
self.request.user.has_perm(motion.state.required_permission_to_see)):
|
|
|
|
motions.append(motion)
|
|
|
|
motions_to_pdf(pdf, motions)
|
2013-02-03 18:18:29 +01:00
|
|
|
else:
|
2014-12-22 18:09:05 +01:00
|
|
|
motion_to_pdf(pdf, self.get_object())
|
2013-02-03 18:18:29 +01:00
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class CategoryViewSet(ModelViewSet):
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
|
|
|
API endpoint to list, retrieve, create, update and destroy categories.
|
|
|
|
"""
|
|
|
|
queryset = Category.objects.all()
|
|
|
|
serializer_class = CategorySerializer
|
|
|
|
|
|
|
|
def check_permissions(self, request):
|
|
|
|
"""
|
|
|
|
Calls self.permission_denied() if the requesting user has not the
|
|
|
|
permission to see motions and in case of create, update or destroy
|
|
|
|
requests the permission to manage motions.
|
|
|
|
"""
|
2015-03-26 05:36:10 +01:00
|
|
|
if (not request.user.has_perm('motions.can_see') or
|
2015-01-24 16:35:50 +01:00
|
|
|
(self.action in ('create', 'update', 'destroy') and not
|
2015-03-26 05:36:10 +01:00
|
|
|
request.user.has_perm('motions.can_manage'))):
|
2015-01-24 16:35:50 +01:00
|
|
|
self.permission_denied(request)
|
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class WorkflowViewSet(ModelViewSet):
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
|
|
|
API endpoint to list, retrieve, create, update and destroy workflows.
|
|
|
|
"""
|
|
|
|
queryset = Workflow.objects.all()
|
|
|
|
serializer_class = WorkflowSerializer
|
|
|
|
|
|
|
|
def check_permissions(self, request):
|
|
|
|
"""
|
|
|
|
Calls self.permission_denied() if the requesting user has not the
|
|
|
|
permission to see motions and in case of create, update or destroy
|
|
|
|
requests the permission to manage motions.
|
|
|
|
"""
|
2015-03-26 05:36:10 +01:00
|
|
|
if (not request.user.has_perm('motions.can_see') or
|
2015-01-24 16:35:50 +01:00
|
|
|
(self.action in ('create', 'update', 'destroy') and not
|
2015-03-26 05:36:10 +01:00
|
|
|
request.user.has_perm('motions.can_manage'))):
|
2015-01-24 16:35:50 +01:00
|
|
|
self.permission_denied(request)
|