Services/AccountPortal/addControllers

From MozillaWiki
Jump to navigation Jump to search

Creating new Account Portal controllers

In order to add a new controller to account portal, add name_of_controller.py into accountportal/controllers. It should have the following basic template:

from accountportal import logger
from accountportal.controllers import BaseController, get_template_lookup

logger gives you access to the configured logger for the application (enabling logger.error(), etc)


class Controller(BaseController):

The class should be named Controller and subclass the base controller.

    def N_(message): return message
    _map = {'password': [N_('Change your password'), 'change_password'],
            'delete': [N_('Delete your account'), 'delete_account']}
    _pretty_name = N_('Account Settings')
    del N_

    _lookup = get_template_lookup('account')

N_ is to allow the babel processor to detect strings that need translating, but the translation needs to be deferred until the actual request.

get_template_lookup initializes the variables that the templating will use. The value passed in is the name of the folder in the template directory in which the templates live.

_pretty_name is the string that will be used as the navigation header in the sidebar

_map is a dictionary of tuples that is used to provide the controller map to the wsgi app. The elements are of the form:

url: [Sidebar name, function in controller]

The actual url of the page will be <prefix>/<name of controller>/<url> unless the url begins with http, at which point it will be treated as a raw url



Base Controller

The BaseController class gives you the following class functions:

  • __init__

loads up the auth from the config file and adds it to self.auth

  • render(self, template, **data)

renders the appropriate template with the data object (see get_base_data)

  • generate_crumb(self, request)
  • check_crumb(self, request)

functions for automatically doing crumbing. Take the result of generate_crumb and make it the value of the 'crumb' parameter in your form and this'll handle the rest.

  • get_base_data(self, request):

returns a set of data to populate the base template files. Additional data you want to add to the template should be added to this dictionary. Data available in there includes:

    • 'prefix': url prefix to the site
    • 'static': url prefix to the static content
    • 'error': any errors that have ocurred
    • 'environ': the request.environ,
    • 'nav': the navigational map for the site,
    • 'username': username
    • 'email': user email


Configuring your controller

In etc/accountportal.cfg, add the name of your controller (without the .py) to the modules list in the console section. The wsgi app will grab the file and use the map to set up your routes.

All pages require login. If you find you have a new page needed that doesn't require a login, please file a bug.