shuup.core.basket package

Submodules

shuup.core.basket.command_dispatcher module

class shuup.core.basket.command_dispatcher.BasketCommandDispatcher(request, basket=None)[source]

Bases: object

BasketCommandDispatcher handles (usually AJAX) requests that somehow update the basket. You should never instantiate BasketCommandDispatcher yourself – instead use get_basket_command_dispatcher().

All handle_* methods are expected to accept **kwargs.

commands_module = <module 'shuup.core.basket.commands' from '/home/runner/work/shuup/shuup/shuup/core/basket/commands.py'>
__init__(request, basket=None)[source]
get_command_handler(command)[source]
handle(command, kwargs=None)[source]

Dispatch and handle processing of the given command.

Parameters:
  • command (unicode) – Name of command to run.

  • kwargs (dict) – Arguments to pass to the command handler. If empty, request.POST is used.

Returns:

response.

Return type:

HttpResponse

preprocess_kwargs(command, kwargs)[source]

Preprocess kwargs before they are passed to the given command handler. Useful for subclassing. Must return the new kwargs, even if it wasn’t mutated.

Parameters:
  • command – The name of the command about to be run.

  • kwargs – dict of arguments.

Returns:

dict of arguments.

postprocess_response(command, kwargs, response)[source]

Postprocess the response dictionary (not a HTTP response!) before it is either turned into JSON or otherwise processed (in the case of non-AJAX requests).

Parameters:
  • command – The command that was run.

  • kwargs – The actual kwargs the command was run with.

  • response – The response the command returned.

Returns:

The response to be processed and sent to the client.

shuup.core.basket.command_middleware module

class shuup.core.basket.command_middleware.BaseBasketCommandMiddleware[source]

Bases: object

A basket command middleware to pre-process the kwargs and post-process the response.

preprocess_kwargs(basket: BaseBasket, request: HttpRequest, command: str, kwargs: dict) dict[source]

Mutate the kwargs that will be passed to the handler. It is possible to raise a ValidationError exception if required.

postprocess_response(basket: BaseBasket, request: HttpRequest, command: str, kwargs: dict, response: dict) dict[source]

Mutate the response before it is returned by the command dispatcher.

shuup.core.basket.commands module

shuup.core.basket.commands.handle_add(request, basket, product_id, quantity=1, unit_type='internal', supplier_id=None, **kwargs)[source]

Handle adding a product to the basket.

Parameters:
  • product_id – product ID to add (or if child_product_id is truey, the parent ID).

  • quantity – quantity of products to add.

  • child_product_id – child product ID to add (if truey).

  • supplier_id – The supplier ID for the new line. If None, the first supplier is used.

shuup.core.basket.commands.handle_add_var(request, basket, product_id, quantity=1, unit_type='internal', **kwargs)[source]

Handle adding a complex variable product into the basket by resolving the combination variables. This actually uses kwargs, expecting var_XXX=YYY to exist there, where XXX is the PK of a ProductVariationVariable and YYY is the PK of a ProductVariationVariableValue. Confused yet?

Parameters:
  • quantity – Quantity of the resolved variation to add.

  • kwargs – Expected to contain var_* values, see above.

shuup.core.basket.commands.handle_del(request, basket, line_id, **kwargs)[source]

Handle deleting a distinct order line from the basket given its unique line ID.

Parameters:

line_id – The line ID to delete.

Returns:

shuup.core.basket.commands.handle_clear(request, basket, **kwargs)[source]

Handle fully clearing the basket.

shuup.core.basket.commands.handle_add_campaign_code(request, basket, code)[source]
shuup.core.basket.commands.handle_remove_campaign_code(request, basket, code)[source]
shuup.core.basket.commands.handle_clear_campaign_codes(request, basket)[source]
shuup.core.basket.commands.handle_set_customer(request, basket, customer, orderer=None)[source]
shuup.core.basket.commands.handle_update(request, basket, **kwargs)[source]

Handle updating a basket, i.e. deleting some lines or updating quantities.

This dispatches further to whatever is declared by the SHUUP_BASKET_UPDATE_METHODS_SPEC configuration entry.

shuup.core.basket.objects module

class shuup.core.basket.objects.BasketLine(source=None, **kwargs)[source]

Bases: SourceLine

Initialize SourceLine with a given source and data.

Parameters:
  • source (OrderSource) – The OrderSource this SourceLine belongs to.

  • kwargs – Data for the SourceLine.

__init__(source=None, **kwargs)[source]

Initialize SourceLine with a given source and data.

Parameters:
  • source (OrderSource) – The OrderSource this SourceLine belongs to.

  • kwargs – Data for the SourceLine.

property shop_product

ShopProduct object of this line.

Return type:

shuup.core.models.ShopProduct

cache_info(pricing_context)[source]
property type
set_quantity(quantity)[source]
property can_delete
property can_change_quantity
class shuup.core.basket.objects.BaseBasket(request, basket_name='basket', shop=None, **kwargs)[source]

Bases: OrderSource

__init__(request, basket_name='basket', shop=None, **kwargs)[source]
get_cache_key()[source]
uncache()[source]

Uncache processed lines.

Should be called after changing the contents and before (re)accessing lines with get_final_lines.

save()[source]

Persist any changes made into the basket to storage.

One does not usually need to directly call this; ShuupFrontMiddleware will usually take care of it.

delete()[source]

Clear and delete the basket data.

finalize()[source]

Mark the basket as “completed” (i.e. an order is created/a conversion made).

This will also clear the basket’s data.

clear_all()[source]

Clear all data for this basket.

