> Read the latest version of this page
Edit me on GitHub

pyramid.interfaces

Other Interfaces

interface IAuthenticationPolicy[source]

An object representing a Pyramid authentication policy.

remember(request, principal, **kw)[source]

Return a set of headers suitable for ‘remembering’ the principal named principal when set in a response. An individual authentication policy and its consumers can decide on the composition and meaning of **kw.

authenticated_userid(request)[source]

Return the authenticated userid or None if no authenticated userid can be found. This method of the policy should ensure that a record exists in whatever persistent store is used related to the user (the user should not have been deleted); if a record associated with the current id does not exist in a persistent store, it should return None.

unauthenticated_userid(request)[source]

Return the unauthenticated userid. This method performs the same duty as authenticated_userid but is permitted to return the userid based only on data present in the request; it needn’t (and shouldn’t) check any persistent store to ensure that the user record related to the request userid exists.

effective_principals(request)[source]

Return a sequence representing the effective principals including the userid and any groups belonged to by the current user, including ‘system’ groups such as Everyone and Authenticated.

forget(request)[source]

Return a set of headers suitable for ‘forgetting’ the current user on subsequent requests.

interface IAuthorizationPolicy[source]

An object representing a Pyramid authorization policy.

principals_allowed_by_permission(context, permission)[source]

Return a set of principal identifiers allowed by the permission in context. This behavior is optional; if you choose to not implement it you should define this method as something which raises a NotImplementedError. This method will only be called when the pyramid.security.principals_allowed_by_permission API is used.

permits(context, principals, permission)[source]

Return True if any of the principals is allowed the permission in the current context, else return False

interface IExceptionResponse[source]

Extends: pyramid.interfaces.IException, pyramid.interfaces.IResponse

An interface representing a WSGI response which is also an exception object. Register an exception view using this interface as a context to apply the registered view for all exception types raised by Pyramid internally (any exception that inherits from pyramid.response.Response, including pyramid.httpexceptions.HTTPNotFound and pyramid.httpexceptions.HTTPForbidden).

interface IRoute[source]

Interface representing the type of object returned from IRoutesMapper.get_route

name

The route name

pattern

The route pattern

factory

The root factory used by the Pyramid router when this route matches (or None)

generate(kw)[source]

Generate a URL based on filling in the dynamic segment markers in the pattern using the kw dictionary provided.

pregenerator

This attribute should either be None or a callable object implementing the IRoutePregenerator interface

predicates

A sequence of route predicate objects used to determine if a request matches this route or not or not after basic pattern matching has been completed.

match(path)[source]

If the path passed to this function can be matched by the pattern of this route, return a dictionary (the ‘matchdict’), which will contain keys representing the dynamic segment markers in the pattern mapped to values extracted from the provided path.

If the path passed to this function cannot be matched by the pattern of this route, return None.

interface IRoutePregenerator[source]
__call__(request, elements, kw)[source]

A pregenerator is a function associated by a developer with a route. The pregenerator for a route is called by pyramid.url.route_url() in order to adjust the set of arguments passed to it by the user for special purposes, such as Pylons ‘subdomain’ support. It will influence the URL returned by route_url.

A pregenerator should return a two-tuple of (elements, kw) after examining the originals passed to this function, which are the arguments (request, elements, kw). The simplest pregenerator is:

def pregenerator(request, elements, kw):
    return elements, kw

You can employ a pregenerator by passing a pregenerator argument to the pyramid.config.Configurator.add_route() function.

interface ISession[source]

