def actually_sync_profile(handle, user=None, hide_profile=True): from dashboard.models import Profile handle = handle.strip().replace('@', '').lower() if user and hasattr(user, 'profile'): try: access_token = user.social_auth.filter( provider='github').latest('pk').access_token data = get_user(handle, token=access_token) user = User.objects.get(username__iexact=handle) if data and data.login: profile = user.profile user.username = data.login user.save() profile.handle = data.login profile.email = user.email profile.save() except Exception as e: logger.error(e) return None else: data = get_user(handle) email = '' is_error = not hasattr(data, 'name') if is_error: print("- error main") logger.warning(f'Failed to fetch github username {handle}', exc_info=True, extra={'handle': handle}) return None defaults = {'last_sync_date': timezone.now(), 'data': data.raw_data} if user and isinstance(user, User): defaults['user'] = user try: defaults['github_access_token'] = user.social_auth.filter( provider='github').latest('pk').access_token if user and user.email: defaults['email'] = user.email except UserSocialAuth.DoesNotExist: pass # store the org info in postgres try: profile_exists = Profile.objects.filter(handle=handle).count() if not profile_exists: defaults['hide_profile'] = hide_profile profile, created = Profile.objects.update_or_create(handle=handle, defaults=defaults) latest_obj = profile.user.social_auth.filter( provider='github').latest('pk') if profile.user else None access_token = latest_obj.access_token if latest_obj else None orgs = get_user(handle, token=access_token).get_orgs() profile.organizations = [ele.login for ele in orgs if ele] if orgs else [] print("Profile:", profile, "- created" if created else "- updated") keywords = [] if get_user(handle): for repo in get_user(handle).get_repos(): language = repo.language or '' _keywords = language.split(',') for key in _keywords: if key != '' and key not in keywords: keywords.append(key) profile.keywords = keywords profile.save() except UserSocialAuth.DoesNotExist: pass except Exception as e: logger.exception(e) return None if user and user.email: email = user.email elif profile and profile.email: email = profile.email if email and profile: profile.email = email profile.save() get_or_save_email_subscriber(email, 'sync_profile', profile=profile) if profile and not profile.github_access_token: token = profile.get_access_token(save=False) profile.github_access_token = token profile.save() if profile and not profile.avatar_baseavatar_related.last(): github_avatar_img = get_user_github_avatar_image(profile.handle) if github_avatar_img: try: github_avatar = SocialAvatar.github_avatar( profile, github_avatar_img) github_avatar.save() profile.activate_avatar(github_avatar.pk) profile.save() except Exception as e: logger.warning( f'Encountered ({e}) while attempting to save a user\'s github avatar' ) return profile
def sync_profile(handle, user=None, hide_profile=True): from dashboard.models import Profile handle = handle.strip().replace('@', '').lower() data = get_user(handle) email = '' is_error = 'name' not in data.keys() if is_error: print("- error main") logger.warning('Failed to fetch github username', exc_info=True, extra={'handle': handle}) return None defaults = { 'last_sync_date': timezone.now(), 'data': data, 'hide_profile': hide_profile, } if user and isinstance(user, User): defaults['user'] = user try: defaults['github_access_token'] = user.social_auth.filter( provider='github').latest('pk').access_token if user and user.email: defaults['email'] = user.email except UserSocialAuth.DoesNotExist: pass # store the org info in postgres try: profile, created = Profile.objects.update_or_create(handle=handle, defaults=defaults) print("Profile:", profile, "- created" if created else "- updated") orgs = get_user(handle, '/orgs') profile.organizations = [ele['login'] for ele in orgs] keywords = [] for repo in profile.repos_data_lite: language = repo.get('language') if repo.get('language') else '' _keywords = language.split(',') for key in _keywords: if key != '' and key not in keywords: keywords.append(key) profile.keywords = keywords profile.save() except Exception as e: logger.error(e) return None if user and user.email: email = user.email elif profile and profile.email: email = profile.email if email and profile: get_or_save_email_subscriber(email, 'sync_profile', profile=profile) if profile and not profile.github_access_token: token = profile.get_access_token(save=False) profile.github_access_token = token profile.save() if profile and not profile.avatar_baseavatar_related.last(): github_avatar_img = get_user_github_avatar_image(profile.handle) if github_avatar_img: try: github_avatar = SocialAvatar.github_avatar( profile, github_avatar_img) github_avatar.save() profile.activate_avatar(github_avatar.pk) profile.save() except Exception as e: logger.warning( f'Encountered ({e}) while attempting to save a user\'s github avatar' ) if profile and created: profile.build_random_avatar() return profile
def sync_profile(handle, user=None, hide_profile=True): from dashboard.models import Profile handle = handle.strip().replace('@', '').lower() # data = get_user(handle, scoped=True) if user and hasattr(user, 'profile'): try: access_token = user.social_auth.filter( provider='github').latest('pk').access_token data = get_user(handle, '', scoped=True, auth=(handle, access_token)) user = User.objects.get(username__iexact=handle) if 'login' in data: profile = user.profile user.username = data['login'] user.save() profile.handle = data['login'] profile.email = user.email profile.save() if profile is not None and ( profile.chat_id is '' or profile.gitcoin_chat_access_token is ''): try: from chat.tasks import associate_chat_to_profile # created, profile = associate_chat_to_profile(profile) except Exception as e: logger.error(str(e)) raise ValueError(e) except UserSocialAuth.DoesNotExist: pass else: data = get_user(handle) email = '' is_error = 'name' not in data.keys() if is_error: print("- error main") logger.warning(f'Failed to fetch github username {handle}', exc_info=True, extra={'handle': handle}) return None defaults = {'last_sync_date': timezone.now(), 'data': data} if user and isinstance(user, User): defaults['user'] = user try: defaults['github_access_token'] = user.social_auth.filter( provider='github').latest('pk').access_token if user and user.email: defaults['email'] = user.email except UserSocialAuth.DoesNotExist: pass # store the org info in postgres try: profile_exists = Profile.objects.filter(handle=handle).count() if not profile_exists: defaults['hide_profile'] = hide_profile profile, created = Profile.objects.update_or_create(handle=handle, defaults=defaults) access_token = profile.user.social_auth.filter( provider='github').latest('pk').access_token orgs = get_user(handle, '/orgs', auth=(profile.handle, access_token)) profile.organizations = [ ele['login'] for ele in orgs if ele and type(ele) is dict ] if orgs else [] print("Profile:", profile, "- created" if created else "- updated") keywords = [] for repo in profile.repos_data_lite: language = repo.get('language') if repo.get('language') else '' _keywords = language.split(',') for key in _keywords: if key != '' and key not in keywords: keywords.append(key) profile.keywords = keywords profile.save() except UserSocialAuth.DoesNotExist: pass except Exception as e: logger.exception(e) return None if user and user.email: email = user.email elif profile and profile.email: email = profile.email if email and profile: profile.email = email profile.save() get_or_save_email_subscriber(email, 'sync_profile', profile=profile) if profile and not profile.github_access_token: token = profile.get_access_token(save=False) profile.github_access_token = token profile.save() if profile and not profile.avatar_baseavatar_related.last(): github_avatar_img = get_user_github_avatar_image(profile.handle) if github_avatar_img: try: github_avatar = SocialAvatar.github_avatar( profile, github_avatar_img) github_avatar.save() profile.activate_avatar(github_avatar.pk) profile.save() except Exception as e: logger.warning( f'Encountered ({e}) while attempting to save a user\'s github avatar' ) return profile