예제 #1
0
def save_profile(backend, user, response, request, *args, **kwargs):
    """Associate a Profile with a User."""
    if backend.name == 'github':
        handle = user.username

        # BLOCKEDUSER BASED BLOCKING
        # COUNTRY-BASED BASED BLOCKING
        if is_blocked(handle):
            raise SuspiciousOperation('You cannot login')

        # INACTIVE USERS
        if not user.is_active:
            raise SuspiciousOperation('You cannot login')

        ## IP BASED BLOCKING
        from retail.helpers import get_ip
        ip_addr = get_ip(request)
        from dashboard.models import BlockedIP
        _is_blocked = BlockedIP.objects.filter(addr=ip_addr).exists()
        if _is_blocked:
            raise SuspiciousOperation('You cannot login')

        # SUCCESS, LET USER LOGIN
        sync_profile(handle, user, hide_profile=False, delay_okay=True)
        setup_lang(request, user)
예제 #2
0
def handle_avatar(request, _org_name='', add_gitcoincologo=False):
    from dashboard.models import Profile
    icon_size = (215, 215)

    if _org_name:
        _org_name = _org_name.replace('@', '')

    if is_blocked(_org_name) or is_deleted_account(_org_name):
        return get_err_response(request, blank_img=(_org_name == 'Self'))

    if _org_name:
        try:
            profile = Profile.objects.prefetch_related('avatar_baseavatar_related')\
                .filter(handle=_org_name.lower()).first()
            if profile and profile.active_avatar_nocache:
                avatar_file, content_type = profile.active_avatar_nocache.determine_response(
                    request.GET.get('email', False)
                )
                if avatar_file:
                    return HttpResponse(avatar_file, content_type=content_type)
        except Exception as e:
            logger.error('Handle Avatar - Exception: (%s) - Handle: (%s)', str(e), _org_name)
            logger.exception(e)

    # default response
    # params
    repo_url = request.GET.get('repo', False)
    if not _org_name and (not repo_url or 'github.com' not in repo_url):
        return get_err_response(request, blank_img=(_org_name == 'Self'))

    try:
        # get avatar of repo
        if not _org_name:
            _org_name = org_name(repo_url)

        filepath = get_avatar(_org_name)
        if isinstance(filepath, JsonResponse):
            raise AvatarNotFoundException('no avatar found')
        # new image
        img = Image.new('RGBA', icon_size, (255, 255, 255))

        # execute
        avatar = Image.open(filepath, 'r').convert("RGBA")
        avatar = ImageOps.fit(avatar, icon_size, Image.ANTIALIAS)
        offset = 0, 0
        img.paste(avatar, offset, avatar)

        # Determine if we should add the Gitcoin logo
        if add_gitcoincologo and _org_name != 'gitcoinco':
            img = add_gitcoin_logo_blend(avatar, icon_size)

        response = HttpResponse(content_type='image/png')
        img.save(response, 'PNG')
        return response
    except AvatarNotFoundException:
        return get_err_response(request, blank_img=(_org_name == 'Self'))
    except (AttributeError, IOError, SyntaxError) as e:
        logger.error('Handle Avatar - Response error: (%s) - Handle: (%s)', str(e), _org_name)
        logger.exception(e)
        return get_err_response(request, blank_img=(_org_name == 'Self'))
예제 #3
0
def save_profile(backend, user, response, request, *args, **kwargs):
    """Associate a Profile with a User."""
    if backend.name == 'github':
        handle = user.username
        if is_blocked(handle):
            raise SuspiciousOperation('You cannot login')

        if not user.is_active:
            raise SuspiciousOperation('You cannot login')

        sync_profile(handle, user, hide_profile=False)
        setup_lang(request, user)
예제 #4
0
    def handle(self, *args, **options):
        # setup
        handles = set([b.org_name for b in Bounty.objects.current()])
        for handle in handles:
            handle = handle.lower()
            print(handle)
            if is_blocked(handle)or is_deleted_account(handle):
                print('not syncing, handle is blocked')
                continue

            # does this handle need a refresh
            needs_refresh = does_need_refresh(handle) or options['force_refresh']

            if not needs_refresh:
                print('- no refresh needed')
            else:
                try:
                    sync_profile(handle)
                except Exception as e:
                    print(e)

            if not settings.DEBUG:
                time.sleep(60)
