Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helper methods for deleting file groups with their contents #248

Merged
merged 4 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pyuploadcare.egg-info/
temp.db
uploadcare.ini
build/
dist/
docs/_build
Expand Down
4 changes: 3 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ The format is based on [Keep a
Changelog](https://keepachangelog.com/en/1.0.0/), and this project
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [4.0.x] - unreleased
## Unreleased

### Added

- For `Uploadcare` and `UploadAPI`:
- `upload_from_url` and `upload_from_url_sync` can now accept two new optional parameters: `check_duplicates` and `save_duplicates`. These correspond to `check_URL_duplicates` and `save_URL_duplicates` for [`/from_url/` upload API endpoint](https://uploadcare.com/api-refs/upload-api/#tag/Upload/operation/fromURLUpload) respectively.
- For `FileGroup`:
- The `delete` method now includes an optional `delete_files` argument, which indicates whether the files within the specified group should also be deleted.

### Changed

Expand Down
4 changes: 4 additions & 0 deletions docs/core_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ Delete file groups::
file_group: FileGroup = uploadcare.file_group('0513dda0-582f-447d-846f-096e5df9e2bb~2')
file_group.delete()

To delete a file group and all the files it contains::

file_group: FileGroup = uploadcare.file_group('0513dda0-582f-447d-846f-096e5df9e2bb~2')
file_group.delete(delete_files=True)

Create webhook
--------------
Expand Down
4 changes: 2 additions & 2 deletions pyuploadcare/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ def store_files(self, files: Iterable[Union[str, UUID, "File"]]) -> None:
for chunk in iterate_over_batches(uuids, self.batch_chunk_size):
self.files_api.batch_store(chunk)

def delete_files(self, files: Iterable[Union[str, "File"]]) -> None:
def delete_files(self, files: Iterable[Union[str, UUID, "File"]]) -> None:
"""Deletes multiple files by requesting Uploadcare API.

Usage example::
Expand Down Expand Up @@ -666,7 +666,7 @@ def list_files(

Returns ``FileList`` instance providing iteration over all uploaded files.

Args:m
Args:
- ``starting_point`` -- a starting point for filtering files.
It is reflects a ``from`` parameter from REST API.
- ``ordering`` -- a string with name of the field what must be used
Expand Down
19 changes: 16 additions & 3 deletions pyuploadcare/resources/file_group.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re
from datetime import datetime
from typing import TYPE_CHECKING, Any, Dict, Optional
from typing import TYPE_CHECKING, Any, Dict, Iterable, Iterator, Optional

import dateutil.parser

Expand All @@ -13,6 +13,7 @@

if TYPE_CHECKING:
from pyuploadcare.client import Uploadcare
from pyuploadcare.resources.file import File


GROUP_ID_REGEX = re.compile(
Expand All @@ -27,7 +28,7 @@
)


class FileGroup:
class FileGroup(Iterable):
"""File Group resource for working with user-uploaded group of files.

It can take group id or group CDN url::
Expand Down Expand Up @@ -92,6 +93,12 @@ def __getitem__(self, key):
if file_info is not None:
return self._client.file(file_info["uuid"], file_info)

def __iter__(self) -> Iterator["File"]:
for i in range(len(self)):
file_ = self[i]
if file_:
yield file_

@property
def cdn_url(self):
"""Returns group's CDN url.
Expand Down Expand Up @@ -213,13 +220,19 @@ def is_deleted(self):
"""
return self._is_deleted

def delete(self):
def delete(self, delete_files: bool = False):
"""Delete group itself, left files unchanged

Added in API v. 0.7.0

Args:
- ``delete_files`` -- ``True`` do also delete files in this group.
"""
if self._is_deleted:
return

if delete_files:
self._client.delete_files(file_ for file_ in self)

self._client.groups_api.delete(self.id)
self._is_deleted = True
Loading