def nodes_view(request): if request.method == "POST": data = json.loads(request.body) node_ids = [item['id'] for item in data] queryset = BaseTreeNode.objects.filter(id__in=node_ids) for node in queryset: # is used allowd to delete ? if not request.user.has_perm(Access.PERM_DELETE, node): # if user does not have delete permission on # one node - forbid entire operation! msg = f"{request.user.username} does not have" +\ f" permission to delete {node.title}" return HttpResponseForbidden(json.dumps({'msg': msg}), content_type="application/json") # yes, user is allowed to delete all nodes, # proceed with delete opration recursive_delete(queryset) return HttpResponse(json.dumps({'msg': 'OK'}), content_type="application/json") return HttpResponse(json.dumps({'msg': 'OK'}), content_type="application/json")
def test_delete_folder_with_document(self): folder_A = Folder.objects.create(title="A", user=self.user) folder_B = Folder.objects.create( title="B", user=self.user, ) doc = Document.create_document( title="document.pdf", file_name="document.pdf", size='1212', lang='DEU', user=self.user, parent_id=folder_A.id, page_count=5, ) doc.save() BaseTreeNode.objects.move_node(folder_B, folder_A) folder_A.refresh_from_db() # at this point, folder_A will have 2 descendants: # * folder B # * document.pdf descendants_count = folder_A.get_descendants( include_self=False).count() self.assertEqual(2, descendants_count) recursive_delete(folder_A) # by now everything should be deleted self.assertEqual(0, BaseTreeNode.objects.count())
def nodes_view(request): """ GET or POST /nodes/ """ if request.method == "POST": data = json.loads(request.body) node_ids = [item['id'] for item in data] queryset = BaseTreeNode.objects.filter(id__in=node_ids) automates = Automate.objects.filter( dst_folder__in=queryset ) if automates.count(): msg = _( "Following Automates have references to folders " "you are trying to delete: " ) msg += ", ".join([auto.name for auto in automates]) msg += _( ". Please delete mentioned Automates frist." ) return msg, HttpResponseBadRequest.status_code nodes_perms = request.user.get_perms_dict( queryset, Access.ALL_PERMS ) node_titles = [] for node in queryset: node_titles.append( node.title ) if not nodes_perms[node.id].get(Access.PERM_DELETE, False): # if user does not have delete permission on # one node - forbid entire operation! msg = f"{request.user.username} does not have" +\ f" permission to delete {node.title}" return msg, HttpResponseForbidden.status_code # yes, user is allowed to delete all nodes, # proceed with delete opration recursive_delete(queryset) signals.nodes_deleted.send( sender='core.views.nodes.nodes_view', user_id=request.user.id, level=logging.INFO, message=_("Nodes deleted"), node_titles=node_titles, node_ids=node_ids ) return 'OK' return 'OK'
def test_delete_folders_and_documents_recursively(self): """ User should be able to delete folder A in following structure: Home | | A / \ / \ doc1.pdf subfolder / \ / \ B doc2.pdf basically this test asserts correct functionality of papermerge.core.models.utils.recursive_delete function """ folder_A = Folder.objects.create(title="A", user=self.user) subfolder = Folder.objects.create(title="subfolder", user=self.user, parent_id=folder_A.id) doc = Document.create_document( title="document.pdf", file_name="document.pdf", size='1212', lang='DEU', user=self.user, parent_id=folder_A.id, page_count=5, ) doc.save() Folder.objects.create(title="B", user=self.user, parent_id=subfolder.id) doc = Document.create_document( title="document.pdf", file_name="document.pdf", size='1212', lang='DEU', user=self.user, parent_id=subfolder.id, page_count=5, ) doc.save() self.assertEqual(5, BaseTreeNode.objects.count()) # no exceptions here recursive_delete(folder_A) self.assertEqual(0, BaseTreeNode.objects.count())
def nodes_view(request): if request.method == "POST": data = json.loads(request.body) node_ids = [item['id'] for item in data] queryset = BaseTreeNode.objects.filter(id__in=node_ids) recursive_delete(queryset) return HttpResponse(json.dumps({'msg': 'OK'}), content_type="application/json") return HttpResponse(json.dumps({'msg': 'OK'}), content_type="application/json")