OpenSlides/openslides/utils/views.py

144 lines
5.3 KiB
Python
Raw Normal View History

Replaces the old `angular2tree` with a custom drag&drop tree Calculates the direction of the moving. Finishes the moving of nodes in same level Adds some style Sets the padding dynamically Adds placeholder depends on the horizontal movement Set the placeholder at the correct place, so the user can see, where he will drop the moved node Finishes moving of nodes - Old parents change their option to expand. - New parents change their option to expand. - If the user moves a node between nodes with a higher level, the node will be moved to the next index with same or lower level. Fixes the visibility of moved node - If the new parent is not visible, the moved node will not be seen. If the user moves an expanded node, the new parent should expanded, too, if it's not already. Sending successfully data to the server - Sorting the items Handles moving nodes between parent and children - If the user moves a node between a parent and its children, the children will be relinked to the moved node as their new parent. Replaces the old `sorting-tree` to a new one - The new `sorted-tree` replaces the old `sorting-tree`. - The old package `angular-tree-component` was removed. - The user will only see the buttons to save or cancel his changes, if he made changes. - The buttons, that do not work currently, were removed. Adds a guard to check if the user made changes. - If the user made changes but he has not saved them, then there is a dialog that will prompt to ask for confirmation. Before cancelling the changes the user has to confirm this.
2019-02-22 12:04:36 +01:00
from typing import Any, Dict, List, Set
2017-08-23 20:51:06 +02:00
Replaces the old `angular2tree` with a custom drag&drop tree Calculates the direction of the moving. Finishes the moving of nodes in same level Adds some style Sets the padding dynamically Adds placeholder depends on the horizontal movement Set the placeholder at the correct place, so the user can see, where he will drop the moved node Finishes moving of nodes - Old parents change their option to expand. - New parents change their option to expand. - If the user moves a node between nodes with a higher level, the node will be moved to the next index with same or lower level. Fixes the visibility of moved node - If the new parent is not visible, the moved node will not be seen. If the user moves an expanded node, the new parent should expanded, too, if it's not already. Sending successfully data to the server - Sorting the items Handles moving nodes between parent and children - If the user moves a node between a parent and its children, the children will be relinked to the moved node as their new parent. Replaces the old `sorting-tree` to a new one - The new `sorted-tree` replaces the old `sorting-tree`. - The old package `angular-tree-component` was removed. - The user will only see the buttons to save or cancel his changes, if he made changes. - The buttons, that do not work currently, were removed. Adds a guard to check if the user made changes. - If the user made changes but he has not saved them, then there is a dialog that will prompt to ask for confirmation. Before cancelling the changes the user has to confirm this.
2019-02-22 12:04:36 +01:00
from django.db import models, transaction
from rest_framework.views import APIView as _APIView
2012-02-20 17:46:45 +01:00
Replaces the old `angular2tree` with a custom drag&drop tree Calculates the direction of the moving. Finishes the moving of nodes in same level Adds some style Sets the padding dynamically Adds placeholder depends on the horizontal movement Set the placeholder at the correct place, so the user can see, where he will drop the moved node Finishes moving of nodes - Old parents change their option to expand. - New parents change their option to expand. - If the user moves a node between nodes with a higher level, the node will be moved to the next index with same or lower level. Fixes the visibility of moved node - If the new parent is not visible, the moved node will not be seen. If the user moves an expanded node, the new parent should expanded, too, if it's not already. Sending successfully data to the server - Sorting the items Handles moving nodes between parent and children - If the user moves a node between a parent and its children, the children will be relinked to the moved node as their new parent. Replaces the old `sorting-tree` to a new one - The new `sorted-tree` replaces the old `sorting-tree`. - The old package `angular-tree-component` was removed. - The user will only see the buttons to save or cancel his changes, if he made changes. - The buttons, that do not work currently, were removed. Adds a guard to check if the user made changes. - If the user made changes but he has not saved them, then there is a dialog that will prompt to ask for confirmation. Before cancelling the changes the user has to confirm this.
2019-02-22 12:04:36 +01:00
from .rest_api import Response, ValidationError
class APIView(_APIView):
"""
The Django Rest framework APIView with improvements for OpenSlides.
"""
2018-08-22 22:00:08 +02:00
http_method_names: List[str] = []
"""
The allowed actions have to be explicitly defined.
Django allowes the following:
http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
"""
2017-08-24 12:26:55 +02:00
def get_context_data(self, **context: Any) -> Dict[str, Any]:
"""
Returns the context for the response.
"""
return context
2017-08-24 12:26:55 +02:00
def method_call(self, request: Any, *args: Any, **kwargs: Any) -> Any:
"""
Http method that returns the response object with the context data.
"""
return Response(self.get_context_data())
# Add the http-methods and delete the method "method_call"
get = post = put = patch = delete = head = options = trace = method_call
del method_call
Replaces the old `angular2tree` with a custom drag&drop tree Calculates the direction of the moving. Finishes the moving of nodes in same level Adds some style Sets the padding dynamically Adds placeholder depends on the horizontal movement Set the placeholder at the correct place, so the user can see, where he will drop the moved node Finishes moving of nodes - Old parents change their option to expand. - New parents change their option to expand. - If the user moves a node between nodes with a higher level, the node will be moved to the next index with same or lower level. Fixes the visibility of moved node - If the new parent is not visible, the moved node will not be seen. If the user moves an expanded node, the new parent should expanded, too, if it's not already. Sending successfully data to the server - Sorting the items Handles moving nodes between parent and children - If the user moves a node between a parent and its children, the children will be relinked to the moved node as their new parent. Replaces the old `sorting-tree` to a new one - The new `sorted-tree` replaces the old `sorting-tree`. - The old package `angular-tree-component` was removed. - The user will only see the buttons to save or cancel his changes, if he made changes. - The buttons, that do not work currently, were removed. Adds a guard to check if the user made changes. - If the user made changes but he has not saved them, then there is a dialog that will prompt to ask for confirmation. Before cancelling the changes the user has to confirm this.
2019-02-22 12:04:36 +01:00
class TreeSortMixin:
"""
Provides a handler for sorting a model tree.
"""
def sort_tree(
self, request: Any, model: models.Model, weight_key: str, parent_id_key: str
) -> None:
"""
Sorts the all model objects represented in a tree of ids. The request data should
be a list (the root) of all main models. Each node is a dict with an id and optional children:
Replaces the old `angular2tree` with a custom drag&drop tree Calculates the direction of the moving. Finishes the moving of nodes in same level Adds some style Sets the padding dynamically Adds placeholder depends on the horizontal movement Set the placeholder at the correct place, so the user can see, where he will drop the moved node Finishes moving of nodes - Old parents change their option to expand. - New parents change their option to expand. - If the user moves a node between nodes with a higher level, the node will be moved to the next index with same or lower level. Fixes the visibility of moved node - If the new parent is not visible, the moved node will not be seen. If the user moves an expanded node, the new parent should expanded, too, if it's not already. Sending successfully data to the server - Sorting the items Handles moving nodes between parent and children - If the user moves a node between a parent and its children, the children will be relinked to the moved node as their new parent. Replaces the old `sorting-tree` to a new one - The new `sorted-tree` replaces the old `sorting-tree`. - The old package `angular-tree-component` was removed. - The user will only see the buttons to save or cancel his changes, if he made changes. - The buttons, that do not work currently, were removed. Adds a guard to check if the user made changes. - If the user made changes but he has not saved them, then there is a dialog that will prompt to ask for confirmation. Before cancelling the changes the user has to confirm this.
2019-02-22 12:04:36 +01:00
{
id: <the id>
children: [
<children, optional>
]
}
Every id has to be given.
This function traverses this tree in preorder to assign the weight. So even if a client
does not have every model, the remaining models are sorted correctly.
Replaces the old `angular2tree` with a custom drag&drop tree Calculates the direction of the moving. Finishes the moving of nodes in same level Adds some style Sets the padding dynamically Adds placeholder depends on the horizontal movement Set the placeholder at the correct place, so the user can see, where he will drop the moved node Finishes moving of nodes - Old parents change their option to expand. - New parents change their option to expand. - If the user moves a node between nodes with a higher level, the node will be moved to the next index with same or lower level. Fixes the visibility of moved node - If the new parent is not visible, the moved node will not be seen. If the user moves an expanded node, the new parent should expanded, too, if it's not already. Sending successfully data to the server - Sorting the items Handles moving nodes between parent and children - If the user moves a node between a parent and its children, the children will be relinked to the moved node as their new parent. Replaces the old `sorting-tree` to a new one - The new `sorted-tree` replaces the old `sorting-tree`. - The old package `angular-tree-component` was removed. - The user will only see the buttons to save or cancel his changes, if he made changes. - The buttons, that do not work currently, were removed. Adds a guard to check if the user made changes. - If the user made changes but he has not saved them, then there is a dialog that will prompt to ask for confirmation. Before cancelling the changes the user has to confirm this.
2019-02-22 12:04:36 +01:00
"""
if not isinstance(request.data, list):
raise ValidationError("The data must be a list.")
# get all item ids to verify, that the user send all ids.
all_model_ids = set(model.objects.all().values_list("pk", flat=True))
ids_found: Set[int] = set() # Set to save all found ids.
fake_root: Dict[str, Any] = {"id": None, "children": []}
fake_root["children"].extend(
request.data
) # this will prevent mutating the request data.
Replaces the old `angular2tree` with a custom drag&drop tree Calculates the direction of the moving. Finishes the moving of nodes in same level Adds some style Sets the padding dynamically Adds placeholder depends on the horizontal movement Set the placeholder at the correct place, so the user can see, where he will drop the moved node Finishes moving of nodes - Old parents change their option to expand. - New parents change their option to expand. - If the user moves a node between nodes with a higher level, the node will be moved to the next index with same or lower level. Fixes the visibility of moved node - If the new parent is not visible, the moved node will not be seen. If the user moves an expanded node, the new parent should expanded, too, if it's not already. Sending successfully data to the server - Sorting the items Handles moving nodes between parent and children - If the user moves a node between a parent and its children, the children will be relinked to the moved node as their new parent. Replaces the old `sorting-tree` to a new one - The new `sorted-tree` replaces the old `sorting-tree`. - The old package `angular-tree-component` was removed. - The user will only see the buttons to save or cancel his changes, if he made changes. - The buttons, that do not work currently, were removed. Adds a guard to check if the user made changes. - If the user made changes but he has not saved them, then there is a dialog that will prompt to ask for confirmation. Before cancelling the changes the user has to confirm this.
2019-02-22 12:04:36 +01:00
# The stack where all nodes to check are saved. Invariant: Each node
# must be a dict with an id, a parent id (may be None for the root
# layer) and a weight.
nodes_to_check = [fake_root]
Replaces the old `angular2tree` with a custom drag&drop tree Calculates the direction of the moving. Finishes the moving of nodes in same level Adds some style Sets the padding dynamically Adds placeholder depends on the horizontal movement Set the placeholder at the correct place, so the user can see, where he will drop the moved node Finishes moving of nodes - Old parents change their option to expand. - New parents change their option to expand. - If the user moves a node between nodes with a higher level, the node will be moved to the next index with same or lower level. Fixes the visibility of moved node - If the new parent is not visible, the moved node will not be seen. If the user moves an expanded node, the new parent should expanded, too, if it's not already. Sending successfully data to the server - Sorting the items Handles moving nodes between parent and children - If the user moves a node between a parent and its children, the children will be relinked to the moved node as their new parent. Replaces the old `sorting-tree` to a new one - The new `sorted-tree` replaces the old `sorting-tree`. - The old package `angular-tree-component` was removed. - The user will only see the buttons to save or cancel his changes, if he made changes. - The buttons, that do not work currently, were removed. Adds a guard to check if the user made changes. - If the user made changes but he has not saved them, then there is a dialog that will prompt to ask for confirmation. Before cancelling the changes the user has to confirm this.
2019-02-22 12:04:36 +01:00
# Traverse and check, if every id is given, valid and there are no duplicate ids.
2019-06-03 17:04:30 +02:00
# The weight values are 2, 4, 6, 8,... to "make space" between entries. This is
# some work around for the agenda: If one creates a content object with an item
# and gives the item's parent, than the weight can be set to the parent's one +1.
# If multiple content objects witht he same parent are created, the ordering is not
# guaranteed.
weight = 2
Replaces the old `angular2tree` with a custom drag&drop tree Calculates the direction of the moving. Finishes the moving of nodes in same level Adds some style Sets the padding dynamically Adds placeholder depends on the horizontal movement Set the placeholder at the correct place, so the user can see, where he will drop the moved node Finishes moving of nodes - Old parents change their option to expand. - New parents change their option to expand. - If the user moves a node between nodes with a higher level, the node will be moved to the next index with same or lower level. Fixes the visibility of moved node - If the new parent is not visible, the moved node will not be seen. If the user moves an expanded node, the new parent should expanded, too, if it's not already. Sending successfully data to the server - Sorting the items Handles moving nodes between parent and children - If the user moves a node between a parent and its children, the children will be relinked to the moved node as their new parent. Replaces the old `sorting-tree` to a new one - The new `sorted-tree` replaces the old `sorting-tree`. - The old package `angular-tree-component` was removed. - The user will only see the buttons to save or cancel his changes, if he made changes. - The buttons, that do not work currently, were removed. Adds a guard to check if the user made changes. - If the user made changes but he has not saved them, then there is a dialog that will prompt to ask for confirmation. Before cancelling the changes the user has to confirm this.
2019-02-22 12:04:36 +01:00
while len(nodes_to_check) > 0:
node = nodes_to_check.pop()
id = node["id"]
if id is not None: # exclude the fake_root
node[weight_key] = weight
2019-06-03 17:04:30 +02:00
weight += 2
if id in ids_found:
raise ValidationError(f"Duplicate id: {id}")
if id not in all_model_ids:
raise ValidationError(f"Id does not exist: {id}")
ids_found.add(id)
Replaces the old `angular2tree` with a custom drag&drop tree Calculates the direction of the moving. Finishes the moving of nodes in same level Adds some style Sets the padding dynamically Adds placeholder depends on the horizontal movement Set the placeholder at the correct place, so the user can see, where he will drop the moved node Finishes moving of nodes - Old parents change their option to expand. - New parents change their option to expand. - If the user moves a node between nodes with a higher level, the node will be moved to the next index with same or lower level. Fixes the visibility of moved node - If the new parent is not visible, the moved node will not be seen. If the user moves an expanded node, the new parent should expanded, too, if it's not already. Sending successfully data to the server - Sorting the items Handles moving nodes between parent and children - If the user moves a node between a parent and its children, the children will be relinked to the moved node as their new parent. Replaces the old `sorting-tree` to a new one - The new `sorted-tree` replaces the old `sorting-tree`. - The old package `angular-tree-component` was removed. - The user will only see the buttons to save or cancel his changes, if he made changes. - The buttons, that do not work currently, were removed. Adds a guard to check if the user made changes. - If the user made changes but he has not saved them, then there is a dialog that will prompt to ask for confirmation. Before cancelling the changes the user has to confirm this.
2019-02-22 12:04:36 +01:00
# Add children, if exist.
if isinstance(node.get("children"), list):
node["children"].reverse()
for child in node["children"]:
Replaces the old `angular2tree` with a custom drag&drop tree Calculates the direction of the moving. Finishes the moving of nodes in same level Adds some style Sets the padding dynamically Adds placeholder depends on the horizontal movement Set the placeholder at the correct place, so the user can see, where he will drop the moved node Finishes moving of nodes - Old parents change their option to expand. - New parents change their option to expand. - If the user moves a node between nodes with a higher level, the node will be moved to the next index with same or lower level. Fixes the visibility of moved node - If the new parent is not visible, the moved node will not be seen. If the user moves an expanded node, the new parent should expanded, too, if it's not already. Sending successfully data to the server - Sorting the items Handles moving nodes between parent and children - If the user moves a node between a parent and its children, the children will be relinked to the moved node as their new parent. Replaces the old `sorting-tree` to a new one - The new `sorted-tree` replaces the old `sorting-tree`. - The old package `angular-tree-component` was removed. - The user will only see the buttons to save or cancel his changes, if he made changes. - The buttons, that do not work currently, were removed. Adds a guard to check if the user made changes. - If the user made changes but he has not saved them, then there is a dialog that will prompt to ask for confirmation. Before cancelling the changes the user has to confirm this.
2019-02-22 12:04:36 +01:00
# ensure invariant for nodes_to_check
if not isinstance(child, dict) or not isinstance(
child.get("id"), int
Replaces the old `angular2tree` with a custom drag&drop tree Calculates the direction of the moving. Finishes the moving of nodes in same level Adds some style Sets the padding dynamically Adds placeholder depends on the horizontal movement Set the placeholder at the correct place, so the user can see, where he will drop the moved node Finishes moving of nodes - Old parents change their option to expand. - New parents change their option to expand. - If the user moves a node between nodes with a higher level, the node will be moved to the next index with same or lower level. Fixes the visibility of moved node - If the new parent is not visible, the moved node will not be seen. If the user moves an expanded node, the new parent should expanded, too, if it's not already. Sending successfully data to the server - Sorting the items Handles moving nodes between parent and children - If the user moves a node between a parent and its children, the children will be relinked to the moved node as their new parent. Replaces the old `sorting-tree` to a new one - The new `sorted-tree` replaces the old `sorting-tree`. - The old package `angular-tree-component` was removed. - The user will only see the buttons to save or cancel his changes, if he made changes. - The buttons, that do not work currently, were removed. Adds a guard to check if the user made changes. - If the user made changes but he has not saved them, then there is a dialog that will prompt to ask for confirmation. Before cancelling the changes the user has to confirm this.
2019-02-22 12:04:36 +01:00
):
raise ValidationError(
"child must be a dict with an id as integer"
Replaces the old `angular2tree` with a custom drag&drop tree Calculates the direction of the moving. Finishes the moving of nodes in same level Adds some style Sets the padding dynamically Adds placeholder depends on the horizontal movement Set the placeholder at the correct place, so the user can see, where he will drop the moved node Finishes moving of nodes - Old parents change their option to expand. - New parents change their option to expand. - If the user moves a node between nodes with a higher level, the node will be moved to the next index with same or lower level. Fixes the visibility of moved node - If the new parent is not visible, the moved node will not be seen. If the user moves an expanded node, the new parent should expanded, too, if it's not already. Sending successfully data to the server - Sorting the items Handles moving nodes between parent and children - If the user moves a node between a parent and its children, the children will be relinked to the moved node as their new parent. Replaces the old `sorting-tree` to a new one - The new `sorted-tree` replaces the old `sorting-tree`. - The old package `angular-tree-component` was removed. - The user will only see the buttons to save or cancel his changes, if he made changes. - The buttons, that do not work currently, were removed. Adds a guard to check if the user made changes. - If the user made changes but he has not saved them, then there is a dialog that will prompt to ask for confirmation. Before cancelling the changes the user has to confirm this.
2019-02-22 12:04:36 +01:00
)
child[parent_id_key] = id
nodes_to_check.append(child)
if len(all_model_ids) != len(ids_found):
Replaces the old `angular2tree` with a custom drag&drop tree Calculates the direction of the moving. Finishes the moving of nodes in same level Adds some style Sets the padding dynamically Adds placeholder depends on the horizontal movement Set the placeholder at the correct place, so the user can see, where he will drop the moved node Finishes moving of nodes - Old parents change their option to expand. - New parents change their option to expand. - If the user moves a node between nodes with a higher level, the node will be moved to the next index with same or lower level. Fixes the visibility of moved node - If the new parent is not visible, the moved node will not be seen. If the user moves an expanded node, the new parent should expanded, too, if it's not already. Sending successfully data to the server - Sorting the items Handles moving nodes between parent and children - If the user moves a node between a parent and its children, the children will be relinked to the moved node as their new parent. Replaces the old `sorting-tree` to a new one - The new `sorted-tree` replaces the old `sorting-tree`. - The old package `angular-tree-component` was removed. - The user will only see the buttons to save or cancel his changes, if he made changes. - The buttons, that do not work currently, were removed. Adds a guard to check if the user made changes. - If the user made changes but he has not saved them, then there is a dialog that will prompt to ask for confirmation. Before cancelling the changes the user has to confirm this.
2019-02-22 12:04:36 +01:00
raise ValidationError(
f"Did not recieved {len(all_model_ids)} ids, got {len(ids_found)}."
Replaces the old `angular2tree` with a custom drag&drop tree Calculates the direction of the moving. Finishes the moving of nodes in same level Adds some style Sets the padding dynamically Adds placeholder depends on the horizontal movement Set the placeholder at the correct place, so the user can see, where he will drop the moved node Finishes moving of nodes - Old parents change their option to expand. - New parents change their option to expand. - If the user moves a node between nodes with a higher level, the node will be moved to the next index with same or lower level. Fixes the visibility of moved node - If the new parent is not visible, the moved node will not be seen. If the user moves an expanded node, the new parent should expanded, too, if it's not already. Sending successfully data to the server - Sorting the items Handles moving nodes between parent and children - If the user moves a node between a parent and its children, the children will be relinked to the moved node as their new parent. Replaces the old `sorting-tree` to a new one - The new `sorted-tree` replaces the old `sorting-tree`. - The old package `angular-tree-component` was removed. - The user will only see the buttons to save or cancel his changes, if he made changes. - The buttons, that do not work currently, were removed. Adds a guard to check if the user made changes. - If the user made changes but he has not saved them, then there is a dialog that will prompt to ask for confirmation. Before cancelling the changes the user has to confirm this.
2019-02-22 12:04:36 +01:00
)
# Do the actual update:
Replaces the old `angular2tree` with a custom drag&drop tree Calculates the direction of the moving. Finishes the moving of nodes in same level Adds some style Sets the padding dynamically Adds placeholder depends on the horizontal movement Set the placeholder at the correct place, so the user can see, where he will drop the moved node Finishes moving of nodes - Old parents change their option to expand. - New parents change their option to expand. - If the user moves a node between nodes with a higher level, the node will be moved to the next index with same or lower level. Fixes the visibility of moved node - If the new parent is not visible, the moved node will not be seen. If the user moves an expanded node, the new parent should expanded, too, if it's not already. Sending successfully data to the server - Sorting the items Handles moving nodes between parent and children - If the user moves a node between a parent and its children, the children will be relinked to the moved node as their new parent. Replaces the old `sorting-tree` to a new one - The new `sorted-tree` replaces the old `sorting-tree`. - The old package `angular-tree-component` was removed. - The user will only see the buttons to save or cancel his changes, if he made changes. - The buttons, that do not work currently, were removed. Adds a guard to check if the user made changes. - If the user made changes but he has not saved them, then there is a dialog that will prompt to ask for confirmation. Before cancelling the changes the user has to confirm this.
2019-02-22 12:04:36 +01:00
nodes_to_update = []
nodes_to_update.extend(
request.data
) # this will prevent mutating the request data.
with transaction.atomic():
while len(nodes_to_update) > 0:
node = nodes_to_update.pop()
id = node["id"]
weight = node[weight_key]
parent_id = node[parent_id_key]
db_node = model.objects.get(pk=id)
db_parent_id = getattr(db_node, parent_id_key, None)
db_weight = getattr(db_node, weight_key, -1)
# Just update, if some attribute was changed
if db_parent_id != parent_id or db_weight != weight:
setattr(db_node, parent_id_key, parent_id)
setattr(db_node, weight_key, weight)
db_node.save()
Replaces the old `angular2tree` with a custom drag&drop tree Calculates the direction of the moving. Finishes the moving of nodes in same level Adds some style Sets the padding dynamically Adds placeholder depends on the horizontal movement Set the placeholder at the correct place, so the user can see, where he will drop the moved node Finishes moving of nodes - Old parents change their option to expand. - New parents change their option to expand. - If the user moves a node between nodes with a higher level, the node will be moved to the next index with same or lower level. Fixes the visibility of moved node - If the new parent is not visible, the moved node will not be seen. If the user moves an expanded node, the new parent should expanded, too, if it's not already. Sending successfully data to the server - Sorting the items Handles moving nodes between parent and children - If the user moves a node between a parent and its children, the children will be relinked to the moved node as their new parent. Replaces the old `sorting-tree` to a new one - The new `sorted-tree` replaces the old `sorting-tree`. - The old package `angular-tree-component` was removed. - The user will only see the buttons to save or cancel his changes, if he made changes. - The buttons, that do not work currently, were removed. Adds a guard to check if the user made changes. - If the user made changes but he has not saved them, then there is a dialog that will prompt to ask for confirmation. Before cancelling the changes the user has to confirm this.
2019-02-22 12:04:36 +01:00
# Add children, if exist.
children = node.get("children")
if isinstance(children, list):
nodes_to_update.extend(children)
return Response()