2018-11-27 22:35:19 +01:00
|
|
|
import jsonschema
|
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
|
2012-02-20 17:46:45 +01:00
|
|
|
|
2015-11-25 10:48:33 +01:00
|
|
|
from openslides.core.config import config
|
2016-12-06 12:21:29 +01:00
|
|
|
from openslides.utils.autoupdate import inform_changed_data
|
2015-05-26 18:21:30 +02:00
|
|
|
from openslides.utils.exceptions import OpenSlidesError
|
|
|
|
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,
|
|
|
|
)
|
2014-03-27 20:30:15 +01:00
|
|
|
|
2016-02-11 22:58:32 +01:00
|
|
|
from .access_permissions import ItemAccessPermissions
|
2015-05-26 18:21:30 +02:00
|
|
|
from .models import Item, Speaker
|
2019-03-04 18:40:08 +01:00
|
|
|
from ..utils.auth import has_perm
|
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
|
|
|
|
2019-01-06 16:22:33 +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.
|
|
|
|
|
2018-11-27 22:35:19 +01:00
|
|
|
There are some views, see check_view_permissions.
|
2015-01-06 00:14:49 +01:00
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2016-02-11 22:58:32 +01:00
|
|
|
access_permissions = ItemAccessPermissions()
|
2015-02-12 18:48:14 +01:00
|
|
|
queryset = Item.objects.all()
|
2015-01-06 00:14:49 +01:00
|
|
|
|
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
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
if self.action in ("list", "retrieve"):
|
2016-09-17 22:26:23 +02:00
|
|
|
result = self.get_access_permissions().check_permissions(self.request.user)
|
2019-01-06 16:22:33 +01:00
|
|
|
elif self.action in ("metadata", "manage_speaker", "tree"):
|
|
|
|
result = has_perm(self.request.user, "agenda.can_see")
|
2015-07-01 23:18:48 +02:00
|
|
|
# For manage_speaker and tree requests the rest of the check is
|
|
|
|
# done in the specific method. See below.
|
2019-01-06 16:22:33 +01:00
|
|
|
elif self.action in ("partial_update", "update", "sort", "assign"):
|
|
|
|
result = (
|
|
|
|
has_perm(self.request.user, "agenda.can_see")
|
|
|
|
and has_perm(self.request.user, "agenda.can_see_internal_items")
|
|
|
|
and has_perm(self.request.user, "agenda.can_manage")
|
|
|
|
)
|
|
|
|
elif self.action in ("speak", "sort_speakers"):
|
|
|
|
result = has_perm(self.request.user, "agenda.can_see") and has_perm(
|
|
|
|
self.request.user, "agenda.can_manage_list_of_speakers"
|
|
|
|
)
|
|
|
|
elif self.action in ("numbering",):
|
|
|
|
result = has_perm(self.request.user, "agenda.can_see") and has_perm(
|
|
|
|
self.request.user, "agenda.can_manage"
|
|
|
|
)
|
2015-07-01 23:18:48 +02:00
|
|
|
else:
|
|
|
|
result = False
|
|
|
|
return result
|
2015-01-06 00:14:49 +01:00
|
|
|
|
2018-03-19 10:16:22 +01:00
|
|
|
def update(self, *args, **kwargs):
|
|
|
|
"""
|
2018-10-15 21:25:41 +02:00
|
|
|
Customized view endpoint to update all children if the item type has changed.
|
2018-03-19 10:16:22 +01:00
|
|
|
"""
|
2018-08-15 11:15:54 +02:00
|
|
|
old_type = self.get_object().type
|
2018-03-19 10:16:22 +01:00
|
|
|
|
2018-10-15 21:25:41 +02:00
|
|
|
response = super().update(*args, **kwargs)
|
2018-03-19 10:16:22 +01:00
|
|
|
|
2018-10-15 21:25:41 +02:00
|
|
|
# Update all children if the item type has changed.
|
2018-03-19 10:16:22 +01:00
|
|
|
item = self.get_object()
|
2018-08-15 11:15:54 +02:00
|
|
|
|
|
|
|
if old_type != item.type:
|
2018-03-19 10:16:22 +01:00
|
|
|
items_to_update = []
|
|
|
|
|
2018-10-15 21:25:41 +02:00
|
|
|
# Recursively add children to items_to_update.
|
2018-03-19 10:16:22 +01:00
|
|
|
def add_item(item):
|
|
|
|
items_to_update.append(item)
|
|
|
|
for child in item.children.all():
|
|
|
|
add_item(child)
|
|
|
|
|
|
|
|
add_item(item)
|
|
|
|
inform_changed_data(items_to_update)
|
|
|
|
|
2018-10-15 21:25:41 +02:00
|
|
|
return response
|
2018-03-19 10:16:22 +01:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@detail_route(methods=["POST", "PATCH", "DELETE"])
|
2015-05-26 18:21:30 +02:00
|
|
|
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
|
2016-07-07 13:33:18 +02:00
|
|
|
data to add yourself. Send DELETE {'speaker': <speaker_id>} or
|
|
|
|
DELETE {'speaker': [<speaker_id>, <speaker_id>, ...]} to remove one or
|
|
|
|
more speakers from the list of speakers. Omit data to remove yourself.
|
2018-02-06 14:52:47 +01:00
|
|
|
Send PATCH {'user': <user_id>, 'marked': <bool>} to mark the speaker.
|
2015-05-26 18:21:30 +02:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
if request.method == "POST":
|
2015-05-26 18:21:30 +02:00
|
|
|
# Retrieve user_id
|
2019-01-06 16:22:33 +01:00
|
|
|
user_id = request.data.get("user")
|
2015-05-26 18:21:30 +02:00
|
|
|
|
|
|
|
# Check permissions and other conditions. Get user instance.
|
|
|
|
if user_id is None:
|
|
|
|
# Add oneself
|
2019-01-06 16:22:33 +01:00
|
|
|
if not has_perm(self.request.user, "agenda.can_be_speaker"):
|
2015-05-26 18:21:30 +02:00
|
|
|
self.permission_denied(request)
|
|
|
|
if item.speaker_list_closed:
|
2019-01-12 23:01:42 +01:00
|
|
|
raise ValidationError({"detail": "The list of speakers is closed."})
|
2015-05-26 18:21:30 +02:00
|
|
|
user = self.request.user
|
|
|
|
else:
|
|
|
|
# Add someone else.
|
2019-01-06 16:22:33 +01:00
|
|
|
if not has_perm(
|
|
|
|
self.request.user, "agenda.can_manage_list_of_speakers"
|
|
|
|
):
|
2015-05-26 18:21:30 +02:00
|
|
|
self.permission_denied(request)
|
|
|
|
try:
|
|
|
|
user = get_user_model().objects.get(pk=int(user_id))
|
|
|
|
except (ValueError, get_user_model().DoesNotExist):
|
2019-01-12 23:01:42 +01:00
|
|
|
raise ValidationError({"detail": "User does not exist."})
|
2015-05-26 18:21:30 +02:00
|
|
|
|
|
|
|
# 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:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError({"detail": str(e)})
|
2015-05-26 18:21:30 +02:00
|
|
|
|
2017-04-28 22:10:18 +02:00
|
|
|
# Send new speaker via autoupdate because users without permission
|
|
|
|
# to see users may not have it but can get it now.
|
|
|
|
inform_changed_data([user])
|
|
|
|
|
2018-02-06 14:52:47 +01:00
|
|
|
# Toggle 'marked' for the speaker
|
2019-01-06 16:22:33 +01:00
|
|
|
elif request.method == "PATCH":
|
2018-02-06 14:52:47 +01:00
|
|
|
# Check permissions
|
2019-01-06 16:22:33 +01:00
|
|
|
if not has_perm(self.request.user, "agenda.can_manage_list_of_speakers"):
|
2018-02-06 14:52:47 +01:00
|
|
|
self.permission_denied(request)
|
|
|
|
|
|
|
|
# Retrieve user_id
|
2019-01-06 16:22:33 +01:00
|
|
|
user_id = request.data.get("user")
|
2018-02-06 14:52:47 +01:00
|
|
|
try:
|
|
|
|
user = get_user_model().objects.get(pk=int(user_id))
|
|
|
|
except (ValueError, get_user_model().DoesNotExist):
|
2019-01-12 23:01:42 +01:00
|
|
|
raise ValidationError({"detail": "User does not exist."})
|
2018-02-06 14:52:47 +01:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
marked = request.data.get("marked")
|
2018-02-06 14:52:47 +01:00
|
|
|
if not isinstance(marked, bool):
|
2019-01-12 23:01:42 +01:00
|
|
|
raise ValidationError({"detail": "Marked has to be a bool."})
|
2018-02-06 14:52:47 +01:00
|
|
|
|
2019-01-19 15:07:51 +01:00
|
|
|
queryset = Speaker.objects.filter(item=item, user=user, begin_time=None)
|
2018-02-06 14:52:47 +01:00
|
|
|
try:
|
2019-01-19 15:07:51 +01:00
|
|
|
# We assume that there aren't multiple entries for speakers that
|
|
|
|
# did not yet begin to speak, because this
|
2018-02-06 14:52:47 +01:00
|
|
|
# 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:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError(
|
2019-01-12 23:01:42 +01:00
|
|
|
{"detail": "The user is not in the list of speakers."}
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
2018-02-06 14:52:47 +01:00
|
|
|
else:
|
|
|
|
speaker.marked = marked
|
|
|
|
speaker.save()
|
|
|
|
|
2015-05-26 18:21:30 +02:00
|
|
|
else:
|
|
|
|
# request.method == 'DELETE'
|
2019-01-06 16:22:33 +01:00
|
|
|
speaker_ids = request.data.get("speaker")
|
2015-05-26 18:21:30 +02:00
|
|
|
|
|
|
|
# Check permissions and other conditions. Get speaker instance.
|
2016-07-07 13:33:18 +02:00
|
|
|
if speaker_ids is None:
|
2015-05-26 18:21:30 +02:00
|
|
|
# Remove oneself
|
|
|
|
queryset = Speaker.objects.filter(
|
2019-01-06 16:22:33 +01:00
|
|
|
item=item, user=self.request.user
|
|
|
|
).exclude(weight=None)
|
2015-05-26 18:21:30 +02:00
|
|
|
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:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError(
|
2019-01-12 23:01:42 +01:00
|
|
|
{"detail": "You are not on the list of speakers."}
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
2016-07-07 13:33:18 +02:00
|
|
|
else:
|
|
|
|
speaker.delete()
|
2015-05-26 18:21:30 +02:00
|
|
|
else:
|
|
|
|
# Remove someone else.
|
2019-01-06 16:22:33 +01:00
|
|
|
if not has_perm(
|
|
|
|
self.request.user, "agenda.can_manage_list_of_speakers"
|
|
|
|
):
|
2015-05-26 18:21:30 +02:00
|
|
|
self.permission_denied(request)
|
2019-01-12 23:01:42 +01:00
|
|
|
if isinstance(speaker_ids, int):
|
2016-07-07 13:33:18 +02:00
|
|
|
speaker_ids = [speaker_ids]
|
2017-01-15 10:49:48 +01:00
|
|
|
deleted_speaker_count = 0
|
2016-07-07 13:33:18 +02:00
|
|
|
for speaker_id in speaker_ids:
|
|
|
|
try:
|
|
|
|
speaker = Speaker.objects.get(pk=int(speaker_id))
|
|
|
|
except (ValueError, Speaker.DoesNotExist):
|
2017-01-15 10:49:48 +01:00
|
|
|
pass
|
|
|
|
else:
|
2017-03-21 11:06:05 +01:00
|
|
|
speaker.delete(skip_autoupdate=True)
|
2017-01-15 10:49:48 +01:00
|
|
|
deleted_speaker_count += 1
|
2017-03-21 11:06:05 +01:00
|
|
|
# send autoupdate if speakers are deleted
|
2019-01-12 23:01:42 +01:00
|
|
|
if deleted_speaker_count:
|
2017-03-21 11:06:05 +01:00
|
|
|
inform_changed_data(item)
|
|
|
|
|
2019-01-12 23:01:42 +01:00
|
|
|
return Response()
|
2015-05-27 15:42:32 +02:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@detail_route(methods=["PUT", "DELETE"])
|
2015-05-27 15:42:32 +02:00
|
|
|
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()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
if request.method == "PUT":
|
2015-05-27 15:42:32 +02:00
|
|
|
# Retrieve speaker_id
|
2019-01-06 16:22:33 +01:00
|
|
|
speaker_id = request.data.get("speaker")
|
2015-05-27 15:42:32 +02:00
|
|
|
if speaker_id is None:
|
|
|
|
speaker = item.get_next_speaker()
|
|
|
|
if speaker is None:
|
2019-01-12 23:01:42 +01:00
|
|
|
raise ValidationError({"detail": "The list of speakers is empty."})
|
2015-05-27 15:42:32 +02:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
speaker = Speaker.objects.get(pk=int(speaker_id))
|
|
|
|
except (ValueError, Speaker.DoesNotExist):
|
2019-01-12 23:01:42 +01:00
|
|
|
raise ValidationError({"detail": "Speaker does not exist."})
|
2015-09-04 18:24:41 +02:00
|
|
|
speaker.begin_speech()
|
2019-01-12 23:01:42 +01:00
|
|
|
message = "User is now speaking."
|
2015-05-27 15:42:32 +02:00
|
|
|
|
|
|
|
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.
|
2019-01-06 16:22:33 +01:00
|
|
|
current_speaker = (
|
|
|
|
Speaker.objects.filter(item=item, end_time=None)
|
|
|
|
.exclude(begin_time=None)
|
|
|
|
.get()
|
|
|
|
)
|
2015-05-27 15:42:32 +02:00
|
|
|
except Speaker.DoesNotExist:
|
|
|
|
raise ValidationError(
|
2019-01-06 16:22:33 +01:00
|
|
|
{
|
2019-01-12 23:01:42 +01:00
|
|
|
"detail": f"There is no one speaking at the moment according to {item}."
|
2019-01-06 16:22:33 +01:00
|
|
|
}
|
|
|
|
)
|
2015-09-04 18:24:41 +02:00
|
|
|
current_speaker.end_speech()
|
2019-01-12 23:01:42 +01:00
|
|
|
message = "The speech is finished now."
|
2015-05-26 18:21:30 +02:00
|
|
|
|
|
|
|
# Initiate response.
|
2019-01-06 16:22:33 +01:00
|
|
|
return Response({"detail": message})
|
2015-05-26 18:21:30 +02:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@detail_route(methods=["POST"])
|
2016-01-08 23:32:29 +01:00
|
|
|
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
|
2019-01-06 16:22:33 +01:00
|
|
|
speaker_ids = request.data.get("speakers")
|
2016-01-08 23:32:29 +01:00
|
|
|
if not isinstance(speaker_ids, list):
|
2019-01-12 23:01:42 +01:00
|
|
|
raise ValidationError({"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:
|
2019-01-12 23:01:42 +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
|
2016-12-06 12:21:29 +01:00
|
|
|
speaker.save(skip_autoupdate=True)
|
2015-12-19 00:29:20 +01:00
|
|
|
weight += 1
|
2016-01-08 23:32:29 +01:00
|
|
|
|
2016-12-06 12:21:29 +01:00
|
|
|
# send autoupdate
|
|
|
|
inform_changed_data(item)
|
|
|
|
|
2016-01-08 23:32:29 +01:00
|
|
|
# Initiate response.
|
2019-01-12 23:01:42 +01:00
|
|
|
return Response({"detail": "List of speakers successfully sorted."})
|
2016-01-08 23:32:29 +01:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@list_route(methods=["post"])
|
2015-11-23 17:21:32 +01:00
|
|
|
def numbering(self, request):
|
|
|
|
"""
|
|
|
|
Auto numbering of the agenda according to the config. Manually added
|
|
|
|
item numbers will be overwritten.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
if not config["agenda_enable_numbering"]:
|
|
|
|
raise ValidationError(
|
2019-01-12 23:01:42 +01:00
|
|
|
{"detail": "Numbering of agenda items is deactivated."}
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
2018-04-11 12:35:42 +02:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
Item.objects.number_all(numeral_system=config["agenda_numeral_system"])
|
2019-01-12 23:01:42 +01:00
|
|
|
return Response({"detail": "The agenda has been numbered."})
|
2017-02-01 14:23:59 +01:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@list_route(methods=["post"])
|
2017-02-01 14:23:59 +01:00
|
|
|
def sort(self, request):
|
|
|
|
"""
|
2017-02-17 17:20:13 +01:00
|
|
|
Sort agenda items. Also checks parent field to prevent hierarchical
|
|
|
|
loops.
|
2017-02-01 14:23:59 +01:00
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
nodes = request.data.get("nodes", [])
|
|
|
|
parent_id = request.data.get("parent_id")
|
2017-02-01 14:23:59 +01:00
|
|
|
items = []
|
|
|
|
with transaction.atomic():
|
|
|
|
for index, node in enumerate(nodes):
|
2019-01-06 16:22:33 +01:00
|
|
|
item = Item.objects.get(pk=node["id"])
|
2017-02-01 14:23:59 +01:00
|
|
|
item.parent_id = parent_id
|
|
|
|
item.weight = index
|
|
|
|
item.save(skip_autoupdate=True)
|
|
|
|
items.append(item)
|
2017-02-17 17:20:13 +01:00
|
|
|
|
|
|
|
# Now check consistency. TODO: Try to use less DB queries.
|
2019-01-06 16:22:33 +01:00
|
|
|
item = Item.objects.get(pk=node["id"])
|
2017-02-17 17:20:13 +01:00
|
|
|
ancestor = item.parent
|
|
|
|
while ancestor is not None:
|
|
|
|
if ancestor == item:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError(
|
|
|
|
{
|
2019-01-12 23:01:42 +01:00
|
|
|
"detail": "There must not be a hierarchical loop. Please reload the page."
|
2019-01-06 16:22:33 +01:00
|
|
|
}
|
|
|
|
)
|
2017-02-17 17:20:13 +01:00
|
|
|
ancestor = ancestor.parent
|
|
|
|
|
2017-02-01 14:23:59 +01:00
|
|
|
inform_changed_data(items)
|
2019-01-12 23:01:42 +01:00
|
|
|
return Response({"detail": "The agenda has been sorted."})
|
2018-11-27 22:35:19 +01:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@list_route(methods=["post"])
|
2018-11-27 22:35:19 +01:00
|
|
|
@transaction.atomic
|
|
|
|
def assign(self, request):
|
|
|
|
"""
|
|
|
|
Assign multiple agenda items to a new parent item.
|
|
|
|
|
|
|
|
Send POST {... see schema ...} to assign the new parent.
|
|
|
|
|
|
|
|
This aslo checks the parent field to prevent hierarchical loops.
|
|
|
|
"""
|
|
|
|
schema = {
|
|
|
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
|
|
"title": "Agenda items assign new parent schema",
|
|
|
|
"description": "An object containing an array of agenda item ids and the new parent id the items should be assigned to.",
|
|
|
|
"type": "object",
|
|
|
|
"propterties": {
|
|
|
|
"items": {
|
|
|
|
"description": "An array of agenda item ids where the items should be assigned to the new parent id.",
|
|
|
|
"type": "array",
|
2019-01-06 16:22:33 +01:00
|
|
|
"items": {"type": "integer"},
|
2018-11-27 22:35:19 +01:00
|
|
|
"minItems": 1,
|
|
|
|
"uniqueItems": True,
|
|
|
|
},
|
|
|
|
"parent_id": {
|
|
|
|
"description": "The agenda item id of the new parent item.",
|
|
|
|
"type": "integer",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"required": ["items", "parent_id"],
|
|
|
|
}
|
|
|
|
|
|
|
|
# Validate request data.
|
|
|
|
try:
|
|
|
|
jsonschema.validate(request.data, schema)
|
|
|
|
except jsonschema.ValidationError as err:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError({"detail": str(err)})
|
2018-11-27 22:35:19 +01:00
|
|
|
|
|
|
|
# Check parent item
|
|
|
|
try:
|
2019-01-06 16:22:33 +01:00
|
|
|
parent = Item.objects.get(pk=request.data["parent_id"])
|
2018-11-27 22:35:19 +01:00
|
|
|
except Item.DoesNotExist:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError(
|
2019-01-12 23:01:42 +01:00
|
|
|
{"detail": f"Parent item {request.data['parent_id']} does not exist"}
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
2018-11-27 22:35:19 +01:00
|
|
|
|
|
|
|
# Collect ancestors
|
2018-11-29 08:42:16 +01:00
|
|
|
ancestors = [parent.pk]
|
2018-11-27 22:35:19 +01:00
|
|
|
grandparent = parent.parent
|
|
|
|
while grandparent is not None:
|
|
|
|
ancestors.append(grandparent.pk)
|
|
|
|
grandparent = grandparent.parent
|
|
|
|
|
2018-11-29 08:42:16 +01:00
|
|
|
# First validate all items before changeing them.
|
|
|
|
items = []
|
2019-01-06 16:22:33 +01:00
|
|
|
for item_id in request.data["items"]:
|
2018-11-27 22:35:19 +01:00
|
|
|
# Prevent hierarchical loops.
|
2018-11-29 08:42:16 +01:00
|
|
|
if item_id in ancestors:
|
2019-01-06 16:22:33 +01:00
|
|
|
raise ValidationError(
|
|
|
|
{
|
2019-01-12 23:01:42 +01:00
|
|
|
"detail": f"Assigning item {item_id} to one of its children is not possible."
|
2019-01-06 16:22:33 +01:00
|
|
|
}
|
|
|
|
)
|
2018-11-27 22:35:19 +01:00
|
|
|
|
|
|
|
# Check every item
|
|
|
|
try:
|
2018-11-29 08:42:16 +01:00
|
|
|
items.append(Item.objects.get(pk=item_id))
|
2018-11-27 22:35:19 +01:00
|
|
|
except Item.DoesNotExist:
|
2019-01-12 23:01:42 +01:00
|
|
|
raise ValidationError({"detail": f"Item {item_id} does not exist"})
|
2018-11-27 22:35:19 +01:00
|
|
|
|
2018-11-29 08:42:16 +01:00
|
|
|
# OK, assign new parents.
|
|
|
|
for item in items:
|
2018-11-27 22:35:19 +01:00
|
|
|
# Assign new parent.
|
|
|
|
item.parent = parent
|
|
|
|
item.save(skip_autoupdate=True)
|
|
|
|
|
|
|
|
# Now inform all clients.
|
2018-11-29 08:42:16 +01:00
|
|
|
inform_changed_data(items)
|
2018-11-27 22:35:19 +01:00
|
|
|
|
|
|
|
# Send response.
|
2019-01-12 23:01:42 +01:00
|
|
|
return Response({"detail": f"{len(items)} items successfully assigned."})
|