2016-12-03 21:59:13 +01:00
|
|
|
import re
|
2017-08-24 12:26:55 +02:00
|
|
|
from typing import Optional # noqa
|
2016-09-13 11:54:30 +02:00
|
|
|
|
2017-02-06 12:11:50 +01:00
|
|
|
from django.conf import settings
|
2017-06-18 20:20:44 +02:00
|
|
|
from django.core.exceptions import ValidationError as DjangoValidationError
|
2016-07-29 23:33:47 +02:00
|
|
|
from django.db import IntegrityError, transaction
|
2015-04-30 19:13:28 +02:00
|
|
|
from django.http import Http404
|
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
|
|
|
|
from rest_framework import status
|
2013-04-24 15:07:39 +02:00
|
|
|
|
2016-10-14 21:48:02 +02:00
|
|
|
from ..core.config import config
|
2017-01-26 15:34:24 +01:00
|
|
|
from ..utils.auth import has_perm
|
2016-10-14 21:48:02 +02:00
|
|
|
from ..utils.autoupdate import inform_changed_data
|
2017-02-27 15:37:01 +01:00
|
|
|
from ..utils.collection import CollectionElement
|
2016-10-14 21:48:02 +02:00
|
|
|
from ..utils.rest_api import (
|
2015-07-22 15:23:57 +02:00
|
|
|
DestroyModelMixin,
|
|
|
|
GenericViewSet,
|
2015-06-16 10:37:23 +02:00
|
|
|
ModelViewSet,
|
|
|
|
Response,
|
2015-07-22 15:23:57 +02:00
|
|
|
UpdateModelMixin,
|
2015-06-16 10:37:23 +02:00
|
|
|
ValidationError,
|
|
|
|
detail_route,
|
|
|
|
)
|
2018-01-26 14:56:42 +01:00
|
|
|
from ..utils.views import BinaryTemplateView
|
2016-02-11 22:58:32 +01:00
|
|
|
from .access_permissions import (
|
|
|
|
CategoryAccessPermissions,
|
|
|
|
MotionAccessPermissions,
|
2016-10-01 20:42:44 +02:00
|
|
|
MotionBlockAccessPermissions,
|
2016-09-10 18:49:38 +02:00
|
|
|
MotionChangeRecommendationAccessPermissions,
|
2016-02-11 22:58:32 +01:00
|
|
|
WorkflowAccessPermissions,
|
|
|
|
)
|
2015-07-22 15:23:57 +02:00
|
|
|
from .exceptions import WorkflowError
|
2016-09-03 21:43:11 +02:00
|
|
|
from .models import (
|
|
|
|
Category,
|
|
|
|
Motion,
|
2016-10-01 20:42:44 +02:00
|
|
|
MotionBlock,
|
2016-09-10 18:49:38 +02:00
|
|
|
MotionChangeRecommendation,
|
2016-09-03 21:43:11 +02:00
|
|
|
MotionPoll,
|
|
|
|
MotionVersion,
|
|
|
|
State,
|
|
|
|
Workflow,
|
|
|
|
)
|
2016-02-11 22:58:32 +01:00
|
|
|
from .serializers import MotionPollSerializer
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2013-02-02 00:37:43 +01:00
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
# Viewsets for the REST API
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class MotionViewSet(ModelViewSet):
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
2015-07-01 23:18:48 +02:00
|
|
|
API endpoint for motions.
|
|
|
|
|
2015-08-31 14:07:24 +02:00
|
|
|
There are the following views: metadata, list, retrieve, create,
|
2015-07-22 15:23:57 +02:00
|
|
|
partial_update, update, destroy, manage_version, support, set_state and
|
|
|
|
create_poll.
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
2016-02-11 22:58:32 +01:00
|
|
|
access_permissions = MotionAccessPermissions()
|
2015-01-24 16:35:50 +01:00
|
|
|
queryset = Motion.objects.all()
|
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
def check_view_permissions(self):
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
2015-07-01 23:18:48 +02:00
|
|
|
Returns True if the user has required permissions.
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
2016-09-17 22:26:23 +02:00
|
|
|
if self.action in ('list', 'retrieve'):
|
|
|
|
result = self.get_access_permissions().check_permissions(self.request.user)
|
2017-12-15 09:26:57 +01:00
|
|
|
elif self.action in ('metadata', 'partial_update', 'update', 'destroy'):
|
2017-01-26 15:34:24 +01:00
|
|
|
result = has_perm(self.request.user, 'motions.can_see')
|
2017-12-15 13:01:25 +01:00
|
|
|
# For partial_update, update and delete requests the rest of the check is
|
2015-07-01 23:18:48 +02:00
|
|
|
# done in the update method. See below.
|
|
|
|
elif self.action == 'create':
|
2017-01-26 15:34:24 +01:00
|
|
|
result = (has_perm(self.request.user, 'motions.can_see') and
|
|
|
|
has_perm(self.request.user, 'motions.can_create') and
|
2015-07-01 23:18:48 +02:00
|
|
|
(not config['motions_stop_submitting'] or
|
2017-01-26 15:34:24 +01:00
|
|
|
has_perm(self.request.user, 'motions.can_manage')))
|
2017-12-15 09:26:57 +01:00
|
|
|
elif self.action in ('manage_version', 'set_state', 'set_recommendation',
|
2017-11-11 12:28:24 +01:00
|
|
|
'follow_recommendation', 'create_poll'):
|
2017-01-26 15:34:24 +01:00
|
|
|
result = (has_perm(self.request.user, 'motions.can_see') and
|
|
|
|
has_perm(self.request.user, 'motions.can_manage'))
|
2015-07-01 23:18:48 +02:00
|
|
|
elif self.action == 'support':
|
2017-01-26 15:34:24 +01:00
|
|
|
result = (has_perm(self.request.user, 'motions.can_see') and
|
|
|
|
has_perm(self.request.user, 'motions.can_support'))
|
2015-07-01 23:18:48 +02:00
|
|
|
else:
|
|
|
|
result = False
|
|
|
|
return result
|
2015-01-24 16:35:50 +01:00
|
|
|
|
2017-12-15 09:26:57 +01:00
|
|
|
def destroy(self, request, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Destroy is allowed if the user has manage permissions, or he is the submitter and
|
|
|
|
the current state allows to edit the motion.
|
|
|
|
"""
|
|
|
|
motion = self.get_object()
|
|
|
|
|
2017-12-15 13:01:25 +01:00
|
|
|
if not ((has_perm(request.user, 'motions.can_manage') or
|
|
|
|
motion.is_submitter(request.user) and motion.state.allow_submitter_edit)):
|
|
|
|
self.permission_denied(request)
|
|
|
|
|
|
|
|
return super().destroy(request, *args, **kwargs)
|
2017-12-15 09:26:57 +01:00
|
|
|
|
2015-04-30 19:13:28 +02:00
|
|
|
def create(self, request, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Customized view endpoint to create a new motion.
|
|
|
|
"""
|
2017-02-27 15:37:01 +01:00
|
|
|
# Check if parent motion exists.
|
|
|
|
if request.data.get('parent_id') is not None:
|
2017-02-24 08:37:27 +01:00
|
|
|
try:
|
2017-02-27 15:37:01 +01:00
|
|
|
parent_motion = CollectionElement.from_values(
|
|
|
|
Motion.get_collection_string(),
|
2017-08-24 12:26:55 +02:00
|
|
|
request.data['parent_id']) # type: Optional[CollectionElement]
|
2017-02-24 08:37:27 +01:00
|
|
|
except Motion.DoesNotExist:
|
|
|
|
raise ValidationError({'detail': _('The parent motion does not exist.')})
|
2017-02-27 15:37:01 +01:00
|
|
|
else:
|
|
|
|
parent_motion = None
|
2017-02-24 08:37:27 +01:00
|
|
|
|
2017-01-14 10:14:18 +01:00
|
|
|
# Check permission to send some data.
|
2017-01-26 15:34:24 +01:00
|
|
|
if not has_perm(request.user, 'motions.can_manage'):
|
2017-03-28 18:41:08 +02:00
|
|
|
# Remove fields that the user is not allowed to send.
|
|
|
|
# The list() is required because we want to use del inside the loop.
|
|
|
|
keys = list(request.data.keys())
|
2017-02-24 08:37:27 +01:00
|
|
|
whitelist = [
|
2017-01-14 10:14:18 +01:00
|
|
|
'title',
|
|
|
|
'text',
|
|
|
|
'reason',
|
|
|
|
'comments', # This is checked later.
|
2017-02-24 08:37:27 +01:00
|
|
|
]
|
2017-02-27 15:37:01 +01:00
|
|
|
if parent_motion is not None:
|
|
|
|
# For creating amendments.
|
2017-02-24 08:37:27 +01:00
|
|
|
whitelist.extend([
|
|
|
|
'parent_id',
|
|
|
|
'category_id', # This will be set to the matching
|
|
|
|
'motion_block_id', # values from parent_motion.
|
|
|
|
])
|
2017-02-27 15:37:01 +01:00
|
|
|
request.data['category_id'] = parent_motion.get_full_data().get('category_id')
|
|
|
|
request.data['motion_block_id'] = parent_motion.get_full_data().get('motion_block_id')
|
2017-03-28 18:41:08 +02:00
|
|
|
for key in keys:
|
2017-01-14 10:14:18 +01:00
|
|
|
if key not in whitelist:
|
2017-03-28 18:41:08 +02:00
|
|
|
del request.data[key]
|
2016-10-01 20:42:44 +02:00
|
|
|
|
2016-07-29 23:33:47 +02:00
|
|
|
# Check permission to send comment data.
|
2018-02-02 13:51:19 +01:00
|
|
|
if (not has_perm(request.user, 'motions.can_see_comments') or
|
|
|
|
not has_perm(request.user, 'motions.can_manage_comments')):
|
2016-09-16 23:35:37 +02:00
|
|
|
try:
|
|
|
|
# Ignore comments data if user is not allowed to send comments.
|
|
|
|
del request.data['comments']
|
|
|
|
except KeyError:
|
|
|
|
# No comments here. Just do nothing.
|
|
|
|
pass
|
2016-07-29 23:33:47 +02:00
|
|
|
|
2015-04-30 19:13:28 +02:00
|
|
|
# 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)
|
2017-04-28 22:10:18 +02:00
|
|
|
|
|
|
|
# Send new submitters and supporters via autoupdate because users
|
|
|
|
# without permission to see users may not have them but can get it now.
|
|
|
|
new_users = list(motion.submitters.all())
|
|
|
|
new_users.extend(motion.supporters.all())
|
|
|
|
inform_changed_data(new_users)
|
|
|
|
|
2015-04-30 19:13:28 +02:00
|
|
|
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
|
2016-12-09 18:00:45 +01:00
|
|
|
self.check_view_permissions()). Also check manage permission or
|
|
|
|
submitter and state.
|
2015-04-30 19:13:28 +02:00
|
|
|
"""
|
|
|
|
# Get motion.
|
|
|
|
motion = self.get_object()
|
|
|
|
|
|
|
|
# Check permissions.
|
2017-01-26 15:34:24 +01:00
|
|
|
if (not has_perm(request.user, 'motions.can_manage') and
|
2017-03-07 09:55:26 +01:00
|
|
|
not (motion.is_submitter(request.user) and motion.state.allow_submitter_edit) and
|
2018-02-02 13:51:19 +01:00
|
|
|
not (has_perm(request.user, 'motions.can_see_comments') and
|
|
|
|
has_perm(request.user, 'motions.can_manage_comments'))):
|
2015-04-30 19:13:28 +02:00
|
|
|
self.permission_denied(request)
|
|
|
|
|
2016-01-14 23:44:19 +01:00
|
|
|
# Check permission to send only some data.
|
2017-01-26 15:34:24 +01:00
|
|
|
if not has_perm(request.user, 'motions.can_manage'):
|
2017-02-27 15:37:01 +01:00
|
|
|
# Remove fields that the user is not allowed to change.
|
|
|
|
# The list() is required because we want to use del inside the loop.
|
|
|
|
keys = list(request.data.keys())
|
2017-03-07 09:55:26 +01:00
|
|
|
whitelist = [
|
2017-02-27 15:37:01 +01:00
|
|
|
'comments', # This is checked later.
|
2017-03-07 09:55:26 +01:00
|
|
|
]
|
|
|
|
# Add title, text and reason to the whitelist only, if the user is the submitter.
|
|
|
|
if motion.is_submitter(request.user) and motion.state.allow_submitter_edit:
|
|
|
|
whitelist.extend((
|
|
|
|
'title',
|
|
|
|
'text',
|
|
|
|
'reason',
|
|
|
|
))
|
2016-01-14 23:44:19 +01:00
|
|
|
for key in keys:
|
|
|
|
if key not in whitelist:
|
|
|
|
del request.data[key]
|
2018-02-02 13:51:19 +01:00
|
|
|
if (not has_perm(request.user, 'motions.can_see_comments') or
|
|
|
|
not has_perm(request.user, 'motions.can_manage_comments')):
|
2016-07-29 23:33:47 +02:00
|
|
|
try:
|
|
|
|
del request.data['comments']
|
|
|
|
except KeyError:
|
|
|
|
# No comments here. Just do nothing.
|
|
|
|
pass
|
2015-04-30 19:13:28 +02:00
|
|
|
|
2018-01-26 14:56:42 +01:00
|
|
|
# get changed comment fields
|
|
|
|
changed_comment_fields = []
|
|
|
|
comments = request.data.get('comments', {})
|
|
|
|
for id, value in comments.items():
|
|
|
|
if not motion.comments or motion.comments.get(id) != value:
|
|
|
|
field = config['motions_comments'].get(id)
|
|
|
|
if field:
|
|
|
|
name = field['name']
|
|
|
|
changed_comment_fields.append(name)
|
|
|
|
|
2015-04-30 19:13:28 +02:00
|
|
|
# Validate data and update motion.
|
|
|
|
serializer = self.get_serializer(
|
|
|
|
motion,
|
|
|
|
data=request.data,
|
|
|
|
partial=kwargs.get('partial', False))
|
|
|
|
serializer.is_valid(raise_exception=True)
|
2016-01-14 23:13:15 +01:00
|
|
|
updated_motion = serializer.save(disable_versioning=request.data.get('disable_versioning'))
|
2015-04-30 19:13:28 +02:00
|
|
|
|
|
|
|
# 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
|
2017-01-26 15:34:24 +01:00
|
|
|
not has_perm(request.user, 'motions.can_manage')):
|
2015-04-30 19:13:28 +02:00
|
|
|
updated_motion.supporters.clear()
|
|
|
|
updated_motion.write_log([ugettext_noop('All supporters removed')], request.user)
|
2017-04-28 22:10:18 +02:00
|
|
|
|
2018-01-26 14:56:42 +01:00
|
|
|
if len(changed_comment_fields) > 0:
|
|
|
|
updated_motion.write_log(
|
|
|
|
[ugettext_noop('Comment {} updated').format(', '.join(changed_comment_fields))],
|
|
|
|
request.user)
|
|
|
|
|
2017-04-28 22:10:18 +02:00
|
|
|
# Send new submitters and supporters via autoupdate because users
|
|
|
|
# without permission to see users may not have them but can get it now.
|
|
|
|
new_users = list(updated_motion.submitters.all())
|
|
|
|
new_users.extend(updated_motion.supporters.all())
|
|
|
|
inform_changed_data(new_users)
|
|
|
|
|
2015-04-30 19:13:28 +02:00
|
|
|
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.
|
|
|
|
"""
|
|
|
|
# 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.
|
|
|
|
"""
|
|
|
|
# Retrieve motion and allowed actions.
|
|
|
|
motion = self.get_object()
|
|
|
|
|
|
|
|
# Support or unsupport motion.
|
|
|
|
if request.method == 'POST':
|
|
|
|
# Support motion.
|
2016-12-09 18:00:45 +01:00
|
|
|
if not (motion.state.allow_support and
|
|
|
|
config['motions_min_supporters'] > 0 and
|
|
|
|
not motion.is_submitter(request.user) and
|
|
|
|
not motion.is_supporter(request.user)):
|
2015-04-30 19:13:28 +02:00
|
|
|
raise ValidationError({'detail': _('You can not support this motion.')})
|
|
|
|
motion.supporters.add(request.user)
|
|
|
|
motion.write_log([ugettext_noop('Motion supported')], request.user)
|
2017-04-28 22:10:18 +02:00
|
|
|
# Send new supporter via autoupdate because users without permission
|
|
|
|
# to see users may not have it but can get it now.
|
|
|
|
inform_changed_data([request.user])
|
2015-04-30 19:13:28 +02:00
|
|
|
message = _('You have supported this motion successfully.')
|
|
|
|
else:
|
|
|
|
# Unsupport motion.
|
|
|
|
# request.method == 'DELETE'
|
2016-12-09 18:00:45 +01:00
|
|
|
if not motion.state.allow_support or not motion.is_supporter(request.user):
|
2015-04-30 19:13:28 +02:00
|
|
|
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.
|
|
|
|
"""
|
|
|
|
# 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.
|
2017-11-11 12:28:24 +01:00
|
|
|
motion.save(update_fields=['state', 'identifier', 'identifier_number'], skip_autoupdate=True)
|
2015-04-30 19:13:28 +02:00
|
|
|
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],
|
2017-11-11 12:28:24 +01:00
|
|
|
person=request.user,
|
|
|
|
skip_autoupdate=True)
|
|
|
|
inform_changed_data(motion)
|
2015-04-30 19:13:28 +02:00
|
|
|
return Response({'detail': message})
|
|
|
|
|
2016-09-03 21:43:11 +02:00
|
|
|
@detail_route(methods=['put'])
|
|
|
|
def set_recommendation(self, request, pk=None):
|
|
|
|
"""
|
|
|
|
Special view endpoint to set a recommendation of a motion.
|
|
|
|
|
|
|
|
Send PUT {'recommendation': <state_id>} to set and just PUT {} to
|
|
|
|
reset the recommendation. Only managers can use this view.
|
|
|
|
"""
|
|
|
|
# Retrieve motion and recommendation state.
|
|
|
|
motion = self.get_object()
|
|
|
|
recommendation_state = request.data.get('recommendation')
|
|
|
|
|
|
|
|
# Set or reset recommendation.
|
|
|
|
if recommendation_state is not None:
|
|
|
|
# Check data and set recommendation.
|
|
|
|
try:
|
|
|
|
recommendation_state_id = int(recommendation_state)
|
|
|
|
except ValueError:
|
|
|
|
raise ValidationError({'detail': _('Invalid data. Recommendation must be an integer.')})
|
|
|
|
recommendable_states = State.objects.filter(workflow=motion.workflow, recommendation_label__isnull=False)
|
|
|
|
if recommendation_state_id not in [item.id for item in recommendable_states]:
|
|
|
|
raise ValidationError(
|
|
|
|
{'detail': _('You can not set the recommendation to {recommendation_state_id}.').format(
|
|
|
|
recommendation_state_id=recommendation_state_id)})
|
|
|
|
motion.set_recommendation(recommendation_state_id)
|
|
|
|
else:
|
|
|
|
# Reset recommendation.
|
|
|
|
motion.recommendation = None
|
|
|
|
|
|
|
|
# Save motion.
|
|
|
|
motion.save(update_fields=['recommendation'])
|
|
|
|
label = motion.recommendation.recommendation_label if motion.recommendation else 'None'
|
|
|
|
message = _('The recommendation of the motion was set to %s.') % label
|
|
|
|
|
|
|
|
# Write the log message and initiate response.
|
|
|
|
motion.write_log(
|
|
|
|
message_list=[ugettext_noop('Recommendation set to'), ' ', label],
|
|
|
|
person=request.user)
|
|
|
|
return Response({'detail': message})
|
|
|
|
|
2017-11-11 12:28:24 +01:00
|
|
|
@detail_route(methods=['post'])
|
|
|
|
def follow_recommendation(self, request, pk=None):
|
|
|
|
motion = self.get_object()
|
|
|
|
if motion.recommendation is None:
|
|
|
|
raise ValidationError({'detail': _('Cannot set an empty recommendation.')})
|
|
|
|
|
|
|
|
# Set state.
|
|
|
|
motion.set_state(motion.recommendation)
|
|
|
|
|
|
|
|
# Set the special state comment.
|
|
|
|
extension = request.data.get('recommendationExtension')
|
|
|
|
if extension is not None:
|
|
|
|
# Find the special "state" comment field.
|
|
|
|
for id, field in config['motions_comments'].items():
|
2018-01-23 08:49:13 +01:00
|
|
|
if isinstance(field, dict) and 'forState' in field and field['forState'] is True:
|
2017-11-11 12:28:24 +01:00
|
|
|
motion.comments[id] = extension
|
|
|
|
break
|
|
|
|
|
|
|
|
# Save and write log.
|
|
|
|
motion.save(
|
|
|
|
update_fields=['state', 'identifier', 'identifier_number', 'comments'],
|
|
|
|
skip_autoupdate=True)
|
|
|
|
motion.write_log(
|
|
|
|
message_list=[ugettext_noop('State set to'), ' ', motion.state.name],
|
|
|
|
person=request.user,
|
|
|
|
skip_autoupdate=True)
|
|
|
|
|
|
|
|
# Now send all changes to the clients.
|
|
|
|
inform_changed_data(motion)
|
|
|
|
return Response({'detail': 'Recommendation followed successfully.'})
|
|
|
|
|
2015-07-22 15:23:57 +02:00
|
|
|
@detail_route(methods=['post'])
|
|
|
|
def create_poll(self, request, pk=None):
|
|
|
|
"""
|
|
|
|
View to create a poll. It is a POST request without any data.
|
|
|
|
"""
|
|
|
|
motion = self.get_object()
|
2016-12-09 18:00:45 +01:00
|
|
|
if not motion.state.allow_create_poll:
|
|
|
|
raise ValidationError({'detail': 'You can not create a poll in this motion state.'})
|
2015-07-22 15:23:57 +02:00
|
|
|
try:
|
|
|
|
with transaction.atomic():
|
2018-03-26 14:01:52 +02:00
|
|
|
poll = motion.create_poll(skip_autoupdate=True)
|
2015-07-22 15:23:57 +02:00
|
|
|
except WorkflowError as e:
|
|
|
|
raise ValidationError({'detail': e})
|
2017-11-11 12:28:24 +01:00
|
|
|
motion.write_log([ugettext_noop('Vote created')], request.user, skip_autoupdate=True)
|
|
|
|
|
|
|
|
inform_changed_data(motion)
|
2018-03-26 14:01:52 +02:00
|
|
|
return Response({
|
|
|
|
'detail': _('Vote created successfully.'),
|
|
|
|
'createdPollId': poll.pk})
|
2015-07-22 15:23:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
class MotionPollViewSet(UpdateModelMixin, DestroyModelMixin, GenericViewSet):
|
|
|
|
"""
|
|
|
|
API endpoint for motion polls.
|
|
|
|
|
2017-02-27 15:37:01 +01:00
|
|
|
There are the following views: update, partial_update and destroy.
|
2015-07-22 15:23:57 +02:00
|
|
|
"""
|
|
|
|
queryset = MotionPoll.objects.all()
|
|
|
|
serializer_class = MotionPollSerializer
|
|
|
|
|
|
|
|
def check_view_permissions(self):
|
|
|
|
"""
|
|
|
|
Returns True if the user has required permissions.
|
|
|
|
"""
|
2017-01-26 15:34:24 +01:00
|
|
|
return (has_perm(self.request.user, 'motions.can_see') and
|
|
|
|
has_perm(self.request.user, 'motions.can_manage'))
|
2015-07-22 15:23:57 +02:00
|
|
|
|
2017-02-20 20:13:58 +01:00
|
|
|
def update(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Customized view endpoint to update a motion poll.
|
|
|
|
"""
|
|
|
|
result = super().update(*args, **kwargs)
|
|
|
|
poll = self.get_object()
|
|
|
|
poll.motion.write_log([ugettext_noop('Vote updated')], self.request.user)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def destroy(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Customized view endpoint to delete a motion poll.
|
|
|
|
"""
|
|
|
|
poll = self.get_object()
|
2017-03-22 14:00:03 +01:00
|
|
|
result = super().destroy(*args, **kwargs)
|
2017-02-20 20:13:58 +01:00
|
|
|
poll.motion.write_log([ugettext_noop('Vote deleted')], self.request.user)
|
|
|
|
return result
|
|
|
|
|
2015-01-24 16:35:50 +01:00
|
|
|
|
2016-09-10 18:49:38 +02:00
|
|
|
class MotionChangeRecommendationViewSet(ModelViewSet):
|
|
|
|
"""
|
|
|
|
API endpoint for motion change recommendations.
|
|
|
|
|
|
|
|
There are the following views: metadata, list, retrieve, create,
|
|
|
|
partial_update, update and destroy.
|
|
|
|
"""
|
|
|
|
access_permissions = MotionChangeRecommendationAccessPermissions()
|
|
|
|
queryset = MotionChangeRecommendation.objects.all()
|
|
|
|
|
|
|
|
def check_view_permissions(self):
|
|
|
|
"""
|
|
|
|
Returns True if the user has required permissions.
|
|
|
|
"""
|
|
|
|
if self.action in ('list', 'retrieve'):
|
|
|
|
result = self.get_access_permissions().check_permissions(self.request.user)
|
|
|
|
elif self.action == 'metadata':
|
2017-01-26 15:34:24 +01:00
|
|
|
result = has_perm(self.request.user, 'motions.can_see')
|
2016-09-10 18:49:38 +02:00
|
|
|
elif self.action in ('create', 'destroy', 'partial_update', 'update'):
|
2017-02-27 15:37:01 +01:00
|
|
|
result = (has_perm(self.request.user, 'motions.can_see') and
|
|
|
|
has_perm(self.request.user, 'motions.can_manage'))
|
2016-09-10 18:49:38 +02:00
|
|
|
else:
|
|
|
|
result = False
|
|
|
|
return result
|
|
|
|
|
2017-06-18 20:20:44 +02:00
|
|
|
def create(self, request, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Creating a Change Recommendation, custom exception handling
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return super().create(request, *args, **kwargs)
|
|
|
|
except DjangoValidationError as err:
|
|
|
|
return Response({'detail': err.message}, status=400)
|
|
|
|
|
2016-09-10 18:49:38 +02:00
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
class CategoryViewSet(ModelViewSet):
|
|
|
|
"""
|
|
|
|
API endpoint for categories.
|
|
|
|
|
2015-08-31 14:07:24 +02:00
|
|
|
There are the following views: metadata, list, retrieve, create,
|
2016-10-01 20:42:44 +02:00
|
|
|
partial_update, update, destroy and numbering.
|
2015-07-01 23:18:48 +02:00
|
|
|
"""
|
2016-02-11 22:58:32 +01:00
|
|
|
access_permissions = CategoryAccessPermissions()
|
2015-07-01 23:18:48 +02:00
|
|
|
queryset = Category.objects.all()
|
|
|
|
|
|
|
|
def check_view_permissions(self):
|
|
|
|
"""
|
|
|
|
Returns True if the user has required permissions.
|
|
|
|
"""
|
2016-09-17 22:26:23 +02:00
|
|
|
if self.action in ('list', 'retrieve'):
|
|
|
|
result = self.get_access_permissions().check_permissions(self.request.user)
|
|
|
|
elif self.action == 'metadata':
|
2017-01-26 15:34:24 +01:00
|
|
|
result = has_perm(self.request.user, 'motions.can_see')
|
2016-07-13 01:39:28 +02:00
|
|
|
elif self.action in ('create', 'partial_update', 'update', 'destroy', 'numbering'):
|
2017-01-26 15:34:24 +01:00
|
|
|
result = (has_perm(self.request.user, 'motions.can_see') and
|
|
|
|
has_perm(self.request.user, 'motions.can_manage'))
|
2015-07-01 23:18:48 +02:00
|
|
|
else:
|
|
|
|
result = False
|
|
|
|
return result
|
|
|
|
|
2016-07-13 01:39:28 +02:00
|
|
|
@detail_route(methods=['post'])
|
|
|
|
def numbering(self, request, pk=None):
|
|
|
|
"""
|
|
|
|
Special view endpoint to number all motions in this category.
|
|
|
|
|
|
|
|
Only managers can use this view.
|
2016-08-15 23:53:21 +02:00
|
|
|
|
|
|
|
Send POST {'motions': [<list of motion ids>]} to sort the given
|
|
|
|
motions in a special order. Ids of motions which do not belong to
|
|
|
|
the category are just ignored. Send just POST {} to sort all
|
2016-12-03 21:59:13 +01:00
|
|
|
motions in the category by database id.
|
|
|
|
|
|
|
|
Amendments will get a new identifier prefix if the old prefix matches
|
|
|
|
the old parent motion identifier.
|
2016-07-13 01:39:28 +02:00
|
|
|
"""
|
|
|
|
category = self.get_object()
|
|
|
|
number = 0
|
2016-12-03 21:59:13 +01:00
|
|
|
instances = []
|
|
|
|
|
2017-02-06 12:11:50 +01:00
|
|
|
# If MOTION_IDENTIFIER_WITHOUT_BLANKS is set, don't use blanks when building identifier.
|
|
|
|
without_blank = hasattr(settings, 'MOTION_IDENTIFIER_WITHOUT_BLANKS') and settings.MOTION_IDENTIFIER_WITHOUT_BLANKS
|
|
|
|
|
2016-12-03 21:59:13 +01:00
|
|
|
# Prepare ordered list of motions.
|
2016-07-13 01:39:28 +02:00
|
|
|
if not category.prefix:
|
|
|
|
prefix = ''
|
2017-02-06 12:11:50 +01:00
|
|
|
elif without_blank:
|
|
|
|
prefix = '%s' % category.prefix
|
2016-07-13 01:39:28 +02:00
|
|
|
else:
|
|
|
|
prefix = '%s ' % category.prefix
|
2016-08-15 23:53:21 +02:00
|
|
|
motions = category.motion_set.all()
|
|
|
|
motion_list = request.data.get('motions')
|
|
|
|
if motion_list:
|
|
|
|
motion_dict = {}
|
|
|
|
for motion in motions.filter(id__in=motion_list):
|
|
|
|
motion_dict[motion.pk] = motion
|
|
|
|
motions = [motion_dict[pk] for pk in motion_list]
|
2016-07-13 01:39:28 +02:00
|
|
|
|
2016-12-03 21:59:13 +01:00
|
|
|
# Change identifiers.
|
2016-07-29 23:33:47 +02:00
|
|
|
try:
|
|
|
|
with transaction.atomic():
|
2016-12-03 21:59:13 +01:00
|
|
|
# Collect old and new identifiers.
|
|
|
|
motions_to_be_sorted = []
|
2016-07-29 23:33:47 +02:00
|
|
|
for motion in motions:
|
|
|
|
if motion.is_amendment():
|
|
|
|
parent_identifier = motion.parent.identifier or ''
|
2017-02-06 12:11:50 +01:00
|
|
|
if without_blank:
|
|
|
|
prefix = '%s%s' % (parent_identifier, config['motions_amendments_prefix'])
|
|
|
|
else:
|
|
|
|
prefix = '%s %s ' % (parent_identifier, config['motions_amendments_prefix'])
|
2016-07-29 23:33:47 +02:00
|
|
|
number += 1
|
2016-12-03 21:59:13 +01:00
|
|
|
new_identifier = '%s%s' % (prefix, motion.extend_identifier_number(number))
|
|
|
|
motions_to_be_sorted.append({
|
|
|
|
'motion': motion,
|
|
|
|
'old_identifier': motion.identifier,
|
|
|
|
'new_identifier': new_identifier,
|
|
|
|
'number': number
|
|
|
|
})
|
|
|
|
|
|
|
|
# Remove old identifiers
|
|
|
|
for motion in motions:
|
|
|
|
motion.identifier = None
|
2018-01-20 09:54:46 +01:00
|
|
|
# This line is to skip agenda item autoupdate. See agenda/signals.py.
|
|
|
|
motion.agenda_item_update_information['skip_autoupdate'] = True
|
2016-12-03 21:59:13 +01:00
|
|
|
motion.save(skip_autoupdate=True)
|
|
|
|
|
|
|
|
# Set new identifers and change identifiers of amendments.
|
|
|
|
for obj in motions_to_be_sorted:
|
2018-03-14 13:11:36 +01:00
|
|
|
if Motion.objects.filter(identifier=obj['new_identifier']).exists():
|
|
|
|
raise ValidationError({
|
|
|
|
'detail':
|
|
|
|
_('Numbering aborted because the motion identifier "%s" already exists outside of this category.') %
|
|
|
|
obj['new_identifier']
|
|
|
|
})
|
2016-12-03 21:59:13 +01:00
|
|
|
motion = obj['motion']
|
|
|
|
motion.identifier = obj['new_identifier']
|
|
|
|
motion.identifier_number = obj['number']
|
2016-12-02 15:47:57 +01:00
|
|
|
motion.save(skip_autoupdate=True)
|
|
|
|
instances.append(motion)
|
|
|
|
instances.append(motion.agenda_item)
|
2016-12-03 21:59:13 +01:00
|
|
|
# Change identifiers of amendments.
|
|
|
|
for child in motion.get_amendments_deep():
|
2018-03-14 13:11:36 +01:00
|
|
|
if child.identifier and child.identifier.startswith(obj['old_identifier']):
|
2016-12-03 21:59:13 +01:00
|
|
|
child.identifier = re.sub(
|
|
|
|
obj['old_identifier'],
|
|
|
|
obj['new_identifier'],
|
|
|
|
child.identifier,
|
|
|
|
count=1)
|
2018-01-20 09:54:46 +01:00
|
|
|
# This line is to skip agenda item autoupdate. See agenda/signals.py.
|
|
|
|
child.agenda_item_update_information['skip_autoupdate'] = True
|
2016-12-03 21:59:13 +01:00
|
|
|
child.save(skip_autoupdate=True)
|
|
|
|
instances.append(child)
|
|
|
|
instances.append(child.agenda_item)
|
2016-07-29 23:33:47 +02:00
|
|
|
except IntegrityError:
|
|
|
|
message = _('Error: At least one identifier of this category does '
|
|
|
|
'already exist in another category.')
|
2016-12-05 12:40:28 +01:00
|
|
|
response = Response({'detail': message}, status=400)
|
2016-07-29 23:33:47 +02:00
|
|
|
else:
|
2016-12-02 15:47:57 +01:00
|
|
|
inform_changed_data(instances)
|
2016-07-29 23:33:47 +02:00
|
|
|
message = _('All motions in category {category} numbered '
|
|
|
|
'successfully.').format(category=category)
|
|
|
|
response = Response({'detail': message})
|
|
|
|
return response
|
2016-07-13 01:39:28 +02:00
|
|
|
|
2016-10-01 20:42:44 +02:00
|
|
|
|
|
|
|
class MotionBlockViewSet(ModelViewSet):
|
|
|
|
"""
|
|
|
|
API endpoint for motion blocks.
|
|
|
|
|
|
|
|
There are the following views: metadata, list, retrieve, create,
|
|
|
|
partial_update, update and destroy.
|
|
|
|
"""
|
|
|
|
access_permissions = MotionBlockAccessPermissions()
|
|
|
|
queryset = MotionBlock.objects.all()
|
|
|
|
|
|
|
|
def check_view_permissions(self):
|
|
|
|
"""
|
|
|
|
Returns True if the user has required permissions.
|
|
|
|
"""
|
|
|
|
if self.action in ('list', 'retrieve'):
|
|
|
|
result = self.get_access_permissions().check_permissions(self.request.user)
|
|
|
|
elif self.action == 'metadata':
|
2017-01-26 15:34:24 +01:00
|
|
|
result = has_perm(self.request.user, 'motions.can_see')
|
2016-10-14 21:48:02 +02:00
|
|
|
elif self.action in ('create', 'partial_update', 'update', 'destroy', 'follow_recommendations'):
|
2017-01-26 15:34:24 +01:00
|
|
|
result = (has_perm(self.request.user, 'motions.can_see') and
|
|
|
|
has_perm(self.request.user, 'motions.can_manage'))
|
2016-10-01 20:42:44 +02:00
|
|
|
else:
|
|
|
|
result = False
|
|
|
|
return result
|
|
|
|
|
2016-10-14 21:48:02 +02:00
|
|
|
@detail_route(methods=['post'])
|
|
|
|
def follow_recommendations(self, request, pk=None):
|
|
|
|
"""
|
|
|
|
View to set the states of all motions of this motion block each to
|
|
|
|
its recommendation. It is a POST request without any data.
|
|
|
|
"""
|
|
|
|
motion_block = self.get_object()
|
|
|
|
instances = []
|
|
|
|
with transaction.atomic():
|
|
|
|
for motion in motion_block.motion_set.all():
|
|
|
|
# Follow recommendation.
|
|
|
|
motion.follow_recommendation()
|
|
|
|
motion.save(skip_autoupdate=True)
|
|
|
|
# Write the log message.
|
|
|
|
motion.write_log(
|
|
|
|
message_list=[ugettext_noop('State set to'), ' ', motion.state.name],
|
|
|
|
person=request.user,
|
|
|
|
skip_autoupdate=True)
|
|
|
|
instances.append(motion)
|
|
|
|
inform_changed_data(instances)
|
|
|
|
return Response({'detail': _('Followed recommendations successfully.')})
|
|
|
|
|
2015-07-01 23:18:48 +02:00
|
|
|
|
|
|
|
class WorkflowViewSet(ModelViewSet):
|
|
|
|
"""
|
|
|
|
API endpoint for workflows.
|
|
|
|
|
2015-08-31 14:07:24 +02:00
|
|
|
There are the following views: metadata, list, retrieve, create,
|
|
|
|
partial_update, update and destroy.
|
2015-07-01 23:18:48 +02:00
|
|
|
"""
|
2016-02-11 22:58:32 +01:00
|
|
|
access_permissions = WorkflowAccessPermissions()
|
2015-07-01 23:18:48 +02:00
|
|
|
queryset = Workflow.objects.all()
|
|
|
|
|
|
|
|
def check_view_permissions(self):
|
|
|
|
"""
|
|
|
|
Returns True if the user has required permissions.
|
|
|
|
"""
|
2016-09-17 22:26:23 +02:00
|
|
|
if self.action in ('list', 'retrieve'):
|
|
|
|
result = self.get_access_permissions().check_permissions(self.request.user)
|
|
|
|
elif self.action == 'metadata':
|
2017-01-26 15:34:24 +01:00
|
|
|
result = has_perm(self.request.user, 'motions.can_see')
|
2015-07-01 23:18:48 +02:00
|
|
|
elif self.action in ('create', 'partial_update', 'update', 'destroy'):
|
2017-01-26 15:34:24 +01:00
|
|
|
result = (has_perm(self.request.user, 'motions.can_see') and
|
|
|
|
has_perm(self.request.user, 'motions.can_manage'))
|
2015-07-01 23:18:48 +02:00
|
|
|
else:
|
|
|
|
result = False
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2018-01-26 14:56:42 +01:00
|
|
|
# Special views
|
2017-02-27 15:37:01 +01:00
|
|
|
|
2018-01-26 14:56:42 +01:00
|
|
|
class MotionDocxTemplateView(BinaryTemplateView):
|
2016-09-13 11:54:30 +02:00
|
|
|
"""
|
|
|
|
Returns the template for motions docx export
|
|
|
|
"""
|
2018-01-26 14:56:42 +01:00
|
|
|
template_name = 'templates/docx/motions.docx'
|