boardinghouse.middleware module

class boardinghouse.middleware.SchemaMiddleware[source]

Middleware to set the postgres schema for the current request’s session.

The schema that will be used is stored in the session. A lookup will occur (but this could easily be cached) on each request.

There are three ways to change the schema as part of a request.

  1. Request a page with a querystring containg a __schema value:

    https://example.com/page/?__schema=<schema-name>
    

The schema will be changed (or cleared, if this user cannot view that schema), and the page will be re-loaded (if it was a GET). This method of changing schema allows you to have a link that changes the current schema and then loads the data with the new schema active.

It is used within the admin for having a link to data from an arbitrary schema in the LogEntry history.

This type of schema change request should not be done with a POST request.

  1. Add a request header:

    X-Change-Schema: <schema-name>
    
This will not cause a redirect to the same page without query string. It is the only way to do a schema change within a POST request, but could be used for any request type.
  1. Use a specific request:

    https://example.com/__change_schema__/<schema-name>/
    
This is designed to be used from AJAX requests, or as part of an API call, as it returns a status code (and a short message) about the schema change request. If you were storing local data, and did one of these, you are probably going to have to invalidate much of that.

You could also come up with other methods.

process_exception(request, exception)[source]

In the case a request returned a DatabaseError, and there was no schema set on request.session, then look and see if the error that was provided by the database may indicate that we should have been looking inside a schema.

In the case we had a TemplateSchemaActivation exception, then we want to remove that key from the session.

boardinghouse.middleware.change_schema(request, schema)[source]

Change the schema for the current request’s session.

Note this does not actually _activate_ the schema, it only stores the schema name in the current request’s session.