Working through the book “Django 5 by Example” by Antonio Melé. I am working on the second project which is “Building a Social Website” called Bookmarks. In Chapter 5, we use python-social-auth to login using Google.
Two issues I am having are on every log in; the users profile picture is downloaded from google in my /media/users/%Y/%m/%d. And the second issue is that I cant display a form after connection to google to allow the user to create their own username. Currently, it will take everything to the left of @example.com and make that the users username. I don’t have code for the username issue right now as I have been playing around for the last 48hrs with it and wiped the code to start over. The profile picture bugs me a little more as the files will start to stack up and cause space issues very quickly.
Is this a normal function of this python package? I’d like to be able to have the user create their own username to display on the website instead of the first part of their email since that can easily be taken advantage of quickly.
here is the pipeline.py
import requests
import urllib.request
from django.core.files import File
from django.core.files.base import ContentFile
def save_profile_picture(backend, user, response, *args, **kwargs):
if backend.name == 'google-oauth2':
url = response['picture'] # The new image URL
profile = user.profile
# Check if the profile has a saved Google image URL
if hasattr(profile, 'google_image_url'):
# Compare the stored Google image URL with the new one
if profile.google_image_url == url:
return # Skip uploading if the URLs match
# At this point, either profile.google_image_url is not set or the URL has changed
response = requests.get(url)
# Save the new image
profile.photo.save(f'{user.username}_google.jpg', ContentFile(response.content), save=False)
# Update the stored Google image URL
profile.google_image_url = url
profile.save()
settings.py > SOCIAL_AUTH_PIPELINE setting
# SOCIAL AUTH PIPELINE
SOCIAL_AUTH_PIPELINE = [
# 'account.pipeline.custom_social_details',
'social_core.pipeline.social_auth.social_details',
'social_core.pipeline.social_auth.social_uid',
'social_core.pipeline.social_auth.auth_allowed',
'social_core.pipeline.social_auth.social_user',
'social_core.pipeline.user.get_username',
'social_core.pipeline.user.create_user',
'account.authentication.create_profile',
# 'account.pipeline.create_username',
'account.pipeline.save_profile_picture',
'social_core.pipeline.social_auth.associate_user',
'social_core.pipeline.social_auth.load_extra_data',
'social_core.pipeline.user.user_details',
]