def test_create_folders2_3(self): """ Create list of folders where some of the folder already exist :return: """ a = Folder.objects.create(name="a") b = Folder.objects.create(name="b", parent=a) base, folder = create_folders(["a", "b", "c.txt"], None) base, folder = create_folders(['a', 'b', 'c.txt'], a) self.assertEqual(Folder.objects.filter(name='a').count(), 2) self.assertEqual(Folder.objects.filter(parent=a).count(), 2)
def test_create_folders(self): """ Creat list of folders in root :return: """ base, folder = create_folders(["a", "b", "c.txt"], None) self.assertEqual(base, "c.txt") self.assertEqual(folder.name, "b") self.assertEqual(folder.parent.name, "a") self.assertEqual(folder.parent.parent, None)
def test_create_folders2(self): """ Create list of folders where some of the folder already exist :return: """ a = Folder.objects.create(name="a") base, folder = create_folders(["a", "b", "c.txt"], None) self.assertEqual(base, "c.txt") self.assertEqual(folder.name, "b") self.assertEqual(folder.parent, a) self.assertEqual(folder.parent.parent, None)
def test_create_folders3(self): """ Create list of folders where base has folders but doesn't match :return: """ a = Folder.objects.create(name="a") b = Folder.objects.create(name="b") base, folder = create_folders(["c", "b", "c.txt"], None) self.assertEqual(base, "c.txt") self.assertEqual(folder.name, "b") self.assertEqual(folder.parent.name, "c") self.assertEqual(folder.parent.parent, None)
def create(self, request, *args, **kwargs): file: InMemoryUploadedFile = request.data.get("file") parent: str = request.data.get("parent") parent_folder: Optional[Folder] = None if parent: parent_folder = Folder.objects.get(id=int(parent)) paths = request.data.get("paths") if file and paths: if has_parent(paths): paths = list(PurePath(paths).parts) base_name, folder = create_folders(paths, parent_folder) request.data['parent'] = str(folder.id) if folder else None serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=201, headers=headers)