Package trac :: Package mimeview :: Module api :: Class Context

Class Context

source code

object --+
         |
        Context

Rendering context.

A rendering context specifies ''how'' the content should be rendered. It holds together all the needed contextual information that will be needed by individual renderer components.

To that end, a context keeps track of the Href instance (.href) which should be used as a base for building URLs.

It also provides a PermissionCache (.perm) which can be used to restrict the output so that only the authorized information is shown.

A rendering context may also be associated to some Trac resource which will be used as the implicit reference when rendering relative links or for retrieving relative content and can be used to retrieve related metadata.

Contexts can be nested, and a new context can be created from an existing context using the call syntax. The previous context can be retrieved using the .parent attribute.

For example, when rendering a wiki text of a wiki page, the context will be associated to a resource identifying that wiki page.

If that wiki text contains a [[TicketQuery]] wiki macro, the macro will set up nested contexts for each matching ticket that will be used for rendering the ticket descriptions.


Since: version 0.11

Instance Methods
 
__init__(self, resource, href=None, perm=None)
Directly create a Context.
source code
 
__repr__(self)
repr(x)
source code
Context
__call__(self, resource=None, id=False, version=False, parent=False)
Create a nested rendering context.
source code
 
__contains__(self, resource)
Check whether a resource is in the rendering path.
source code
 
set_hints(self, **keyvalues)
Set rendering hints for this rendering context.
source code
 
get_hint(self, hint, default=None)
Retrieve a rendering hint from this context or an ancestor context.
source code
 
has_hint(self, hint)
Test whether a rendering hint is defined in this context or in some ancestor context.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Methods
Context
from_request(cls, req, resource=None, id=False, version=False, parent=False, absurls=False)
Create a rendering context from a request.
source code
Instance Variables
  parent
The parent context, if any
Properties

Inherited from object: __class__

Method Details

__init__(self, resource, href=None, perm=None)
(Constructor)

source code 

Directly create a Context.

The actual .perm attribute of the rendering context will be bound to the given resource so that fine-grained permission checks will apply to that.

Parameters:
  • resource (Resource) - the associated resource
  • href - an Href object suitable for creating URLs
  • perm - a PermissionCache object used for restricting the generated output to "authorized" information only.
Overrides: object.__init__

from_request(cls, req, resource=None, id=False, version=False, parent=False, absurls=False)
Class Method

source code 

Create a rendering context from a request.

The perm and href properties of the context will be initialized from the corresponding properties of the request object.

>>> from trac.test import Mock, MockPerm
>>> req = Mock(href=Mock(), perm=MockPerm())
>>> context = Context.from_request(req)
>>> context.href is req.href
True
>>> context.perm is req.perm
True
Parameters:
  • req - the HTTP request object
  • resource - the Resource object or realm
  • id - the resource identifier
  • version - the resource version
  • absurls - whether URLs generated by the href object should be absolute (including the protocol scheme and host name)
Returns: Context
a new rendering context

__repr__(self)
(Representation operator)

source code 
repr(x)
Overrides: object.__repr__
(inherited documentation)

__call__(self, resource=None, id=False, version=False, parent=False)
(Call operator)

source code 

Create a nested rendering context.

self will be the parent for the new nested context.

>>> context = Context('wiki', 'WikiStart')
>>> ticket1 = Resource('ticket', 1)
>>> context('ticket', 1).resource == ticket1
True
>>> context(ticket1).resource is ticket1
True
>>> context(ticket1)().resource is ticket1
True
Parameters:
  • resource - either a Resource object or the realm string for a resource specification to be associated to the new context. If None, the resource will be the same as the resource of the parent context.
  • id - the identifier part of the resource specification
  • version - the version of the resource specification
Returns: Context
the new context object

__contains__(self, resource)
(In operator)

source code 

Check whether a resource is in the rendering path.

The primary use for this check is to avoid to render the content of a resource if we're already embedded in a context associated to that resource.

Parameters:
  • resource - a Resource specification which will be checked for