gavo.web.common module

Common code for the nevow based interface.

class gavo.web.common.BinaryItem(original)[source]

Bases: object

A sentinel marking request.args items that couldn’t be utf-8 decoded

Whatever it was (most likely a file upload) is found in the original attribute.

class gavo.web.common.CommonRenderers[source]

Bases: object

A base for renderer (python) mixins within the DC.

Including standard stylesheets/js/whatever: <head n:render=”commonhead”>…</head>

Rendering internal links (important for off-root operation):

  • <tag href|src=”/foo” n:render=”rootlink”/>

commonhead(request, tag)[source]
getconfig(request, tag)[source]

looks up the text child in the DaCHS configuration and inserts the value as a (unicode) string.

The config key is either [section]item or just item for something in [general]. Behaviour for undefined config items is undefined.

unicode(request, tag)[source]

returns unicode(data); this is deprecated in favour of string.

urlescape(request, tag)[source]

renders data as a url-escaped string.

class gavo.web.common.HTMLMetaBuilder(macroPackage=None)[source]

Bases: MetaBuilder

clear()[source]
endKey(atom)[source]
enterValue(value)[source]
getResult()[source]
startKey(atom)[source]
class gavo.web.common.JSONQuery[source]

Bases: Resource

A resource returning JSON for a database query.

Have the query in a query attribute to the class. Use %(whatever)s to put in the first match of that in request.strargs.

TODO: we should do some more sensible error handling.

doQuery(queryArgs)[source]

returns a list of dictionaries for the query result. The default just executes self.query.

render(request)[source]

Render a given resource. See L{IResource}’s render method.

I delegate to methods of self with the form ‘render_METHOD’ where METHOD is the HTTP that was used to make the request. Examples: render_GET, render_HEAD, render_POST, and so on. Generally you should implement those methods instead of overriding this one.

render_METHOD methods are expected to return a byte string which will be the rendered page, unless the return value is C{server.NOT_DONE_YET}, in which case it is this class’s responsibility to write the results using C{request.write(data)} and then call C{request.finish()}.

Old code that overrides render() directly is likewise expected to return a byte string or NOT_DONE_YET.

@see: L{IResource.render}

class gavo.web.common.Request(*args, **kwargs)[source]

Bases: MPRequest

a custom request class used in DaCHS’ application server.

Overridden behaviour:

  • you can write str; it’s going to be utf-8 encoded automatically, which out to be about the right thing almost always in DaCHS.

  • There’s a popSegments method for use during resource dispatch (it gets all remaining segments and pushes them to prepath at the same time). popSegments also returns (utf-8-decoded) strings rather than bytes.

  • There’s strargs, which is a dict mapping strings to lists of strings. This is being created at first call can then doesn’t follow request.args any more. DaCHS code should only use strargs unless you’re actually dealing with non-utf-8 data (and then you should probably use request.files).

connectionLost(reason)[source]

There is no longer a connection for this request to respond over. Clean up anything which can’t be useful anymore.

finishCallback(perhapsFailure)[source]

A convenience method that finishes the request and perhaps handles and error

This is mainly here as a shortcut to .addBoth(lambda _: request.finish()); It also is a last-resort check that catches escaping, unhandled errors; but, at least for now, you shouldn’t rely on getting sensible behaviour (e.g., error reporting to users) from here.

getAuthUser()[source]

returns a username backed by credentials as a string.

This will return None if either no user was passed in or if the credentials are wrong.

This is for optional-auth scenarios. For anything else, use the runAuthenticated function from this module.

getPassword()[source]

returns the password utf-8-decoded.

See getUser on the rationale and on limitations.

getUser()[source]

returns a username as a string – or None if no user info has been passed.

We’re overriding this because we really don’t want to get any bytes in here. If anyone manages to put in non-utf, this will return None.

Note that getUser does not authenticate; it just returns whatever twisted deems to be the user name.

Use getAuthUser for a user name validated through some credential.

gotLength(length)[source]

Called when HTTP channel got length of content in this request.

This method is not intended for users.

@param length: The length of the request body, as indicated by the

request headers. L{None} if the request headers do not indicate a length.

popSegments(name)[source]

returns [name]+postpath and clears the current postpath, where all elements are utf-8-decoded strings.

This facilitates nevow-style full-path resource resolution.

processingFailed(failure)[source]

Finish this request with an indication that processing failed and possibly display a traceback.

@param reason: Reason this request has failed. @type reason: L{twisted.python.failure.Failure}

@return: The reason passed to this method. @rtype: L{twisted.python.failure.Failure}

pushBackSegments(segments)[source]

tries to put segments back to the prepath from the postpath.

This will remove existing segments from postpath as long as they match segments. If you’re devious, you might exhaust the prepath in this way; that’s your fault, then.

segments may contain strings or bytes and will be destroyed in the process.

property strargs
write(payload)[source]

Write data to the transport (if not responding to a HEAD request).

@param data: A string to write to the response. @type data: L{bytes}

class gavo.web.common.TypedData(data, mime, modificationStamp=None)[source]

Bases: Resource

A simple resource just producing bytes passed in during construction, declared as a special content type.

render(request)[source]

Render a given resource. See L{IResource}’s render method.

I delegate to methods of self with the form ‘render_METHOD’ where METHOD is the HTTP that was used to make the request. Examples: render_GET, render_HEAD, render_POST, and so on. Generally you should implement those methods instead of overriding this one.

render_METHOD methods are expected to return a byte string which will be the rendered page, unless the return value is C{server.NOT_DONE_YET}, in which case it is this class’s responsibility to write the results using C{request.write(data)} and then call C{request.finish()}.

Old code that overrides render() directly is likewise expected to return a byte string or NOT_DONE_YET.

@see: L{IResource.render}

gavo.web.common.compwrap(pg)[source]

wraps some twisted resource for compression.

class gavo.web.common.doctypedStan(rootEl)[source]

Bases: TagLoader

is the TagLoader loader that arranges for a an XHTML doctype and namespace.

As this relies on a magic attribute gavo_useDoctype, this will only work if the template will be rendered through nevowc.TemplatedPage.

DOCTYPE = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
gavo.web.common.escapeForHTML(aString)[source]
gavo.web.common.getfirst(request, key, default)[source]

returns the first value of key in the arguments of a twisted request.

gavo.web.common.runAuthenticated(request, reqGroup, fun, *args)[source]

returns the value of fun(*args) if the logged in user is in reqGroup, requests authentication otherwise.