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 _
|
2014-10-16 18:04:30 +02:00
|
|
|
from django.shortcuts import get_object_or_404
|
2015-03-09 15:40:54 +01:00
|
|
|
from reportlab.platypus import SimpleDocTemplate
|
2013-04-24 15:07:39 +02:00
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
from openslides.utils.rest_api import ModelViewSet
|
2015-03-09 15:40:54 +01:00
|
|
|
from openslides.utils.views import (PDFView, SingleObjectMixin)
|
|
|
|
|
|
|
|
from .models import (Category, Motion, MotionPoll, Workflow)
|
2013-09-25 10:01:01 +02:00
|
|
|
from .pdf import motion_poll_to_pdf, motion_to_pdf, motions_to_pdf
|
2015-01-24 16:35:50 +01: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
|
|
|
|
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
|
|
|
# TODO: Use motions.can_create permission and
|
|
|
|
# motions.can_support permission to create and update some
|
2015-01-24 16:35:50 +01:00
|
|
|
# objects but restricted concerning the requesting user.
|
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)
|
|
|
|
|
|
|
|
|
2013-02-01 13:24:44 +01:00
|
|
|
class PollMixin(object):
|
2013-04-21 19:12:50 +02:00
|
|
|
"""
|
|
|
|
Mixin for the PollUpdateView and the PollDeleteView.
|
|
|
|
"""
|
|
|
|
|
2015-03-26 05:36:10 +01:00
|
|
|
required_permission = 'motions.can_manage'
|
2013-02-01 12:51:54 +01:00
|
|
|
success_url_name = 'motion_detail'
|
|
|
|
|
|
|
|
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-02-01 13:24:44 +01:00
|
|
|
def get_url_name_args(self):
|
2013-04-21 19:12:50 +02:00
|
|
|
"""
|
|
|
|
Return the arguments to create the url to the success_url.
|
|
|
|
"""
|
2014-12-22 18:09:05 +01:00
|
|
|
return [self.get_object().motion.pk]
|
2013-02-01 13:24:44 +01:00
|
|
|
|
|
|
|
|
2013-04-21 19:12:50 +02:00
|
|
|
class PollPDFView(PollMixin, PDFView):
|
|
|
|
"""
|
|
|
|
Generates a ballotpaper.
|
|
|
|
"""
|
|
|
|
|
2015-03-26 05:36:10 +01:00
|
|
|
required_permission = 'motions.can_manage'
|
2013-04-24 15:07:39 +02:00
|
|
|
top_space = 0
|
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)
|