def record_bounty_activity(event_name, old_bounty, new_bounty, _fulfillment=None, override_created=None): """Records activity based on bounty changes Args: event_name (string): the event old_bounty (dashboard.models.Bounty): The old Bounty object. new_bounty (dashboard.models.Bounty): The new Bounty object. Raises: Exception: Log all exceptions that occur during fulfillment checks. Returns: dashboard.Activity: The Activity object if user_profile is present or None. """ user_profile = None fulfillment = _fulfillment try: user_profile = Profile.objects.filter( handle=new_bounty.bounty_owner_github_username.lower()).first() funder_actions = [ 'new_bounty', 'worker_approved', 'killed_bounty', 'increased_bounty', 'worker_rejected', 'bounty_changed' ] if event_name not in funder_actions: if not fulfillment: if new_bounty.fulfillments.exists(): fulfillment = new_bounty.fulfillments.order_by( '-pk').first() if event_name == 'work_done': fulfillment = new_bounty.fulfillments.filter( accepted=True).latest('fulfillment_id') if fulfillment: user_profile = Profile.objects.filter( handle=fulfillment.fulfiller_github_username.lower( )).first() if not user_profile: user_profile = sync_profile( fulfillment.fulfiller_github_username) except Exception as e: logger.error(f'{e} during record_bounty_activity for {new_bounty}') if user_profile: if event_name in bounty_activity_event_adapter: event = BountyEvent.objects.create( bounty=new_bounty, event_type=bounty_activity_event_adapter[event_name], created_by=user_profile) new_bounty.handle_event(event) return Activity.objects.create( created_on=timezone.now() if not override_created else override_created, profile=user_profile, activity_type=event_name, bounty=new_bounty, metadata={ 'new_bounty': get_bounty_data_for_activity(new_bounty) if new_bounty else None, 'old_bounty': get_bounty_data_for_activity(old_bounty) if old_bounty else None, 'fulfillment': get_fulfillment_data_for_activity(fulfillment) if fulfillment else None, }) return None
def settings_helper_get_auth(request, key=None): # setup github_handle = request.user.username if request.user.is_authenticated else False is_logged_in = bool(request.user.is_authenticated) es = EmailSubscriber.objects.none() # check if user's email has changed if request.user.is_authenticated: current_email = get_github_primary_email( request.user.profile.github_access_token) if current_email != request.user.profile.email: request.user.profile.email = current_email request.user.profile.save() if current_email != request.user.email: request.user.email = current_email request.user.save() # find the user info if key is None or not EmailSubscriber.objects.filter(priv=key).exists(): email = request.user.email if request.user.is_authenticated else None if not email: github_handle = request.user.username if request.user.is_authenticated else None if hasattr(request.user, 'profile'): if request.user.profile.email_subscriptions.exists(): es = request.user.profile.email_subscriptions.first() profile_email = request.user.profile.email if es.email != profile_email \ and not EmailSubscriber.objects.filter(email=profile_email).exists(): es.email = request.user.profile.email es.save() if not es or es and not es.priv: es = get_or_save_email_subscriber(request.user.email, 'settings', profile=request.user.profile) else: try: es = EmailSubscriber.objects.get(priv=key) email = es.email except EmailSubscriber.DoesNotExist: pass # lazily create profile if needed profiles = Profile.objects.none() if github_handle: profiles = Profile.objects.prefetch_related('alumni').filter( handle=github_handle.lower()) profile = None if not profiles.exists() else profiles.first() if not profile and github_handle: profile = sync_profile(github_handle, user=request.user) # lazily create email settings if needed if not es: if request.user.is_authenticated and request.user.email: es = EmailSubscriber.objects.create( email=request.user.email, source='settings_page', profile=request.user.profile, ) es.set_priv() es.save() return profile, es, request.user, is_logged_in
from app.utils import sync_profile from avatar.utils import get_avatar from dashboard.models import Profile handle='proofsuite' profile = Profile.objects.get(handle=handle) avatar = profile.avatar_baseavatar_related.first() if avatar: avatar.delete() sync_profile(profile.handle)
def save_profile(backend, user, response, request, *args, **kwargs): """Associate a Profile with a User.""" if backend.name == 'github': handle = user.username sync_profile(handle, user, hide_profile=False) setup_lang(request, user)
def recursive_sync(lsynced, handle): try: if handle not in lsynced: print(f'Syncing User Handle: {handle}') profile = sync_profile(handle) print('Profile from sync') print(profile) access_token = profile.user.social_auth.filter( provider='github').latest('pk').access_token print('Removing Stale Organizations and Groups') remove_org_groups = [ x for x in profile.profile_organizations.all() if x.name not in profile.organizations ] for y in remove_org_groups: profile.profile_organizations.remove(y) profile.user.groups.filter( name__contains=y.name).delete() print( f'Removing: {profile.handle} from Organization: {y.name} ' ) user_access_repos = get_repo(handle, '/repos', (handle, access_token), is_user=True) # Question around user repo acccess if we can't get user repos, should we assume all repos are no longer available in the platform? if 'message' in user_access_repos: print(user_access_repos['message']) return lsynced current_user_repos = [] for y in user_access_repos: current_user_repos.append(y['name']) remove_user_repos_names = [ x for x in profile.repos.all() if x.name not in current_user_repos ] remove_user_repos = Repo.objects.filter( name__in=remove_user_repos_names, profile__handle=handle) for y in remove_user_repos: profile.repos.remove(y) lsynced.append(handle) else: return lsynced members_to_sync = [] if profile.organizations is None: print("no organizations to sync") return [] for org in profile.organizations: try: if org in orgs_synced: print(f'{org} has been synced already') continue orgs_synced.append(org) db_org = Organization.objects.get_or_create( name=org)[0] print(f'Syncing Organization: {db_org.name}') profile.profile_organizations.add(db_org) org_members = get_organization(db_org.name, '/members', (handle, access_token)) if 'message' in org_members: print(org_members['message']) continue for member in org_members: try: membership = get_organization( db_org.name, f'/memberships/{member["login"]}', (handle, access_token)) if 'message' in membership: print(membership['message']) continue role = membership[ 'role'] if 'role' in membership else "member" db_group = Group.objects.get_or_create( name=f'{db_org.name}-role-{role}')[0] db_org.groups.add(db_group) member_profile_obj = Profile.objects.get( handle=member['login'], user__is_active=True) member_profile_obj.user.groups.add(db_group) members_to_sync.append(member['login']) except Exception as e: # print(f'An exception happened in the Organization Loop: handle {member["login"]} {e}') continue org_repos = get_organization(db_org.name, '/repos', (handle, access_token)) if 'message' in org_repos: print(org_repos['message']) continue for repo in org_repos: db_repo = Repo.objects.get_or_create( name=repo['name'])[0] db_org.repos.add(db_repo) print(f'Syncing Repo: {db_repo.name}') repo_collabs = get_repo(repo['full_name'], '/collaborators', (handle, access_token)) if 'message' in repo_collabs: print(repo_collabs['message']) continue for collaborator in repo_collabs: if collaborator['permissions']['admin']: permission = "admin" elif collaborator['permissions']['push']: permission = "write" elif collaborator['permissions']['pull']: permission = "pull" else: permission = "none" db_group = Group.objects.get_or_create( name= f'{db_org.name}-repo-{repo["name"]}-{permission}' )[0] db_org.groups.add(db_group) try: member_user_profile = Profile.objects.get( handle=collaborator['login'], user__is_active=True) member_user_profile.user.groups.add( db_group) member_user_profile.repos.add(db_repo) if collaborator['login'] not in members_to_sync or \ collaborator['login'] not in lsynced: members_to_sync.append( collaborator['login']) except Exception as e: continue for x in members_to_sync: try: lsynced = lsynced + recursive_sync(lsynced, x) except Exception as e: # print(f'An exception happened in the Members sync Loop: handle: {handle} {e}') continue except Exception as e: # print(f'An exception happened in the Organization Loop: handle {handle} {e}') continue except Exception as exc: print(f'Exception occurred inside recursive_sync: {exc}') return lsynced