Extras

At this point our application is complete and tested. At this point you can dive right in to the User’s Guide, but there are a few extra things we can add to further demonstrate some more of Ferris’ capabilities.

Searching

Another useful component is search. It is slightly more difficult to use but still quite easy.

First, we need to modify our model.

Import the index and unindex functions:

from ferris.components.search import index, unindex

Add two methods:

def after_put(self, key):
    index(self)

@classmethod
def after_delete(cls, key):
    unindex(key)

These are model callbacks (covered more extensively in the User’s Guide) that will index and unindex a post whenever it is saved or deleted.

Note

Any posts created before you made this change will not be searchable until you edit and resave them.

Import the Search component and add it to our Posts handler:

from ferris.core.easy_handler import EasyHandler, scaffold, route
from ferris.components.pagination import Pagination
from ferris.components.search import Search


@scaffold
class Posts(EasyHandler):
    components = [Pagination, Search]

Now let’s add a new search action:

def search(self):
    self.components.search.search()

Import the search macros into templates/posts/list.html:

{% import 'macros/search.html' as search with context %}

Finally, add these somewhere at the top of the inside of the layout_content block:

{{search.search_filter(action='list')}}
{{search.search_info()}}

Now when we visit http://localhost:8080/posts there should be a search box from which we can search through all posts.

Table Of Contents

Previous topic

Functional Testing

Next topic

User’s Guide

This Page