shop.services.hooks

Customization / hook service

Register own implementations (Customization) to influence a specific behavior (Hook) of the viur-shop.

Unlike events, which are just a trigger, hooks can (and usually should) modify objects and return something.

Attributes

Classes

Hook

The hook'able events / actions.

Customization

Abstract base class for own implementations.

HookService

Note: Methods should be called only with positional arguments. Or keyword-only if really necessary.

Module Contents

shop.services.hooks.logger
class shop.services.hooks.Hook

Bases: enum.IntEnum

The hook’able events / actions.

Initialize self. See help(type(self)) for accurate signature.

ORDER_ASSIGN_UID

Hook that assign a order id on a OrderSkel type: (order_skel: SkeletonInstance_T[OrderSkel]) -> SkeletonInstance_T[OrderSkel]

CURRENT_COUNTRY

Provide the country of a global site context type: (context: t.Literal[“cart”, “article”, “vat_rate”]) -> str

ORDER_ADD_ADDITION

Do some additional modifications on the OrderSkel on order_add action. Called in viur.shop.modules.order.Order.order_add() before saving with Skeleton.write(). type: (order_skel: SkeletonInstance_T[OrderSkel]) -> SkeletonInstance_T[OrderSkel]

ORDER_UPDATE_ADDITION

Do some additional modifications on the OrderSkel on order_update action. Called in viur.shop.modules.order.Order.order_update() before saving with Skeleton.write(). type: (order_skel: SkeletonInstance_T[OrderSkel]) -> SkeletonInstance_T[OrderSkel]

ORDER_CHECKOUT_START_ADDITION

Do some additional modifications on the OrderSkel on checkout_start action. Called in viur.shop.modules.order.Order.checkout_start() before saving with Skeleton.write(). type: (order_skel: SkeletonInstance_T[OrderSkel]) -> SkeletonInstance_T[OrderSkel]

PAYMENT_RETURN_HANDLER_SUCCESS

The action that is executed after the customer has returned from the payment provider and the payment has been successful. This can be, for example, a rendered template or a redirect to another page. type: (order_skel: SkeletonInstance_T[OrderSkel], payment_data: t.Any) -> t.Any

PAYMENT_RETURN_HANDLER_ERROR

The action that is executed after the customer has returned from the payment provider and the payment has failed. This can be, for example, a rendered template or a redirect to another page. type: (order_skel: SkeletonInstance_T[OrderSkel], payment_data: t.Any) -> t.Any

class shop.services.hooks.Customization

Bases: abc.ABC

Abstract base class for own implementations.

property kind: Hook
Abstractmethod:

Return type:

Hook

The action this implementation is for

abstract __call__(*args, **kwargs)

The main logic of this implementation

Return type:

Any

__repr__()
Return type:

str

classmethod from_method(func, kind)

Just a handy variant to define an implementation without a class definition

Parameters:
  • func (Callable)

  • kind (Hook)

Return type:

Self

class shop.services.hooks.HookService

Note: Methods should be called only with positional arguments. Or keyword-only if really necessary.

customizations: Final[list[Customization]] = []
register(customization)

Register a customization with this service

Can be used as class decorator too

@HOOK_SERVICE.register
class MyImplementation(Customization):
    kind = Hook.ORDER_ASSIGN_UID

    def __call__(self, *args, **kwargs) -> t.Any:
        ...
Parameters:

customization (Customization | Type[Customization])

Return type:

Customization

unregister(customization)
Parameters:

customization (Customization)

dispatch(kind, default=None)

Choose the matching registered customization for this kind

Parameters:
  • kind (Hook)

  • default (Callable)

Return type:

Callable

shop.services.hooks.HOOK_SERVICE