2016-11-18 5 views
0

Setup-django django-allauth extra_data aus sozialer Login in Signal speichert

Ich verwende Django 1.8.15 und django-allauth 0.28.0

Beschreibung

Was ich will, zu tun ist ziemlich einfach zu erklären: Wenn ein Benutzer sich über soziale Medien (Facebook, Google + oder Twitter) anmeldet, möchte ich die extra_data von sociallogin abrufen, um die URL zum Avatar und andere Informationen zu erhalten, um sie zu speichern mein Profile, das ist ein One-to-one relation zu meinem User Modell.

Meine Ansätze

1)

Zuerst habe ich eine Klasse mit pre_social_login wie here, die nach der Benutzer über Social Media unterzeichnet hat, in aufgerufen wird hinzugefügt.

models.py

class SocialAdapter(DefaultSocialAccountAdapter): 
def pre_social_login(self, request, sociallogin): 
    """Get called after a social login. check for data and save what you want.""" 
    user = User.objects.get(id=request.user.id) # Error: user not available 
    profile = Profile(user=user) 
    picture = sociallogin.account.extra_data.get('picture', None) 
    if picture: 
     # ... code to save picture to profile 

settings.py

SOCIALACCOUNT_ADAPTER = 'profile.models.SocialAdapter' 

Ich möchte die Instanz von dem Benutzer erhalten, aber es scheint, dass es noch nicht erstellt wurde. Also versuchte ich folgendes:

2)

Ich habe ein anderes Signal, die ein Profil erstellt, wenn ein neuer Benutzer hinzugefügt wird:

models.py

@receiver(post_save, sender=settings.AUTH_USER_MODEL) 
def create_profile_for_new_user(sender, created, instance, **kwargs): 
    """Signal, that creates a new profile for a new user.""" 
    if created: 
     profile = Profile(user=instance) 
     # following code was pasted here 
     profile.save() 

I habe folgendes hinzugefügt:

data = SocialAccount.objects.filter(user=instance) 
# do more with data 

aber data es ist immer leer []

Ich habe Probleme, das zu verstehen. Ich meine, wenn ein Benutzer sich über soziale Medien anmeldet und ich kann nicht auf den Benutzer von User (Fall 1) zugreifen, weil es noch nicht erstellt wurde, sollte die SocialAccount erstellt worden sein, wenn ich versuche, es in Fall 2 zu bekommen ?! Haben Sie noch andere Ideen, um das zu lösen? Oder verpasse ich hier etwas?

Vielen Dank im Voraus

Antwort

0

bekam es nach einigen Stunden der Verzweiflung.

ich verwendet habe, nur ein anderes Signal user_signed_up, nicht derjenige, der socialaccount

from allauth.account.signals import user_signed_up 

@receiver(user_signed_up) 
def retrieve_social_data(request, user, **kwargs): 
    """Signal, that gets extra data from sociallogin and put it to profile.""" 
    # get the profile where i want to store the extra_data 
    profile = Profile(user=user) 
    # in this signal I can retrieve the obj from SocialAccount 
    data = SocialAccount.objects.filter(user=user, provider='facebook') 
    # check if the user has signed up via social media 
    if data: 
     picture = data[0].get_avatar_url() 
     if picture: 
      # custom def to save the pic in the profile 
      save_image_from_url(model=profile, url=picture) 
     profile.save() 

http://django-allauth.readthedocs.io/en/latest/signals.html#allauth-account