From 6480c3bb327dcff3ccb4b8dcd7f2d821faf9b632 Mon Sep 17 00:00:00 2001 From: Tobi DEGNON Date: Thu, 30 Mar 2023 10:11:54 +0100 Subject: [PATCH] refactor: empty all make more sense as a classmethod --- dj_shop_cart/cart.py | 12 ++++++------ docs/usage.md | 8 +++++++- pyproject.toml | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/dj_shop_cart/cart.py b/dj_shop_cart/cart.py index b597942..5902125 100644 --- a/dj_shop_cart/cart.py +++ b/dj_shop_cart/cart.py @@ -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. @@ -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]: """ diff --git a/docs/usage.md b/docs/usage.md index 01597d3..e1ed3d4 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -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): @@ -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. diff --git a/pyproject.toml b/pyproject.toml index a618271..ba91434 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] license = "MIT"