context.request
The request object, with all the information sent by the client. Usually you won’t access directly to it, because the API offered by the context object is preferred, and enough most of the time.
context.response
The response object, which will be sent to the client. You can manipulate it directly, though this is rarely needed.
context.server
The Web server. Useful for example to access the error log.
context.root
The root object, your application.
context.user
The authenticated user (an object that provides the API for users, we will see them later). Or None if the user is not authenticated.
context.uri
The URI as it was typed by the user in the browser bar. May be different than the URI of the request object when there is virtual hosting. It is a Reference instance.
context.path
The path to traverse from the application’s root to reach the object to be published. It is a Path object.
context.method
The name of the method to be called on the object.
context.object
The object we get after traversing the path, or None.
context.commit
A boolean value we check to decide whether to commit the transaction or to abort it. By default it is False for the GET and HEAD request methods, and True for the others, but the programmer can change it.
This is what the context object is made of, but the programmer can set attributes to it to pass values around.
The context also provides an API.
The client may send data to the server either with the URI’s query, or within the request body, for example when the user submits a form. To access these values it is possible to use the request object, but it is strongly recommended to use the higher level API provided by the context:
get_form_keys()
Returns the keys of all the form values sent by the client.
has_form_value(name)
Returns True if the client sent a value with the given name, False if not.
get_form_value(name, default=None, type=None)
Returns the form value for the given name. If the client sent more than one value for the same name it will return the first one.
The value returned will be a byte string. Unless the type parameter is passed, then it will be used to deserialize the value (see Chapter 4 for details on itools datatypes).
If the client did not sent any value, the value of the default parameter will be returned. Unless the type parameter is passed, then the default value for the given type will be returned.
get_form_values(name, default=[], type=None)
Returns a list with all the values the client sent for the given name, even if the client only sent one value.
The values will be byte strings. Unless the type parameter is passed, then it will be used to deserialize the values.
If the client did not sent any value an empty list will be returned.
To practice the API above we are going to see an slightly more elaborate example:
class MyRoot(Root):
GET__access__ = True
def GET(self, context):
name = context.get_form_value('name', default='World')
return 'Hello %s' % name
Now, the URI http://localhost:8080 will return the same response as before, but http://localhost:8080/?name=buddy will give a customized message. You can try with other values to better appreciate the power of this code.
Cookies can be used to implement client side sessions1, this is, to keep information across several requests. The context object provides a high level API to work with them:
get_cookie(name)
Returns the value of the cookie with the given name. If there is not a cookie with that name return None.
has_cookie(name)
Returns True if there is a cookie with the given name, False otherwise.
set_cookie(name, value, **kw)
Sets the cookie with the given name to the given value. The keyword parameters are to define any of the cookie attributes expires, domain, path, max_age, comment and secure.
del_cookie(name)
Removes the cookie with the given name.
The context object offers this API for redirections:
redirect(reference, status=302)
Obsolete: return a Reference instance instead. Redirects the client to the given reference. By default the response code will be “302 Found”, but this can be overridden with the parameter status.
come_back(message, goto=None, keep=[], **kw)
This is a high level function that builds and returns a Reference instance that can be sent back for a redirection. It is often useful to use in the action of a form.
The base URI is defined by the goto parameter. If it is not passed the referrer will be used instead.
To the base URI we add the form values defined by the keep parameter. By default we add nothing.
Finally, we add the value defined by the message parameter. But first this message will be translated (see Chapter ), and then interpolated (using the “$” syntax) with the given keyword parameters (kw).
Footnotes