- Language: en
- Documentation version: 0.1
Pyramid reusable applications are tricky (or I’m not capable enough). Here are details on how to enable this application on Pyramid.
$ pip install social-auth-app-pyramid
Enabling the application¶
The application can be scanned by
Configurator.scan(), also it defines an
includeme() in the
__init__.py file which will add the needed routes to
your application configuration. To scan it just add:
At the moment the models for python-social-auth are defined inside a function
because they need the reference to the current DB instance and the User model
used on your project (check User model reference below). Once the Pyramid
application configuration and database are defined, call
register the models:
from social_pyramid.models import init_social
init_social(config, Base, DBSession)
So far I wasn’t able to find another way to define the models on another way
rather than making it as a side-effect of calling this function since the
database is not available and
current_app cannot be used on initialization
time, just run time.
User model reference¶
The application keeps a reference to the User model used by your project, define it by using this setting:
SOCIAL_AUTH_USER_MODEL = 'foobar.models.User'
The value must be the import path to the User model.
The application expects the current logged in user accessible at
the example application ensures that with this hander:
user_id = request.session.get('user_id')
user = DBSession.query(User)\
.filter(User.id == user_id)\
user = None
The handler is added to the configuration doing:
config.add_request_method('example.auth.get_user', 'user', reify=True)
This is just a simple example, probably your project does it in a better way.
Since the application doesn’t make any assumption on how you are going to login the users, you need to specify it. In order to do that, define these settings:
SOCIAL_AUTH_LOGIN_FUNCTION = 'example.auth.login_user'
SOCIAL_AUTH_LOGGEDIN_FUNCTION = 'example.auth.login_required'
The first one must accept the strategy used and the user instance that was created or retrieved from the database, there you can set the user id in the session or cookies or whatever place used later to retrieve the id again and load the user from the database (check the snippet above in Global User).
The second one is used to ensure that there’s a user logged in when calling the
disconnect view. It must accept a
User instance and return
Check the auth.py in the example application for details on how it’s done there.