예제 #5
0
파일: helpers.py 프로젝트: vsramanujan/web
def handle_bounty_fulfillments(fulfillments, new_bounty, old_bounty):
    """Handle BountyFulfillment creation for new bounties.

    Args:
        fulfillments (dict): The fulfillments data dictionary.
        new_bounty (dashboard.models.Bounty): The new Bounty object.
        old_bounty (dashboard.models.Bounty): The old Bounty object.

    Returns:
        QuerySet: The BountyFulfillments queryset.

    """
    from dashboard.utils import is_blocked
    for fulfillment in fulfillments:
        kwargs = {}
        accepted_on = None
        github_username = fulfillment.get('data', {}).get('payload', {}).get(
            'fulfiller', {}).get('githubUsername', '')
        if github_username:
            if is_blocked(github_username):
                continue
            try:
                kwargs['profile_id'] = Profile.objects.get(
                    handle__iexact=github_username).pk
            except Profile.MultipleObjectsReturned:
                kwargs['profile_id'] = Profile.objects.filter(
                    handle__iexact=github_username).first().pk
            except Profile.DoesNotExist:
                pass
        if fulfillment.get('accepted'):
            kwargs['accepted'] = True
            accepted_on = timezone.now()
        try:
            created_on = timezone.now()
            modified_on = timezone.now()
            if old_bounty:
                old_fulfillments = old_bounty.fulfillments.filter(
                    fulfillment_id=fulfillment.get('id')).nocache()
                if old_fulfillments.exists():
                    old_fulfillment = old_fulfillments.first()
                    created_on = old_fulfillment.created_on
                    modified_on = old_fulfillment.modified_on
                    if old_fulfillment.accepted:
                        accepted_on = old_fulfillment.accepted_on
            hours_worked = fulfillment.get('data', {}).get('payload', {}).get(
                'fulfiller', {}).get('hoursWorked', None)
            if not hours_worked or not hours_worked.isdigit():
                hours_worked = None
            new_bounty.fulfillments.create(
                fulfiller_address=fulfillment.get(
                    'fulfiller', '0x0000000000000000000000000000000000000000'),
                fulfiller_email=fulfillment.get('data', {}).get(
                    'payload', {}).get('fulfiller', {}).get('email', ''),
                fulfiller_github_username=github_username,
                fulfiller_name=fulfillment.get('data', {}).get(
                    'payload', {}).get('fulfiller', {}).get('name', ''),
                fulfiller_metadata=fulfillment,
                fulfillment_id=fulfillment.get('id'),
                fulfiller_github_url=fulfillment.get('data', {}).get(
                    'payload', {}).get('fulfiller',
                                       {}).get('githubPRLink', ''),
                fulfiller_hours_worked=hours_worked,
                created_on=created_on,
                modified_on=modified_on,
                accepted_on=accepted_on,
                **kwargs)
        except Exception as e:
            logger.error(
                f'{e} during new fulfillment creation for {new_bounty}')
            continue
    return new_bounty.fulfillments.all()
예제 #6
0
def handle_bounty_fulfillments(fulfillments, new_bounty, old_bounty):
    """Handle BountyFulfillment creation for new bounties.

    Args:
        fulfillments (dict): The fulfillments data dictionary.
        new_bounty (dashboard.models.Bounty): The new Bounty object.
        old_bounty (dashboard.models.Bounty): The old Bounty object.

    Returns:
        QuerySet: The BountyFulfillments queryset.

    """
    from dashboard.utils import is_blocked

    for fulfillment in fulfillments:
        fulfillment_id = fulfillment.get('id')
        old_fulfillment = None
        try:
            old_fulfillment = BountyFulfillment.objects.get(
                bounty=old_bounty, fulfillment_id=fulfillment_id)

        except MultipleObjectsReturned as error:
            logger.warning(
                f'error: found duplicate fulfillments for bounty {old_bounty} {error}'
            )
            old_bounty_fulfillments = BountyFulfillment.objects.filter(
                fulfillment_id=fulfillment_id, bounty=old_bounty).nocache()
            if old_bounty_fulfillments.exists():
                old_fulfillment = old_bounty_fulfillments.first()

        except BountyFulfillment.DoesNotExist as error:
            logger.warning(
                f'info: bounty {old_bounty} has no fulfillments in db {error}')

        if old_fulfillment:
            if not old_fulfillment.accepted and fulfillment.get('accepted'):
                # update fulfillment to accepted + reference to new bounty
                now = timezone.now()
                old_fulfillment.modified_on = now
                old_fulfillment.accepted_on = now
                old_fulfillment.accepted = True
            old_fulfillment.bounty = new_bounty
            old_fulfillment.save()
        else:
            # create new fulfillment object
            kwargs = {}
            accepted_on = None
            github_username = fulfillment.get('data', {}).get(
                'payload', {}).get('fulfiller', {}).get('githubUsername', '')
            if github_username:
                if is_blocked(github_username):
                    continue
                try:
                    kwargs['profile_id'] = Profile.objects.get(
                        handle=github_username.lower()).pk
                except Profile.MultipleObjectsReturned:
                    kwargs['profile_id'] = Profile.objects.filter(
                        handle=github_username.lower()).first().pk
                except Profile.DoesNotExist:
                    pass
            if fulfillment.get('accepted'):
                kwargs['accepted'] = True
                accepted_on = timezone.now()
            try:
                created_on = timezone.now()
                modified_on = timezone.now()
                fulfiller_github_url = fulfillment.get('data', {}).get(
                    'payload', {}).get('fulfiller',
                                       {}).get('githubPRLink', '')
                hours_worked = fulfillment.get('data', {}).get(
                    'payload', {}).get('fulfiller',
                                       {}).get('hoursWorked', None)
                fulfiller_address = fulfillment.get(
                    'fulfiller', '0x0000000000000000000000000000000000000000')
                if not hours_worked.isdigit():
                    hours_worked = None

                new_bounty.fulfillments.create(
                    funder_profile=new_bounty.bounty_owner_profile,
                    funder_address=new_bounty.bounty_owner_address,
                    payout_type='bounties_network',
                    tenant='ETH',
                    token_name=new_bounty.token_name,
                    fulfiller_address=fulfiller_address,
                    fulfiller_metadata=fulfillment,
                    fulfillment_id=fulfillment.get('id'),
                    fulfiller_github_url=fulfiller_github_url,
                    fulfiller_hours_worked=hours_worked,
                    created_on=created_on,
                    modified_on=modified_on,
                    accepted_on=accepted_on,
                    **kwargs)
            except Exception as e:
                logger.error(
                    f'{e} during new fulfillment creation for {new_bounty}')
                continue

    old_bounty_fulfillments = BountyFulfillment.objects.filter(
        bounty=old_bounty)
    if old_bounty_fulfillments:
        # fail safe to ensure all fulfillments are migrated over
        for fulfillment in old_bounty_fulfillments:
            fulfillment.bounty = new_bounty.id
            fulfillment.save()

    if new_bounty:
        return new_bounty.fulfillments.all()