property customer
property orderer
property shipping_address
property billing_address
property shipping_method
property payment_method
property customer_comment
extra_data
shipping_data
payment_data
add_line(**kwargs)[source]
create_line(**kwargs)[source]
add_code(code)[source]

Add a code to this OrderSource.

At this point it is expected that the customers permission to use the code has already been checked by the caller.

The code will be converted to text.

Parameters:

code (str) – The code to add.

Returns:

True if code was added, False if it was already there.

Return type:

bool

clear_codes()[source]

Remove all codes from this OrderSource.

Returns:

True if there were any codes before clearing.

Return type:

bool

remove_code(code)[source]

Remove a given code from this OrderSource.

Parameters:

code (str) – The code to remove.

Returns:

True if code was removed, False if code was not there.

Return type:

bool

property is_empty
get_unorderable_lines()[source]
get_lines()[source]

Get unprocessed lines in this OrderSource.

See also get_final_lines.

clean_empty_lines()[source]
add_product(supplier, shop, product, quantity, force_new_line=False, extra=None, parent_line=None)[source]
refresh_lines()[source]

Refresh lines and recalculating prices.

update_line(data_line, **kwargs)[source]
add_product_with_child_product(supplier, shop, product, child_product, quantity)[source]
delete_line(line_id)[source]
get_basket_line(line_id)[source]

Get basket line by line id.

Return type:

BasketLine

find_line_by_line_id(line_id)[source]

Find basket data line by line id.

Return type:

dict

find_lines_by_parent_line_id(parent_line_id)[source]

Find basket data lines by parent line id.

Return type:

Iterable[dict]

property orderable
get_methods_validation_errors()[source]
get_validation_errors()[source]
get_product_ids_and_quantities()[source]
get_available_shipping_methods()[source]

Get available shipping methods.

Return type:

list[ShippingMethod]

get_available_payment_methods()[source]

Get available payment methods.

Return type:

list[PaymentMethod]

add_log_entry(message, extra=None, kind=LogEntryKind.NOTE)[source]

Log errors to basket storage

get_log_entries()[source]
class shuup.core.basket.objects.Basket(request, basket_name='basket', shop=None, **kwargs)[source]

Bases: BaseBasket

shuup.core.basket.order_creator module

class shuup.core.basket.order_creator.BasketOrderCreator(request=None)[source]

Bases: OrderCreator

Initialize order creator.

Parameters:

request (django.http.HttpRequest|None) – Optional request object for backward compatibility. Passing non-None value is DEPRECATED.

shuup.core.basket.storage module

exception shuup.core.basket.storage.BasketCompatibilityError[source]

Bases: Exception

class shuup.core.basket.storage.BasketStorage[source]

Bases: object

load(basket)[source]

Load the given basket’s data dictionary from the storage.

Return type:

dict

Raises:

BasketCompatibilityError if basket loaded from the storage is not compatible with the requested basket.

abstractmethod save(basket, data)[source]

Save the given data dictionary into the storage for the given basket.

Rtype str:

Returns:

The unique identifier of the basket just created

abstractmethod delete(basket)[source]

Delete the basket from storage.

finalize(basket)[source]

Mark the basket as “finalized” (i.e. completed) in the storage.

The actual semantics of what finalization does are up to each backend.

basket_exists(key, shop)[source]

Check if basket exists in the storage.

For example this is used from API to check whether the basket actually exists for certain shop when accessed with key.

class shuup.core.basket.storage.BaseDatabaseBasketStorage[source]

Bases: BasketStorage

model

alias of Basket

get_basket_kwargs(basket)[source]
save(basket, data)[source]
delete(basket)[source]

Delete the basket from storage.

finalize(basket)[source]

Mark the basket as “finalized” (i.e. completed) in the storage.

The actual semantics of what finalization does are up to each backend.

basket_exists(key, shop)[source]

Check if basket exists in the storage.

For example this is used from API to check whether the basket actually exists for certain shop when accessed with key.

class shuup.core.basket.storage.DatabaseBasketStorage[source]

Bases: BaseDatabaseBasketStorage

get_basket_kwargs(basket)[source]
shuup.core.basket.storage.get_storage()[source]

Retrieve a basket storage object.

Returns:

A basket storage object.

Return type:

BasketStorage

shuup.core.basket.update_methods module

class shuup.core.basket.update_methods.BasketUpdateMethods(request, basket)[source]

Bases: object

Initialize.

__init__(request, basket)[source]

Initialize.

get_prefix_to_method_map()[source]

Override this method to link prefixes with their associated methods to call.

Format of the dictionary is: { FIELD_NAME_PREFIX: METHOD }.

METHOD is a function which accepts the keyword arguments given in update_basket_contents. It should perform the necessary changes to the basket_line and then return whether the value had changed or not. (See update_quantity or delete_line for examples.)

delete_line(line, **kwargs)[source]
update_display_quantity(line, value, **kwargs)[source]
update_quantity(line, value, **kwargs)[source]

Module contents

shuup.core.basket.get_basket_order_creator(request=None)[source]
shuup.core.basket.get_basket_view()[source]
shuup.core.basket.get_basket_command_dispatcher(request)[source]
Return type:

shuup.front.basket.command_dispatcher.BasketCommandDispatcher

shuup.core.basket.get_basket(request, basket_name='basket', basket_class=None)[source]

Get the basket cached in the request or create and cache a new one.

The basket_class is used when creating a new basket, i.e. when the request doesn’t already have a basket cached with the given name. If no basket_class is given, will load a class using the SHUUP_BASKET_CLASS_SPEC setting.

Return type:

shuup.front.basket.objects.BaseBasket