django-carrot utilities

This module contains a number of helper functions for performing basic Carrot functions, e.g. publish, schedule and consume

Most users should use the functions defined in this module, rather than attempting to subclass the base level objects

carrot.utilities.create_class_view(view, decorator)

Applies a decorator to the dispatch method of a given class based view. Can be chained

Parameters:
  • view (class) – the class-based view to apply the decorator to
  • decorator (function) – the decorator to apply
Return type:

the updated class based view

carrot.utilities.create_function_view(view, decorator)

Similar to create_class_view(), but attaches a decorator to a function based view, instead of a class-based one

Parameters:
  • view (func) – the function based view to attach a decorator tio
  • decorator (func) – the decorator attach
Return type:

the updated view function

carrot.utilities.create_message(task, priority=0, task_args=(), queue=None, exchange='', routing_key=None, task_kwargs=None)

Creates a carrot.objects.Message object without publishing it

The task to execute (as a string or a callable) needs to be supplied. All other arguments are optional :param task: the task to be handled asynchronously. :type task: str or func :param int priority: the priority to be applied to the message when it is published to RabbitMQ

Parameters:
  • task_args (tuple) – the positional arguments to be passed to the function when it is called
  • queue (str or None) – the name of the queue to publish the message to. Will be set to “default” if not provided
  • exchange (str) – the exchange name
  • routing_key (str or NoneType) – the routing key
  • task_kwargs (dict) – the keyword arguments to be passed to the function when it is called
Return type:

carrot.objects.Message

carrot.utilities.create_scheduled_task(task, interval, queue=None, **kwargs)

Helper function for creating a carrot.models.ScheduledTask

Parameters:
  • task (str or callable) – a callable, or a valid path to one as a string
  • interval (dict) – the interval at which to publish the message, as a dict, e.g.: {‘seconds’: 5}
  • queue (str) – the name of the queue to publish the message to.
  • kwargs – the keyword arguments to be passed to the function when it is executed
Return type:

carrot.models.ScheduledTask

carrot.utilities.decorate_class_view(view_class, decorators=None)

Loop through a list of string paths to decorator functions, and call create_class_view() for each one

Parameters:
  • view_class (class) – the class-based view to attach the decorators to
  • decorators (list) – a list of string decorators, e.g. [‘myapp.mymodule.decorator1’, ‘myapp.mymodule.decorator2’]
Returns:

the class based view with all decorators attached to the dispatch method

carrot.utilities.decorate_function_view(view, decorators=None)

Similar to decorate_class_view(), but for function based views

carrot.utilities.get_host_from_name(name)

Gets a host object from a given queue name based on the Django configuration

If no queue name is provided (as may be the case from some callers), this function returns a VirtualHost based on the CARROT.default_broker value.

May raise an exception if the given queue name is not registered in the settings.

Parameters:name (str) – the name of the queue to lookup.
Return type:carrot.objects.VirtualHost
carrot.utilities.get_mixin(decorator)

Helper function that allows dynamic application of decorators to a class-based views

Parameters:decorator (func) – the decorator to apply to the view
Returns:
carrot.utilities.publish_message(task, *task_args, priority=0, queue=None, exchange='', routing_key=None, **task_kwargs)

Wrapped for create_message(), which publishes the task to the queue

This function is the primary method of publishing tasks to a message queue

carrot.utilities.validate_task(task)

Helper function for dealing with task inputs which may either be a callable, or a path to a callable as a string

In case of a string being provided, this function checks whether the import path leads to a valid callable

Otherwise, the callable is converted back into a string (as the carrot.objects.Message requires a string input)

This function is used by the following other utility functions: - create_scheduled_task() - create_message()

Parameters:task (str or callable) – a callable or a path to one as a string
Returns:a validated path to the callable, as a string