Skip to content

Commit

Permalink
refactor: empty all make more sense as a classmethod
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobi-De committed Mar 30, 2023
1 parent fb96999 commit 6480c3b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
12 changes: 6 additions & 6 deletions dj_shop_cart/cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,6 @@ def empty(self, clear_metadata: bool = True) -> None:
self._metadata.clear()
self.storage.save(data)

def empty_all(self) -> None:
"""Empty all carts, prefixed or not."""
self._items = []
self._metadata = {}
self.storage.clear()

def variants_group_by_product(self) -> dict[str, list[CartItem]]:
"""
Return a dictionary with the products ids as keys and a list of variant as values.
Expand Down Expand Up @@ -306,6 +300,12 @@ def new(cls, request: HttpRequest, prefix: str = DEFAULT_CART_PREFIX) -> Cart:
instance._metadata = metadata
return instance

@classmethod
def empty_all(cls, request: HttpRequest) -> None:
"""Empty all carts, prefixed or not."""
storage = get_module(conf.CART_STORAGE_BACKEND)(request)
storage.clear()


def get_cart_class() -> type[Cart]:
"""
Expand Down
8 changes: 7 additions & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ cart.empty()
```
This method take no arguments.

### Additional Properties of the cart object
### Attributes of a `Cart` instance

```python
def my_view(request):
Expand Down Expand Up @@ -123,12 +123,18 @@ def my_view(request):
- **unique_count** : The number of unique items in the cart, regardless of the quantity.
- **products** : A list of associated products.
- **metadata** : A dictionary containing the metadata of the cart.
- **empty(clear_metadata=True)** : Empty the cart. Takes an optional argument `clear_metadata` that defaults to `True`, if set to `False` the metadata of the cart will not be cleared.
- **update_metadata(metadata:dict)** : Update the metadata of the cart.
- **clear_metadata(\*keys:list[str])** : Clear the metadata of the cart. Takes an optional list of keys to clear, if no keys are specified, all metadata is cleared.
- **find(\*\*criteria)** : Returns a list of cart items matching the given criteria.
- **find_one(\*\*criteria)** : Returns the first cart item that matches the given criteria, if no match is found return None.
- **variants_group_by_product()** : Return a dictionary with the products ids as keys and a list of variant as values.

### Classmethods of `Cart`

- **new(request:HttpRequest, prefix="default")** : Create a new cart instance and load existing data from the storage backend.
- **empty_all(request:HttpRequest)** : Empty all carts for the current user.

## Multiple Carts

You can manage multiple carts at the same time with the same storage using the `prefix` argument of the `Cart.new` method.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dj-shop-cart"
version = "6.1.0"
version = "7.0.0"
description = "Simple django cart manager for your django projects."
authors = ["Tobi DEGNON <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit 6480c3b

Please sign in to comment.