def duplicate_nodes(request): logging.debug("Entering the copy_node endpoint") if request.method != 'POST': return HttpResponseBadRequest( "Only POST requests are allowed on this endpoint.") else: data = json.loads(request.body) try: nodes = data["nodes"] sort_order = data.get("sort_order") or 1 channel_id = data["channel_id"] new_nodes = [] target_parent = ContentNode.objects.get(pk=data["target_parent"]) channel = target_parent.get_channel() request.user.can_edit(channel and channel.pk) nodes_being_copied = [] for node_data in nodes: nodes_being_copied.append( ContentNode.objects.get(pk=node_data['id'])) record_node_duplication_stats( nodes_being_copied, ContentNode.objects.get(pk=target_parent.pk), Channel.objects.get(pk=channel_id)) with transaction.atomic(): with ContentNode.objects.disable_mptt_updates(): for node_data in nodes: new_node = _duplicate_node_bulk(node_data['id'], sort_order=sort_order, parent=target_parent, channel_id=channel_id, user=request.user) new_nodes.append(new_node.pk) sort_order += 1 except KeyError: raise ObjectDoesNotExist( "Missing attribute from data: {}".format(data)) serialized = ContentNodeSerializer( ContentNode.objects.filter(pk__in=new_nodes), many=True).data return HttpResponse(JSONRenderer().render(serialized))
def duplicate_node_inline(request): logging.debug("Entering the copy_node endpoint") if request.method != 'POST': return HttpResponseBadRequest( "Only POST requests are allowed on this endpoint.") else: data = json.loads(request.body) try: node = ContentNode.objects.get(pk=data["node_id"]) channel_id = data["channel_id"] target_parent = ContentNode.objects.get(pk=data["target_parent"]) channel = target_parent.get_channel() request.user.can_edit(channel and channel.pk) record_node_duplication_stats( [node], ContentNode.objects.get(pk=target_parent.pk), Channel.objects.get(pk=channel_id)) new_node = None with transaction.atomic(): with ContentNode.objects.disable_mptt_updates(): sort_order = ( node.sort_order + node.get_next_sibling().sort_order ) / 2 if node.get_next_sibling() else node.sort_order + 1 new_node = _duplicate_node_bulk(node, sort_order=sort_order, parent=target_parent, channel_id=channel_id, user=request.user) if not new_node.title.endswith(_(" (Copy)")): new_node.title = new_node.title + _(" (Copy)") new_node.save() return HttpResponse(JSONRenderer().render( ContentNodeSerializer( ContentNode.objects.filter(pk=new_node.pk), many=True).data)) except KeyError: raise ObjectDoesNotExist( "Missing attribute from data: {}".format(data))