procyon

Overview

procyon is a collection of reusable components for Pyramid.

Currently, it includes the following components:

  • A base model and a DB session that is configured in the common way.
  • A user model and login/logout views.
  • Some common panels.

Especially, the base model is not required. You can also use the user model with your own base model.

Setup

Use the Configurator.include() method:

config = Configurator()
config.include('procyon')

or add procyon to the pyramid.includes list in your ini file:

pyramid.includes =
    procyon

Both are equivalent.

Using models

There are various cases to define and use models with procyon.

Create a default user model and use it

Simply, call Configurator.set_default_user_model() method with an optional tablename argument.

config.set_default_user_model(tablename='user')

If you want to get the user model later, use request.get_user_model() API.

Use your own base model (advanced)

from procyon import UserModelMixin

class UserModel(YourBaseModel, UserModelMixin):
    __tablename__ = 'user'

YourBaseModel is any class you defined somewhere that provides IBaseModel.

Using views

BaseLoginView and BaseLogoutView are provided. All you need is to override them and to define some lacking methods.

from procyon.user.views import BaseLoginView, BaseLogoutView
from yourapp.security import login

class LoginView(BaseLoginView):
    def do_login(self, user_name, password):
        # you should return a (userid, headers) tuple
        return login(self.request, user_name, password)

    def get_redirect_url(self):
        # return any url you want to redirect to after login
        return self.request.route_url('top')

class LogoutView(BaseLogoutView):
    def get_redirect_url(self):
        # return any url you want to redirect to after logout
        return self.request.route_url('top')

LoginView and LogoutView are now class views ready to register via view_config() or Configurator.add_view().

Note

If you use repoze.who, login() function in the above example can be defined like this:

def login(request, user_name, password):
    from repoze.who.api import get_api
    who_api = get_api(request.environ)

    credentials = {
        'login': user_name,
        'password': password,
        }
    return who_api.login(credentials)

Using panels

procyon also provides some ‘panels’ for pyramid_panels.

There are some extra requirements for this functionality:

  • pyramid_panels
  • pyramid_jinja2
  • Twitter Bootstrap CSS

All available panels are listed here.

panel('flash', queue='')

Show a flash message area. queue is the queue name. Default is "".

This panel requires some session factory to be set up. See the ‘Session’ chapter in the Pyramid documentation for detail.

panel('login_menuitem')

Show a login menu item. A user model must be configured as described in the above section.

If you already enable pyramid_panels, you can then use them with the following configuration:

config.include('procyon.panels')