Skip to main content

A simple shopping cart for Django.

Project description

dj-cart

Introduction

This is not a session based cart system. Session based carts are not suitable for REST endpoints because REST is stateless. This implementation stores the contents of the cart in the database.

Prerequisites

  • Django 1.1+
  • django content type framework in your INSTALLED_APPS

Installation

pip install dj-cart

After installation is complete:

add 'cart' to your INSTALLED_APPS 

Run

python manage.py migrate

Usage

from cart.models import Cart
from myproducts.models import Product


class CartAdd(APIView):
    def get_object(self, product_id):
        try:
            return Product.objects.get(pk=product_id)
        except Product.DoesNotExist:
            raise Http404

    def post(self, request, product_id):
        quantity = request.data.get('quantity')
        product = self.get_object(product_id)
        cart, created = Cart.objects.get_or_create(user=self.request.user)
        cart.add_item(product, product.price, quantity)
        return Response(status=status.HTTP_201_CREATED)


class CartRemove(APIView):
      def get_object(self, product_id):
        try:
            return Product.objects.get(pk=product_id)
        except Product.DoesNotExist:
            raise Http404

    def put(self, request, product_id):
        product = self.get_object(product_id)
        cart = get_object_or_404(Cart, user=self.request.user)
        cart.remove_item(product)
        return Response(status=status.HTTP_204_NO_CONTENT)


class CartDetail(APIView):

    def get(self, request):
        try:
            cart = Cart.objects.get(patient=self.request.user.patient)
        except Cart.DoesNotExist:
            cart = Cart.objects.create(patient=self.request.user.patient)
        return Response(cart.serializable(), status=status.HTTP_200_OK)

Testing

pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dj_cart-1.0.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file dj_cart-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: dj_cart-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.6

File hashes

Hashes for dj_cart-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bd203a8816da1790ec094beb586b3a27aae89cb54d17b9239fef8dd49f59ed33
MD5 39fa830b8c5fe97d86cd9ee7cb5449f1
BLAKE2b-256 593ab9a026191a70825c6f866cdadde5df408097e7d60f33dd218d3ec4421842

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page