eventtracking.django

A django specific tracker

class eventtracking.django.DjangoTracker[source]

Bases: eventtracking.tracker.Tracker

A eventtracking.tracker.Tracker that constructs its backends from Django settings.

create_backends_from_settings()[source]

Expects the Django setting “EVENT_TRACKING_BACKENDS” to be defined and point to a dictionary of backend engine configurations.

Example:

EVENT_TRACKING_BACKENDS = {
    'default': {
        'ENGINE': 'some.arbitrary.Backend',
        'OPTIONS': {
            'endpoint': 'http://something/event'
        }
    },
    'another_engine': {
        'ENGINE': 'some.arbitrary.OtherBackend',
        'OPTIONS': {
            'user': 'foo'
        }
    },
}
create_processors_from_settings()[source]

Expects the Django setting “EVENT_TRACKING_PROCESSORS” to be defined and point to a list of backend engine configurations.

Example:

EVENT_TRACKING_PROCESSORS = [
    {
        'ENGINE': 'some.arbitrary.Processor'
    },
    {
        'ENGINE': 'some.arbitrary.OtherProcessor',
        'OPTIONS': {
            'user': 'foo'
        }
    },
]
instantiate_from_dict(values)[source]

Constructs an object given a dictionary containing an “ENGINE” key which contains the full module path to the class, and an “OPTIONS” key which contains a dictionary that will be passed in to the constructor as keyword args.

instantiate_objects(node)[source]

Recursively traverse a structure to identify dictionaries that represent objects that need to be instantiated

Traverse all values of all dictionaries and all elements of all lists to identify dictionaries that contain the special “ENGINE” key which indicates that a class of that type should be instantiated and passed all key-value pairs found in the sibling “OPTIONS” dictionary as keyword arguments.

For example:

tree = {
    'a': {
        'b': {
            'first_obj': {
                'ENGINE': 'mypackage.mymodule.Clazz',
                'OPTIONS': {
                    'size': 10,
                    'foo': 'bar'
                }
            }
        },
        'c': [
            {
                'ENGINE': 'mypackage.mymodule.Clazz2',
                'OPTIONS': {
                    'more_objects': {
                        'd': {'ENGINE': 'mypackage.foo.Bar'}
                    }
                }
            }
        ]
    }
}
root = self.instantiate_objects(tree)

That structure of dicts, lists, and strings will end up with (this example assumes that all keyword arguments to constructors were saved as attributes of the same name):

assert type(root[‘a’][‘b’][‘first_obj’]) == <type ‘mypackage.mymodule.Clazz’> assert root[‘a’][‘b’][‘first_obj’].size == 10 assert root[‘a’][‘b’][‘first_obj’].foo == ‘bar’ assert type(root[‘a’][‘c’][0]) == <type ‘mypackage.mymodule.Clazz2’> assert type(root[‘a’][‘c’][0].more_objects[‘d’]) == <type ‘mypackage.foo.Bar’>

eventtracking.django.override_default_tracker()[source]

Sets the default tracker to a DjangoTracker