Components

Components are reusable pieces of functionality that can be invoked via a controller or react to controller events. This are a great way to re-use code between controllers, such as with pagination, searching, emailing, etc.

Using Components

Import the components and include them in the Meta.components property on your controller, like so:

from ferris.components.pagination import Pagination
from ferris.components.search import Search

class Documents(Controller):
    class Meta:
        components = (Pagination, Search)

Inside of your actions, you can access the component instances using self.components:

def list(self):
    self.components.pagination()

Creating Components

You can of course create your own components. Here’s a very simple component that listens for the startup event and logs:

class Logger(object):
    def __init__(self, controller):
        self.controller = controller
        self.controller.events.after_start += self.on_after_startup

    def on_after_startup(self, controller):
        logging.info("Hello!")

Built-in Components

The ferris.components module provides a few built-in components. These can be used directly, customized, or used as guidelines when building your own.

Cache

The Cache component provides utilities for doing client-side (edge) caching. This is not to be confused with memcache which is server-side caching.

class ferris.components.cache.Cache(controller)[source]

Provides easy methods to for setting edge caching, both via the browser and App Engine’s intermediate caching proxies.

Usage is pretty easy. Include the cache component and use the set_cache decorator.

ferris.components.cache.set_cache(mode='public', minutes=None)[source]

Decorator that calls the cache component automatically for an action

For example:

from ferris import Controller, route
from ferris.components.cache import Cache, set_cache

class Posts(Controller):
    class Meta:
        components = (Cache)

    @route
    @set_cache('public', 60*60)
    def list_all(self):
        ...

    @route
    @set_cache('private', 60*60)
    def list_mine(self):
        ...

OAuth

The OAuth component is documented in OAuth2.

Pagination

class ferris.components.pagination.Pagination(controller)[source]

Provides a generic, reusable Pagination API.

This can be used to automatically paginate ndb.Query objects but it can also be used directly to provide pagination over custom datasources.

Automatically happens for any list actions but can also be manually invoked via paginate() or __call__().

Pagination.paginate(query=None, cursor=None, limit=None)[source]

Paginates a ndb.Query and sets up the appropriate template variables.

Uses Controller.Meta.pagination_limit to determine how many items per page or defaults to 10 if omitted.

Returns the data, and if query is a string, sets that template variable.

If query is omitted it’ll attempt to find the dataset using the scaffold variable names.

For example of using this, see Extras

You can also use the pagination component to provide pagination for custom datasets such as external apis or CloudSQL. The pagination component can automatically keep track of previous cursors to provide backwards pagination. For example:

cursor, limit = self.components.pagination.get_pagination_params()

data, next_cursor = get_custom_data(cursor, limit)

self.components.pagination.set_pagination_info(
    current_cursor=cursor,
    next_cursor=next_cursor,
    limit=limit,
    count=len(data)
)
Pagination.get_pagination_params(cursor=None, limit=None)[source]

Retuns the pagination parameters provided by the request. Use this in your custom APIs to determine which cursor and the number of objects the user is requesting.

Pagination.set_pagination_info(current_cursor=None, next_cursor=None, limit=None, count=None)[source]

Sets the pagintion information for the view context. Use by your custom APIs to expose the next cursor, the limit, and the number of objects currently visible.

Sets the paging template variable to a dictionary like:

{
    "cursor": "abc...",
    "previous_cursor": "rvx...",
    "next_cursor": "nzb...",
    "limit": 10,
    "count": 10
}
Pagination.get_pagination_info()[source]

Returns the current pagination infomation from the view context: previous cursor, current cursor, next cursor, page, limit, and count.

There is also a helpful macro to print simple forward and backwards pagination controls:

{% import "macros/pagination.html" as p %}

{{p.next_page_link()}}

Generates bootstrap style forward and backwards pagination arrows.

Upload

The search component is documented in the Uploads & Downloads section.