API

hooks.formhook Module

HookFactory Object

class hooks.formhook.HookFactory(instances)

Hook factory, provide some short-cuts to make a sequence of forms behave as a single form. This is used by Hook

Parameters:instances (list) – Sequence of callables, usually django.forms.Form or django.forms.ModelForm
__iter__()

Forms iterator

Yield:Form instance
is_valid()

Validate all the forms

Returns:The result of validating all the forms
Return type:bool
save(*args, **kwargs)

Save all the forms

Parameters:
  • *args – Positional arguments passed to the forms
  • **kwargs – Keyword arguments passed to the forms
Returns:

Sequence of returned values by all the forms as tuples of (instance, result)

Return type:

list

Hook Object

class hooks.formhook.Hook(providing_args=None)

Container of forms

Parameters:providing_args (list) – A list of the arguments this hook can pass along in a __call__()
__call__(*args, **kwargs)

Call all registered forms

Parameters:
  • prefix (str) – Prefix for the forms to avoid clashing of fields, it must be of the form text_%d. Defaults to hook%d
  • *args – Positional arguments passed to the forms
  • **kwargs – Keyword arguments passed to the forms
Returns:

Factory to handle all the forms as they were one

Return type:

HookFactory

register(form)

Register form

Parameters:form (callable) – The form, usually django.forms.Form or django.forms.ModelForm
unregister(form)

Remove form from registry

Parameters:form (callable) – The previously registered form

hooks.signalhook Module

Hook Object

class hooks.signalhook.Hook

A dynamic-signal dispatcher. Should be used through hook

thread-safety: it’s not thread safe, this may change in the future if a RLock is added around _registry operations. In the meanwhile, you should register/connect/disconnect at import time (global scope) to ensure thread-safety, doing it in the AppConfig.ready() method is safe

connect(name, func, sender=None, dispatch_uid=None)

Connects a function to a hook. Creates the hook (name) if it does not exists

Parameters:
  • name (str) – The hook name
  • func (callable) – A function reference used as a callback
  • sender (class) – Optional sender __class__ to which the func should respond. Default will match all
  • dispatch_uid (str) – Optional unique id, see django.dispatch.Signal for more info
disconnect(name, func, dispatch_uid=None)

Disconnects a function from a hook

Parameters:
  • name (str) – The hook name
  • func (callable) – A function reference registered previously
  • dispatch_uid (str) – optional unique id, see django.dispatch.Signal for more info.
register(name)

Register a new hook. Not required (see connect() method)

Parameters:name (str) – The hook name
Returns:Django signal
Return type:django.dispatch.Signal
send(name, sender=None, **kwargs)

Sends the signal. Return every function response that was hooked to hook-name as a list: [(func, response), ]

Parameters:
  • name (str) – The hook name
  • sender (class) – Optional sender __class__ to which registered callback should match (see connect() method)
Returns:

Signal responses as a sequence of tuples (func, response)

Return type:

list

hook Singleton

hooks.signalhook.hook = <hooks.signalhook.Hook object>

A dynamic-signal dispatcher. Should be used through hook

thread-safety: it’s not thread safe, this may change in the future if a RLock is added around _registry operations. In the meanwhile, you should register/connect/disconnect at import time (global scope) to ensure thread-safety, doing it in the AppConfig.ready() method is safe

hooks.templatehook Module

TemplateHook Object

class hooks.templatehook.TemplateHook(providing_args=None)

A hook for templates. This can be used directly or through the Hook dispatcher

Parameters:providing_args (list) – A list of the arguments this hook can pass along in a __call__()
register(func)

Register a new callback

Parameters:func (callable) – A function reference used as a callback
unregister(func)

Remove a previously registered callback

Parameters:func (callable) – A function reference that was registered previously
unregister_all()

Remove all callbacks

Hook Object

class hooks.templatehook.Hook

Dynamic dispatcher (proxy) for TemplateHook

register(name, func)

Register a new callback. When the name/id is not found a new hook is created under its name, meaning the hook is usually created by the first registered callback

Parameters:
  • name (str) – Hook name
  • func (callable) – A func reference (callback)
unregister(name, func)

Remove a previously registered callback

Parameters:
  • name (str) – Hook name
  • func (callable) – A function reference that was registered previously
unregister_all(name)

Remove all callbacks

Parameters:name (str) – Hook name

hooks.templatetags.hooks_tags Module

Template tags

hooks.templatetags.hooks_tags.hook_tag(context, name, *args, **kwargs)

Hook tag to call within templates

Parameters:
  • context (dict) – This is automatically passed, contains the template state/variables
  • name (str) – The hook which will be dispatched
  • *args – Positional arguments, will be passed to hook callbacks
  • **kwargs – Keyword arguments, will be passed to hook callbacks
Returns:

A concatenation of all callbacks responses marked as safe (conditionally)

Return type:

str

Helpers

hooks.templatetags.hooks_tags.template_hook_collect(module, hook_name, *args, **kwargs)

Helper to include in your own templatetag, for static TemplateHooks

Example:

import myhooks
from hooks.templatetags import template_hook_collect

@register.simple_tag(takes_context=True)
def hook(context, name, *args, **kwargs):
    return template_hook_collect(myhooks, name, context, *args, **kwargs)
Parameters:
  • module (module) – Module containing the template hook definitions
  • hook_name (str) – The hook name to be dispatched
  • *args – Positional arguments, will be passed to hook callbacks
  • **kwargs – Keyword arguments, will be passed to hook callbacks
Returns:

A concatenation of all callbacks responses marked as safe (conditionally)

Return type:

str