Context & View Templates

DataContext and DataView both come with a template and default field. These can be used in downstream client implementations for enabling default context and/or view data for user agents. For example, Serrano checks if there is a default template defined for the resource if this is the requesting user's first time accessing it.

cxt = DataContext({...}, template=True, default=True)

Many templates can be created for each type, but only one default can exist for each. Upon save, this will be checked to prevent ambiguity for downstream clients.

Custom QueryProcessor

The QueryProcessor is the preferred entry point for most consumers of Avocado's APIs for providing and end-to-end query processing pipeline. The QueryProcessor class is broken up into simply methods which enables customizing each step along the pipeline by overriding or extending these methods.

One common need is to globally or conditionally restrict access to certain data. This can generally be accomplished by adding one or more additional conditions to the query being processed. For example, if both authenticated and non-authenticated users can access some data:

from avocado.query.pipeline import QueryProcessor

class AuthQueryProcessor(QueryProcessor):
    def get_queryset(*args, **kwargs):
        # Get the normally generated queryset
        queryset = super(AuthQueryProcessor, self).get_queryset(*args, **kwargs)

        # Filter down to only include the objects marked as being public
        if 'request' in kwargs and not kwargs['request'].user.is_authenticated():
            queryset = queryset.filter(public=True)
        return queryset

This of course is assumes the processor is being utilized in a web context where a request object is available (such as Serrano).