def put(self, request, node_pk): """ If you put with a right_id, then your node will be placed immediately to the right of the node corresponding with the right_id. If you put with a parent_id, then your node will be placed as the first-child of the node corresponding with the parent_id. .. todo:: put this in the model """ node = get_object_or_404(Node, pk=node_pk) data = self.data() if not self.site.has_change_permission(request, node.content): raise PermissionDenied(_("You don't have permission to move this widget.")) if not node.content.draggable: raise InvalidTreeMovement({'message': "You can't move me"}) try: right = Node.objects.get(pk=extract_id(data['right_id'])) node.content.reposition(self.site, right=right.content) except Node.DoesNotExist: try: parent = Node.objects.get(pk=extract_id(data['parent_id'])) node.content.reposition(self.site, parent=parent.content) except Node.DoesNotExist: raise Http404 return self.render_as_node(None, status=200)
def post(self, request, node_pk=None): data = self.data() app_label, model = data['__class__'].split('.') try: content_class = get_model(app_label, model) if content_class is None: # BBB Django < 1.7 returns None instead of raising LookupError raise LookupError except LookupError: raise Http404 try: right = get_object_or_404(Node, pk=extract_id(data['right_id'])) parent = right.get_parent() create_content = right.content.add_sibling except Http404: parent = get_object_or_404(Node, pk=extract_id(data['parent_id'])) create_content = parent.content.add_child if not self.site.has_add_permission(request, parent.content, content_class): raise PermissionDenied(_("You don't have permission to add this widget.")) content = create_content(self.site, content_class) return self.render_as_node(content.node.to_json(self.site), status=201)
def render_as_node(self, obj, *args, **kwargs): obj = {'node': obj} compatibility_node_url = self.request.GET.get('include_compatibility_for', None) if compatibility_node_url: node = get_object_or_404(Node, pk=extract_id(compatibility_node_url)) obj['compatibility'] = ShelfView.get_compatibility_data(self.site, self.request, node) return self.render_to_response(obj, *args, **kwargs)
def post(self, request, node_pk=None): data = self.data() app_label, model = data['__class__'].split('.') content_class = get_model(app_label, model) if not content_class: raise Http404 if not self.site.has_add_permission(request, content_class): raise PermissionDenied(_("You don't have permission to add this widget.")) try: right = get_object_or_404(Node, pk=extract_id(data['right_id'])) content = right.content.add_sibling(self.site, content_class) except Http404: parent = get_object_or_404(Node, pk=extract_id(data['parent_id'])) content = parent.content.add_child(self.site, content_class) return self.render_as_node(content.node.to_json(self.site), status=201)
def post(self, request, node_pk=None): data = self.data() app_label, model = data['__class__'].split('.') try: content_class = get_model(app_label, model) if content_class is None: # BBB Django < 1.7 returns None instead of raising LookupError raise LookupError except LookupError: raise Http404 if not self.site.has_add_permission(request, content_class): raise PermissionDenied( _("You don't have permission to add this widget.")) try: right = get_object_or_404(Node, pk=extract_id(data['right_id'])) content = right.content.add_sibling(self.site, content_class) except Http404: parent = get_object_or_404(Node, pk=extract_id(data['parent_id'])) content = parent.content.add_child(self.site, content_class) return self.render_as_node(content.node.to_json(self.site), status=201)
def post(self, request, node_pk=None): data = self.data() app_label, model = data['__class__'].split('.') try: content_class = apps.get_model(app_label, model) except LookupError: raise Http404 try: right = get_object_or_404(Node, pk=extract_id(data['right_id'])) parent = right.get_parent() create_content = right.content.add_sibling except Http404: parent = get_object_or_404(Node, pk=extract_id(data['parent_id'])) create_content = parent.content.add_child if not self.site.has_add_permission(request, parent.content, content_class): raise PermissionDenied(_("You don't have permission to add this widget.")) content = create_content(self.site, content_class) return self.render_as_node(content.node.to_json(self.site), status=201)
def put(self, request, node_pk): """ If you put with a right_id, then your node will be placed immediately to the right of the node corresponding with the right_id. If you put with a parent_id, then your node will be placed as the first-child of the node corresponding with the parent_id. .. todo:: put this in the model """ node = get_object_or_404(Node, pk=node_pk) data = self.data() if not self.site.has_change_permission(request, node.content): raise PermissionDenied( _("You don't have permission to move this widget.")) if not node.content.draggable: raise InvalidTreeMovement({'message': "You can't move me"}) try: right = Node.objects.get(pk=extract_id(data['right_id'])) node.content.reposition(self.site, right=right.content) except Node.DoesNotExist: try: parent = Node.objects.get(pk=extract_id(data['parent_id'])) node.content.reposition(self.site, parent=parent.content) except Node.DoesNotExist: raise Http404 # We have to refetch before returning because treebeard doesn't # update the in-memory instance, only the database, see # <https://tabo.pe/projects/django-treebeard/docs/tip/caveats.html#raw-queries> node = Node.objects.get(pk=node.pk) node.prefetch_tree() return self.render_as_node(node.to_json(self.site), status=200)
def put(self, request, node_pk): """ If you put with a right_id, then your node will be placed immediately to the right of the node corresponding with the right_id. If you put with a parent_id, then your node will be placed as the first-child of the node corresponding with the parent_id. .. todo:: put this in the model """ node = get_object_or_404(Node, pk=node_pk) data = self.data() if not self.site.has_change_permission(request, node.content): raise PermissionDenied(_("You don't have permission to move this widget.")) if not node.content.draggable: raise InvalidTreeMovement({"message": "You can't move me"}) try: right = Node.objects.get(pk=extract_id(data["right_id"])) node.content.reposition(self.site, right=right.content) except Node.DoesNotExist: try: parent = Node.objects.get(pk=extract_id(data["parent_id"])) node.content.reposition(self.site, parent=parent.content) except Node.DoesNotExist: raise Http404 # We have to refetch before returning because treebeard doesn't # update the in-memory instance, only the database, see # <https://tabo.pe/projects/django-treebeard/docs/tip/caveats.html#raw-queries> node = Node.objects.get(pk=node.pk) node.prefetch_tree() return self.render_as_node(node.to_json(self.site), status=200)