An interface representing a session (a web session object, usually accessed via request.session.

Keys and values of a session must be pickleable.

invalidate()[source]

Invalidate the session. The action caused by invalidate is implementation-dependent, but it should have the effect of completely dissociating any data stored in the session with the current request. It might set response values (such as one which clears a cookie), or it might not.

new_csrf_token()[source]

Create and set into the session a new, random cross-site request forgery protection token. Return the token. It will be a string.

pop(k, *args)[source]

remove specified key and return the corresponding value *args may contain a single default value, or may not be supplied. If key is not found, default is returned if given, otherwise KeyError is raised

get_csrf_token()[source]

Return a random cross-site request forgery protection token. It will be a string. If a token was previously added to the session via new_csrf_token, that token will be returned. If no CSRF token was previously set into the session, new_csrf_token will be called, which will create and set a token, and this token will be returned.

__contains__(key)[source]

Return true if a key exists in the mapping.

flash(msg, queue='', allow_duplicate=True)[source]

Push a flash message onto the end of the flash queue represented by queue. An alternate flash message queue can used by passing an optional queue, which must be a string. If allow_duplicate is false, if the msg already exists in the queue, it will not be readded.

peek_flash(queue='')[source]

Peek at a queue in the flash storage. The queue remains in flash storage after this message is called. The queue is returned; it is a list of flash messages added by pyramid.interfaces.ISesssion.flash()

itervalues()[source]

iterate over values

new

Boolean attribute. If True, the session is new.

__len__()[source]

Return the number of items in the session.

__getitem__(key)[source]

Get a value for a key

A KeyError is raised if there is no value for the key.

get(key, default=None)[source]

Get a value for a key

The default is returned if there is no value for the key.

keys()[source]

Return the keys of the mapping object.

update(d)[source]

Update D from E: for k in E.keys(): D[k] = E[k]

__setitem__(key, value)[source]

Set a new item in the mapping.

iteritems()[source]

iterate over items

popitem()[source]

remove and return some (key, value) pair as a 2-tuple; but raise KeyError if mapping is empty

iterkeys()[source]

iterate over keys; equivalent to __iter__

pop_flash(queue='')[source]

Pop a queue from the flash storage. The queue is removed from flash storage after this message is called. The queue is returned; it is a list of flash messages added by pyramid.interfaces.ISesssion.flash()

__delitem__(key)[source]

Delete a value from the mapping using the key.

A KeyError is raised if there is no value for the key.

setdefault(key, default=None)[source]

D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D

created

Integer representing Epoch time when created.

items()[source]

Return the items of the mapping object.

clear()[source]

delete all items

changed()[source]

Mark the session as changed. A user of a session should call this method after he or she mutates a mutable object that is a value of the session (it should not be required after mutating the session itself). For example, if the user has stored a dictionary in the session under the key foo, and he or she does session['foo'] = {}, changed() needn’t be called. However, if subsequently he or she does session['foo']['a'] = 1, changed() must be called for the sessioning machinery to notice the mutation of the internal dictionary.

__iter__()[source]

Return an iterator for the keys of the mapping object.

values()[source]

Return the values of the mapping object.

interface ISessionFactory[source]

An interface representing a factory which accepts a request object and returns an ISession object

__call__(request)[source]

Return an ISession object

interface IRendererInfo[source]

An object implementing this interface is passed to every renderer factory constructor as its only argument (conventionally named info)

name

The value passed by the user as the renderer name

package

The “current package” when the renderer configuration statement was found

settings

The deployment settings dictionary related to the current application

registry

The “current” application registry when the renderer was created

type

The renderer type name

interface ITemplateRenderer[source]

Extends: pyramid.interfaces.IRenderer

implementation()[source]

Return the object that the underlying templating system uses to render the template; it is typically a callable that accepts arbitrary keyword arguments and returns a string or unicode object

interface IViewMapperFactory[source]
__call__(self, **kw)[source]

Return an object which implements pyramid.interfaces.IViewMapper. kw will be a dictionary containing view-specific arguments, such as permission, predicates, attr, renderer, and other items. An IViewMapperFactory is used by pyramid.config.Configurator.add_view() to provide a plugpoint to extension developers who want to modify potential view callable invocation signatures and response values.

interface IViewMapper[source]
__call__(self, object)[source]

Provided with an arbitrary object (a function, class, or instance), returns a callable with the call signature (context, request). The callable returned should itself return a Response object. An IViewMapper is returned by pyramid.interfaces.IViewMapperFactory.

interface IMultiDict[source]

An ordered dictionary that can have multiple values for each key. A multidict adds the methods getall, getone, mixed, extend add, and dict_of_lists to the normal dictionary interface. A multidict data structure is used as request.POST, request.GET, and request.params within an Pyramid application.

extend(other=None, **kwargs)[source]

Add a set of keys and values, not overwriting any previous values. The other structure may be a list of two-tuples or a dictionary. If **kwargs is passed, its value will overwrite existing values.

getall(key)[source]

Return a list of all values matching the key (may be an empty list)

add(key, value)[source]

Add the key and value, not overwriting any previous value.

getone(key)[source]

Get one value matching the key, raising a KeyError if multiple values were found.

dict_of_lists()[source]

Returns a dictionary where each key is associated with a list of values.

mixed()[source]

Returns a dictionary where the values are either single values, or a list of values when a key/value appears more than once in this dictionary. This is similar to the kind of dictionary often used to represent the variables in a web request.

interface IResponse[source]

Represents a WSGI response using the WebOb response interface. Some attribute and method documentation of this interface references RFC 2616.

This interface is most famously implemented by pyramid.response.Response and the HTTP exception classes in pyramid.httpexceptions.

content_length

Gets and sets and deletes the Content-Length header. For more information on Content-Length see RFC 2616 section 14.17. Converts using int.

status

The status string.

encode_content(encoding='gzip', lazy=False)[source]

Encode the content with the given encoding (only gzip and identity are supported).

cache_expires

Get/set the Cache-Control and Expires headers. This sets the response to expire in the number of seconds passed when set.

Set (add) a cookie for the response

vary

Gets and sets and deletes the Vary header. For more information on Vary see section 14.44. Converts using list.

retry_after

Gets and sets and deletes the Retry-After header. For more information on Retry-After see RFC 2616 section 14.37. Converts using HTTP date or delta seconds.

www_authenticate

Gets and sets and deletes the WWW-Authenticate header. For more information on WWW-Authenticate see RFC 2616 section 14.47. Converts using ‘parse_auth’ and ‘serialize_auth’.

content_language

Gets and sets and deletes the Content-Language header. Converts using list. For more information about Content-Language see RFC 2616 section 14.12.

etag

Gets and sets and deletes the ETag header. For more information on ETag see RFC 2616 section 14.19. Converts using Entity tag.

content_location

Gets and sets and deletes the Content-Location header. For more information on Content-Location see RFC 2616 section 14.14.

server

Gets and sets and deletes the Server header. For more information on Server see RFC216 section 14.38.

Unset a cookie with the given name (remove it from the response).

pragma

Gets and sets and deletes the Pragma header. For more information on Pragma see RFC 2616 section 14.32.

app_iter

Returns the app_iter of the response.

If body was set, this will create an app_iter from that body (a single-item list)

headers

The headers in a dictionary-like object

charset

Get/set the charset (in the Content-Type)

unicode_body

Get/set the unicode value of the body (using the charset of the Content-Type)

status_int

The status as an integer

conditional_response_app(environ, start_response)[source]

Like the normal __call__ interface, but checks conditional headers:

  • If-Modified-Since (304 Not Modified; only on GET, HEAD)
  • If-None-Match (304 Not Modified; only on GET, HEAD)
  • Range (406 Partial Content; only on GET, HEAD)

Delete a cookie from the client. Note that path and domain must match how the cookie was originally set. This sets the cookie to the empty string, and max_age=0 so that it should expire immediately.

content_range

Gets and sets and deletes the Content-Range header. For more information on Content-Range see section 14.16. Converts using ContentRange object.

content_encoding

Gets and sets and deletes the Content-Encoding header. For more information about Content-Encoding see RFC 2616 section 14.11.

__call__(environ, start_response)[source]

WSGI call interface, should call the start_response callback and should return an iterable

content_md5

Gets and sets and deletes the Content-MD5 header. For more information on Content-MD5 see RFC 2616 section 14.14.

content_disposition

Gets and sets and deletes the Content-Disposition header. For more information on Content-Disposition see RFC 2616 section 19.5.1.

cache_control

Get/set/modify the Cache-Control header (RFC 2616 section 14.9)

location

Gets and sets and deletes the Location header. For more information on Location see RFC 2616 section 14.30.

body

The body of the response, as a str. This will read in the entire app_iter if necessary.

expires

Gets and sets and deletes the Expires header. For more information on Expires see RFC 2616 section 14.21. Converts using HTTP date.

content_type_params

A dictionary of all the parameters in the content type. This is not a view, set to change, modifications of the dict would not be applied otherwise.

md5_etag(body=None, set_content_md5=False)[source]

Generate an etag for the response object using an MD5 hash of the body (the body parameter, or self.body if not given). Sets self.etag. If set_content_md5 is True sets self.content_md5 as well

app_iter_range(start, stop)[source]

Return a new app_iter built from the response app_iter that serves up only the given start:stop range.

last_modified

Gets and sets and deletes the Last-Modified header. For more information on Last-Modified see RFC 2616 section 14.29. Converts using HTTP date.

RequestClass

Alias for pyramid.request.Request

content_type

Get/set the Content-Type header (or None), without the charset or any parameters. If you include parameters (or ; at all) when setting the content_type, any existing parameters will be deleted; otherwise they will be preserved.

date

Gets and sets and deletes the Date header. For more information on Date see RFC 2616 section 14.18. Converts using HTTP date.

copy()[source]

Makes a copy of the response and returns the copy.

accept_ranges

Gets and sets and deletes the Accept-Ranges header. For more information on Accept-Ranges see RFC 2616, section 14.5

age

Gets and sets and deletes the Age header. Converts using int. For more information on Age see RFC 2616, section 14.6.

request

Return the request associated with this response if any.

merge_cookies(resp)[source]

Merge the cookies that were set on this response with the given resp object (which can be any WSGI application). If the resp is a webob.Response object, then the other object will be modified in-place.

headerlist

The list of response headers.

environ

Get/set the request environ associated with this response, if any.

allow

Gets and sets and deletes the Allow header. Converts using list. For more information on Allow see RFC 2616, Section 14.7.

body_file

A file-like object that can be used to write to the body. If you passed in a list app_iter, that app_iter will be modified by writes.

Table Of Contents

Previous topic

pyramid.i18n

Next topic

pyramid.location