(handle, user=None, hide_profile=True)
| 173 | |
| 174 | |
| 175 | def sync_profile(handle, user=None, hide_profile=True): |
| 176 | handle = handle.strip().replace('@', '') |
| 177 | data = get_user(handle) |
| 178 | email = '' |
| 179 | is_error = 'name' not in data.keys() |
| 180 | if is_error: |
| 181 | print("- error main") |
| 182 | logger.warning('Failed to fetch github username', exc_info=True, extra={'handle': handle}) |
| 183 | return None |
| 184 | |
| 185 | defaults = {'last_sync_date': timezone.now(), 'data': data, 'hide_profile': hide_profile, } |
| 186 | |
| 187 | if user and isinstance(user, User): |
| 188 | defaults['user'] = user |
| 189 | try: |
| 190 | defaults['github_access_token'] = user.social_auth.filter(provider='github').latest('pk').access_token |
| 191 | if user and user.email: |
| 192 | defaults['email'] = user.email |
| 193 | except UserSocialAuth.DoesNotExist: |
| 194 | pass |
| 195 | |
| 196 | # store the org info in postgres |
| 197 | try: |
| 198 | profile, created = Profile.objects.update_or_create(handle=handle, defaults=defaults) |
| 199 | print("Profile:", profile, "- created" if created else "- updated") |
| 200 | except Exception as e: |
| 201 | logger.error(e) |
| 202 | return None |
| 203 | |
| 204 | if user and user.email: |
| 205 | email = user.email |
| 206 | elif profile and profile.email: |
| 207 | email = profile.email |
| 208 | |
| 209 | if email and profile: |
| 210 | get_or_save_email_subscriber(email, 'sync_profile', profile=profile) |
| 211 | |
| 212 | if profile and not profile.github_access_token: |
| 213 | token = profile.get_access_token(save=False) |
| 214 | profile.github_access_token = token |
| 215 | profile.save() |
| 216 | |
| 217 | return profile |
| 218 | |
| 219 | |
| 220 | def fetch_last_email_id(email_id, password, host='imap.gmail.com', folder='INBOX'): |
no test coverage detected