Merge pull request #2985 from normanjaeckel/AgendaSortCheck

Added check for hierarchical loops in agenda sort view. See #2972.
This commit is contained in:
Norman Jäckel 2017-02-17 22:08:23 +01:00 committed by GitHub
commit e35e65b7d4
3 changed files with 26 additions and 2 deletions

View File

@ -585,6 +585,7 @@ angular.module('OpenSlidesApp.agenda.site', [
$scope.items = AgendaTree.getTree(Agenda.getAll()); $scope.items = AgendaTree.getTree(Agenda.getAll());
}); });
$scope.showInternalItems = true; $scope.showInternalItems = true;
$scope.alert = {};
// save parent and weight of moved agenda item (and all items on same level) // save parent and weight of moved agenda item (and all items on same level)
$scope.treeOptions = { $scope.treeOptions = {
@ -594,7 +595,15 @@ angular.module('OpenSlidesApp.agenda.site', [
if (event.dest.nodesScope.item) { if (event.dest.nodesScope.item) {
parentID = event.dest.nodesScope.item.id; parentID = event.dest.nodesScope.item.id;
} }
$http.post('/rest/agenda/item/sort/', {nodes: event.dest.nodesScope.$modelValue, parent_id: parentID}); $http.post('/rest/agenda/item/sort/', {
nodes: event.dest.nodesScope.$modelValue,
parent_id: parentID}
).then(
function(success) {},
function(error){
$scope.alert = {type: 'danger', msg: error.data.detail, show: true};
}
);
} }
}; };
} }

View File

@ -19,6 +19,10 @@
<translate ng-if="!showInternalItems">Show internal items</translate> <translate ng-if="!showInternalItems">Show internal items</translate>
</button> </button>
<div uib-alert ng-show="alert.show" ng-class="'alert-' + (alert.type || 'warning')" ng-click="alert={}" close="alert={}">
{{ alert.msg }}
</div>
<div ui-tree="treeOptions" id="tree-root"> <div ui-tree="treeOptions" id="tree-root">
<ol ui-tree-nodes ng-model="items"> <ol ui-tree-nodes ng-model="items">
<li ng-repeat="item in items" ui-tree-node ng-include="'nodes_renderer.html'"> <li ng-repeat="item in items" ui-tree-node ng-include="'nodes_renderer.html'">

View File

@ -241,7 +241,8 @@ class ItemViewSet(ListModelMixin, RetrieveModelMixin, UpdateModelMixin, GenericV
@list_route(methods=['post']) @list_route(methods=['post'])
def sort(self, request): def sort(self, request):
""" """
Sort agenda items. Sort agenda items. Also checks parent field to prevent hierarchical
loops.
""" """
nodes = request.data.get('nodes', []) nodes = request.data.get('nodes', [])
parent_id = request.data.get('parent_id') parent_id = request.data.get('parent_id')
@ -253,5 +254,15 @@ class ItemViewSet(ListModelMixin, RetrieveModelMixin, UpdateModelMixin, GenericV
item.weight = index item.weight = index
item.save(skip_autoupdate=True) item.save(skip_autoupdate=True)
items.append(item) items.append(item)
# Now check consistency. TODO: Try to use less DB queries.
item = Item.objects.get(pk=node['id'])
ancestor = item.parent
while ancestor is not None:
if ancestor == item:
raise ValidationError({'detail': _(
'There must not be a hierarchical loop. Please reload the page.')})
ancestor = ancestor.parent
inform_changed_data(items) inform_changed_data(items)
return Response({'detail': _('The agenda has been sorted.')}) return Response({'detail': _('The agenda has been sorted.')})