Skip to content

Commit

Permalink
Added retrieval of fullname, image from github, google when linking a…
Browse files Browse the repository at this point in the history
…ccounts (#1639)
  • Loading branch information
daveoconnor committed Feb 21, 2025
1 parent b993d55 commit e502e44
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ class User(BaseUser):
# elapsed.
delete_permanently_at = models.DateTimeField(null=True, editable=False)

def save_image_from_github(self, avatar_url):
def save_image_from_provider(self, avatar_url):
response = requests.get(avatar_url)
filename = f"{self.profile_image_filename_root}.png"
os.path.join(settings.MEDIA_ROOT, "media", "profile-images", filename)
Expand Down
21 changes: 14 additions & 7 deletions users/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from users.constants import LOGIN_METHOD_SESSION_FIELD_NAME

GITHUB = "github"
GOOGLE = "google"


@receiver(post_save, sender=SocialAccount)
Expand All @@ -16,19 +17,25 @@ def import_social_profile_data(sender, instance, created, **kwargs):
user's profile
"""
if not created:
# for display name to save this needs to be resaved after the redirect.
instance.user.save()
return

if instance.provider != GITHUB:
if instance.provider not in [GITHUB, GOOGLE]:
return

github_username = instance.extra_data.get("login")
avatar_url = instance.extra_data.get("avatar_url")
if github_username:
instance.user.github_username = github_username
instance.save()
avatar_url = None
if instance.provider == GITHUB:
instance.user.github_username = instance.extra_data.get("login")
avatar_url = instance.extra_data.get("avatar_url")
elif instance.provider == GOOGLE:
avatar_url = instance.extra_data.get("picture")

if avatar_url:
instance.user.save_image_from_github(avatar_url)
instance.user.save_image_from_provider(avatar_url)

instance.user.display_name = instance.extra_data.get("name")
instance.save()


@receiver(user_logged_in)
Expand Down

0 comments on commit e502e44

Please sign in to comment.