When users are signed up through Python Social Auth, they get created as users in my database, which is fine and dandy, however I want them to be added to a certain group upon user creation.
How to do that?
When users are signed up through Python Social Auth, they get created as users in my database, which is fine and dandy, however I want them to be added to a certain group upon user creation.
How to do that?
I'm geeting the same issues trying to add a user in a group using python-social-auth
The error: 'function' object has no attribute connect
Here is my myapp.pipeline.py
from django.db.models import signals
from django.dispatch import Signal
from social.pipeline.user import *
from django.contrib.auth.models import User, Group
from social.utils import module_member
def new_users_handler(sender, user, response, details, **kwargs):
user.groups.add(Group.objects.get(name='candidates'))
user_details.connect(new_users_handler, sender=None)
And my app.settings.py
SOCIAL_AUTH_PIPELINE = (
'social.pipeline.social_auth.social_details',
'social.pipeline.social_auth.social_uid',
'social.pipeline.social_auth.auth_allowed',
'social.pipeline.social_auth.social_user',
'social.pipeline.social_auth.associate_by_email',
'social.pipeline.user.get_username',
'social.pipeline.user.create_user',
'social.pipeline.social_auth.associate_user',
'social.pipeline.social_auth.load_extra_data',
'social.pipeline.user.user_details',
'companies.pipeline.new_users_handler',
)
So to make a workaround, I did the following:
Step 1: Kept the python-social-auth pipeline (without 'companies.pipeline.new_users_handler')
Step 2: in settings.py added SOCIAL_AUTH_NEW_USER_REDIRECT_URL = '/social/success/'
Step 3: added a url for the /social/success/
Step 4: added a function in views.py like
@login_required(login_url="/login")
def socialsuccess(request):
defaultgroup = Group.objects.get(name = 'name')
user = request.user
user.groups.add(defaultgroup)
return render(request, 'app/social-success.html')
It works just fine. But I still think this is not the best approach.
Regards
For:
You need to create a file to hold your custom pipelines (eg company.social_auth_pipelines.py) and then add to that the following function:
from django.contrib.auth.models import Group
def new_users_handler(backend, user, response, *args, **kwargs):
user.groups.add(Group.objects.get(name='students'))
You then need to register the pipeline with social core, so in your settings.py, find the SOCIAL_AUTH_PIPELINE entry and add your pipeline to it. I added mine just below social_core.pipeline.social_auth.associate_user eg:
...
'social_core.pipeline.social_auth.associate_user',
'company.social_auth_pipelines.new_users_handler',
'social_core.pipeline.social_auth.load_extra_data',
...
There's a new user signal on django-social-auth
from social_auth.signals import socialauth_registered
def new_users_handler(sender, user, response, details, **kwargs):
user.groups.add(Group.objects.get(name='mygroup'))
socialauth_registered.connect(new_users_handler, sender=None)
This code is not tested (I'm not on my computer now) but it should give you an idea.
For those who are using Python Social Auth and might need to add User to some Group, here is the code I use:
#pipeline.py
from django.contrib.auth.models import Group
def save_to_group(backend, user, response, *args, **kwargs):
defaultgroup = Group.objects.get(name = 'blah')
user.groups.add(defaultgroup)
And add it to SOCIAL_AUTH_PIPELINE
#settings.py
SOCIAL_AUTH_PIPELINE = {
...
'app_name.pipeline.save_to_group'
}