Models

shopkit.core.models

class shopkit.core.models.AddressBase(*args, **kwargs)

Bases: django.db.models.base.Model

Base class for address models.

This base class should be used when defining addresses.

class shopkit.core.models.CartBase(*args, **kwargs)

Bases: shopkit.core.basemodels.AbstractPricedItemBase

Abstract base class for shopping carts.

add_item(product, quantity=1, **kwargs)

Adds the specified product in the specified quantity to the current shopping Cart. This effectively creates a CartItem for the Product-Cart combination or updates it when a CartItem already exists.

When kwargs are specified, these are passed along to get_item and signify properties of the CartItem.

Returns:added CartItem
classmethod from_request(request)

Get an existing Cart object from the session or return a blank one.

Returns:Cart object corresponding with this request
get_item(product, create=True, **kwargs)

Either instantiates and returns a CartItem for the Cart-Product combination or fetches it from the database. The creation is lazy: the resulting CartItem is not automatically saved.

Parameters:
  • create – Whether or not to create a new object if no object was found.
  • kwargs – If kwargs are specified, these signify filters or instantiation parameters for getting or creating the item.
get_items()

Gets items from the cart with a quantity > 0.

get_order_line()

Get a string representation of this OrderItem for use in list views.

get_price(**kwargs)

Wraps the get_total_price function.

get_total_items()

Gets the total quantity of products in the shopping cart.

Todo

Use aggregation here.

get_total_price(**kwargs)

Gets the total price for all items in the cart.

remove_item(product, **kwargs)

Remove item from cart.

Returns:True if the item was deleted succesfully, False if the item could not be found.
to_request(request)

Store a reference to the current Cart object in the session.

class shopkit.core.models.CartItemBase(*args, **kwargs)

Bases: shopkit.core.basemodels.AbstractPricedItemBase, shopkit.core.basemodels.QuantizedItemBase

Abstract base class for shopping cart items.

get_order_line()

Natural (unicode) representation of this cart item in an order overview.

get_parent()

Get the relevant Cart. Used to have a generic API for Carts and Orders.

get_piece_price(**kwargs)

Gets the price per piece for a given quantity of items.

get_price(**kwargs)

Wraps get_total_price().

get_total_price(**kwargs)

Gets the tatal price for the items in the cart.

class shopkit.core.models.CustomerAddressBase(*args, **kwargs)

Bases: django.db.models.base.Model

Base class for addresses with a relation to a customer, for which the addressee field is automatically set when saving.

save(**kwargs)

Default the addressee to the full name of the user if none has been specified explicitly.

class shopkit.core.models.CustomerCartBase(*args, **kwargs)

Bases: shopkit.core.models.CartBase

Abstract base class for shopping carts related to a Customer.

classmethod from_request(request)

Get cart from request and associate with customer, if related to the authenticated user.

class shopkit.core.models.CustomerOrderBase(*args, **kwargs)

Bases: shopkit.core.models.OrderBase

Abstract base class for orders with Customer management.

classmethod from_cart(cart)

Make sure we copy the customer from the Cart, if available.

class shopkit.core.models.CustomerPaymentBase(*args, **kwargs)

Bases: shopkit.core.models.PaymentBase

Abstract base class for payment related to a Customer.

class shopkit.core.models.OrderBase(*args, **kwargs)

Bases: shopkit.core.basemodels.AbstractPricedItemBase, shopkit.core.basemodels.DatedItemBase

Abstract base class for orders.

confirm()

Method which performs actions to be taken upon order confirmation.

By default, this method writes a log message and calls the register_confirmation method on all order items. It also deletes to shopping cart this order was created from.

Subclasses can use this to perform actions such as updating the stock or registering the use of a discount. When overriding, make sure this method calls its supermethods.

When subclassing this method, please make sure you implement proper safety checks in the overrides of the prepare_confirm() method as this method should not raise errors under normal circumstances as this could lead to potential data/state inconsistencies.

In general, it makes sense to connect this method to a change in order state such that it is called automatically. For example:

..todo::
Write a code example here.
classmethod from_cart(cart)

Instantiate an order based on the basis of a shopping cart, copying all the items.

get_items()

Get all order items (with a quantity greater than 0).

get_price(**kwargs)

Wraps the get_total_price function.

get_total_items()

Gets the total quantity of products in the shopping cart.

Todo

Use aggregation here.

get_total_price(**kwargs)

Gets the total price for all items in the order.

prepare_confirm()

Run necessary checks in order to confirm whether an order can be safely confirmed. By default this method only checks whether or not the order has already been confirmed, but could be potentially overridden by methods checking the item’s stock etcetera.

Raises:AlreadyConfirmedException
save(*args, **kwargs)

Make sure we log a state change where applicable.

class shopkit.core.models.OrderItemBase(*args, **kwargs)

Bases: shopkit.core.basemodels.AbstractPricedItemBase, shopkit.core.basemodels.QuantizedItemBase

Abstract base class for order items. An OrderItem should, ideally, copy all specific properties from the shopping cart as an order should not change at all when the objects they relate to change.

confirm()

Register confirmation of the current OrderItem. This can be overridden in subclasses to perform functionality such as stock keeping or discount usage administration. By default it merely emits a debug message.

When overriding, be sure to call the superclass.

classmethod from_cartitem(cartitem, order)

Create and populate an order item from a shopping cart item. The result is not automatically saved.

When the CartItem model has extra properties, such as variations, these should be copied over to the OrderItem in overrides of this function as follows:

class OrderItem(...):
    @classmethod
    def from_cartitem(cls, cartitem, order):
        orderitem = super(OrderItem, cls).from_cartitem(
            cartitem, order
        )

        orderitem.<someproperty> = cartitem.<someproperty>

        return orderitem
get_parent()

Get the relevant Order. Used to have a generic API for Carts and Orders.

get_piece_price(**kwargs)

Gets the price per piece for a given quantity of items.

get_price(**kwargs)

Wraps get_total_price().

get_total_price(**kwargs)

Gets the tatal price for the items in the cart.

class shopkit.core.models.OrderStateChangeBase(*args, **kwargs)

Bases: django.db.models.base.Model

Abstract base class for logging order state changes.

classmethod get_latest(order)

Get the latest state change for a particular order, or None if no StateChange is available.

class shopkit.core.models.PaymentBase(*args, **kwargs)

Bases: django.db.models.base.Model

Abstract base class for payments.

class shopkit.core.models.ProductBase(*args, **kwargs)

Bases: shopkit.core.basemodels.AbstractPricedItemBase

Abstract base class for products in the webshop.

The in_shop property should be a Manager containing all the Product objects which should be enabled in the shop’s frontend.

class shopkit.core.models.UserCustomerBase(*args, **kwargs)

Bases: shopkit.core.basemodels.AbstractCustomerBase, django.contrib.auth.models.User

Abstract base class for Customers which can also be Django users.