Пример #1
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'))
Пример #2
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)