Skip to content

Instantly share code, notes, and snippets.

@zvictor
Created November 9, 2011 11:24
Show Gist options
  • Save zvictor/1351169 to your computer and use it in GitHub Desktop.
Save zvictor/1351169 to your computer and use it in GitHub Desktop.
An example how to connect userena with django-social registration.
from socialregistration.signals import connect as profile_connect
from userena.managers import ASSIGNED_PERMISSIONS
@receiver(socialregistration_signals.connect, sender = FacebookProfile, dispatch_uid = 'facebook.connect')
def social_connect_callback(sender, user, profile, client, **kwargs):
"""
Create a profile for this user after connecting
"""
# Create a userena user.
# TODO: You could make it prettier by setting a ``activation_key`` of ``ALREADY_ACTIVATED``
# and looking at good values for the other fields of the model.
userenaSignup = UserenaSignup.objects.get_or_create(user=user)
# Create profile for user
try:
new_profile = Profile.objects.get(user=user)
except:
new_profile = Profile.objects.create(user=user)
# Give permissions to view and change profile
for perm in ASSIGNED_PERMISSIONS['profile']:
assign(perm[0], user, new_profile)
# Give permissions to view and change itself
for perm in ASSIGNED_PERMISSIONS['user']:
assign(perm[0], user, user)
@ccfiel
Copy link

ccfiel commented May 31, 2012

I am new with django-social registration from userena. so I need this to copy in my projects model.py? or to usererna model.py?

@zvictor
Copy link
Author

zvictor commented May 31, 2012

copy it into your project. It doesn't necessarily needs to be in your models, but it is usually there. You can create a sygnals.py or receivers.py and import it from init.py or from somewhere else.

The idea of signals is that you don't need to change the core or third-party apps when you want to change their behaviors. So, don't putting it inside of Userena is a good pattern. If you want to change userena, there is better ways than using signals.

@ccfiel
Copy link

ccfiel commented May 31, 2012

thanks zvictor. I have successfully implemented it. but django-social is lack of features. so i go with django-social-auth is has more plugin and function implemented. Thanks again.

@zvictor
Copy link
Author

zvictor commented Jun 1, 2012

I understand, but what features do you miss?

@ccfiel
Copy link

ccfiel commented Jun 1, 2012

Maybe its not missing in the django-social but you need to implemented it by yourself. example in django-social-auth the last name, first name is default populated in the auth_user table. also profile picture can easily be put to mugshot field in userena.

@zvictor
Copy link
Author

zvictor commented Jun 1, 2012

I tried this before, but got tired. None is enough good.
I am really thinking about remove it before launch my website to avoid headaches.

@iskorum
Copy link

iskorum commented Dec 15, 2013

what is the assign function here? or where it is?

@spenoir
Copy link

spenoir commented Feb 27, 2014

use this instead as assign is deprecated:

from guardian.shortcuts import assign_perm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment