Simple django cart manager for your django projects.
Project description
dj-shop-cart
A simple and flexible cart manager for your django projects.
Features
- Add, remove, decrement and clear items from cart
- Authenticated users cart can be saved to database
- Write custom methods to easily hook into the items add / remove flow
- Custom get_price method to ensure that the cart always have an up-to-date products price
- Each item in the cart hold a reference to the associated product
- Metadata data can be attached to cart items
- Supports specification of product variation details
- Available context processor for easy access to the user cart in all your django templates
- Swappable backend storage, with session and database provided by default
Installation
Install dj-shop-cart with pip or poetry.
pip install dj-shop-cart
Usage/Examples
# settings.py
TEMPLATES = [
{
"OPTIONS": {
"context_processors": [
...,
"dj_shop_cart.context_processors.cart", # If you want access to the cart instance in all templates
],
},
}
]
# models.py
from django.db import models
from dj_shop_cart.cart import CartItem
from decimal import Decimal
class Product(models.Model):
...
def get_price(self, item:CartItem)->Decimal:
"""The only requirements of the dj_shop_cart package apart from the fact that the products you add
to the cart must be instances of django based models. You can use a different name for this method
but be sure to update the corresponding setting (see Configuration). Even if you change the name the
function signature should match this one.
"""
# views.py
from dj_shop_cart.cart import get_cart_class
from django.http import HttpRequest
from django.views.decorators.http import require_POST
# This function has nothing to do with the package itself
from .helpers import collect_params
Cart = get_cart_class()
@require_POST
def add_product(request: HttpRequest):
product, quantity = collect_params(request)
cart = Cart.new(request)
cart.add(product, quantity=quantity)
...
@require_POST
def remove_product(request: HttpRequest):
product, quantity = collect_params(request)
cart = Cart.new(request)
cart.remove(product, quantity=quantity)
...
@require_POST
def empty_cart(request: HttpRequest):
Cart.new(request).empty()
...
Configuration
Configure the cart behaviour in your Django settings. All settings are optional and must be strings if defined.
Name | Description | Default |
---|---|---|
CART_SESSION_KEY | The key used to store the cart in session | CART-ID |
CART_CLASS | The path to the Cart class to use. If you are using a custom class it must subclass dj_shop_cart.cart.Cart | dj_shop_cart.cart.Cart |
CART_PRODUCT_GET_PRICE_METHOD | The method name to use to dynamically get the price on the product instance | get_price |
CART_STORAGE_BACKEND | The path to the storage backend to use. If you define a custom storage backend, it should follow the Storage protocol, see the Backend Storage section below | dj_shop_cart.storages.SessionStorage |
API reference
TODO
Storage Backend
TODO
Used By
This project is used by the following companies:
Development
Poetry is required (not really, you can set up the environment however you want and install the requirements manually) to set up a virtualenv, install it then run the following:
poetry install
pre-commit install --install-hooks
Tests can then be run quickly in that environment:
pytest
Feedback
If you have any feedback, please reach out to me at degnonfrancis@gmail.com
Todos
- More examples
- Add an example for a custom redis backend
- Complete the example project
- Write more tests
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
dj-shop-cart-4.0.0.tar.gz
(10.1 kB
view hashes)
Built Distribution
Close
Hashes for dj_shop_cart-4.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67caefd63932c6061960a48ca46e0af9839ad94fb4cc24fe73771999be76d993 |
|
MD5 | b7103017cb7aee32ec5b393ad7d1ee70 |
|
BLAKE2b-256 | b18b49f92c014ac8c7b2ef3bf96b5e2e78acbeeca089d9ceb6379692d285c067 |