<a id="cherrypy-framework"></a>

# CherryPy Framework

CherryPy framework is supported, it works but I’m sure there’s room for
improvements. The implementation uses SQLAlchemy as ORM and expects some values
accessible on `cherrypy.request` for it to work.

At the moment the configuration is expected on `cherrypy.config` but ideally
it should be an application configuration instead.

Expected values are:

`cherrypy.request.user`
: Current logged in user, load it in your application on a `before_handler`
  handler.

`cherrypy.request.db`
: Current database session, again, load it in your application on
  a `before_handler`.

<a id="dependencies"></a>

## Dependencies

The CherryPy application depends on [sqlalchemy](http://www.sqlalchemy.org/), there’s no support for
others ORMs yet.

<a id="installing"></a>

## Installing

From [pypi](http://pypi.python.org/pypi/social-auth-app-cherrypy/):

```default
$ pip install social-auth-app-cherrypy
```

<a id="enabling-the-application"></a>

## Enabling the application

The application is defined on `social_cherrypy.views.CherryPyPSAViews`,
register it in the preferred way for your project.

Check the rest of the docs for the other settings like enabling authentication
backends and backends keys.

<a id="models-setup"></a>

## Models Setup

The models are located in `social_cherrypy.models`. A reference to
your `User` model is required to be defined in the project settings, it
should be an import path, for example:

```default
cherrypy.config.update({
    'SOCIAL_AUTH_USER_MODEL': 'models.User'
})
```

<a id="login-mechanism"></a>

## Login mechanism

By default the application sets the session value `user_id`, this is a simple
solution and it should be improved, if you want to provider your own login
mechanism you can do it by defining the `SOCIAL_AUTH_LOGIN_METHOD` setting,
it should be an import path to a callable, like this:

```default
SOCIAL_AUTH_USER_MODEL = 'app.login_user'
```

And an example of this function:

```default
def login_user(strategy, user):
    strategy.session_set('user_id', user.id)
```

Then, ensure to load the user in your application at `cherrypy.request.user`,
for example:

```default
def load_user():
    user_id = cherrypy.session.get('user_id')
    if user_id:
        cherrypy.request.user = cherrypy.request.db.query(User).get(user_id)
    else:
        cherrypy.request.user = None


cherrypy.tools.authenticate = cherrypy.Tool('before_handler', load_user)
```
