Beispiel #1
0
def avatar_path_handler(instance=None, filename=None, size=None, ext=None):
    tmppath = [settings.AVATAR_STORAGE_DIR]
    if settings.AVATAR_HASH_USERDIRNAMES:
        tmp = hashlib.md5(force_bytes(get_username(instance.user))).hexdigest()
        tmppath.extend(tmp[0:2])
    if settings.AVATAR_EXPOSE_USERNAMES:
        tmppath.append(get_username(instance.user))
    else:
        tmppath.append(force_text(instance.user.pk))
    if not filename:
        # Filename already stored in database
        filename = instance.avatar.name
        if ext and settings.AVATAR_HASH_FILENAMES:
            # An extension was provided, probably because the thumbnail
            # is in a different format than the file. Use it. Because it's
            # only enabled if AVATAR_HASH_FILENAMES is true, we can trust
            # it won't conflict with another filename
            (root, oldext) = os.path.splitext(filename)
            filename = root + "." + ext
    else:
        # File doesn't exist yet
        if settings.AVATAR_HASH_FILENAMES:
            (root, ext) = os.path.splitext(filename)
            if settings.AVATAR_RANDOMIZE_HASHES:
                filename = binascii.hexlify(os.urandom(16)).decode('ascii')
            else:
                filename = hashlib.md5(force_bytes(filename)).hexdigest()
            filename = filename + ext
    if size:
        tmppath.extend(['resized', str(size)])
    tmppath.append(os.path.basename(filename))
    return os.path.join(*tmppath)
Beispiel #2
0
def avatar_path_handler(instance=None, filename=None, size=None, ext=None):
    tmppath = [settings.AVATAR_STORAGE_DIR]
    if settings.AVATAR_HASH_USERDIRNAMES:
        tmp = hashlib.md5(force_bytes(get_username(instance.user))).hexdigest()
        tmppath.extend(tmp[0:2])
    if settings.AVATAR_EXPOSE_USERNAMES:
        tmppath.append(get_username(instance.user))
    else:
        tmppath.append(force_text(instance.user.pk))
    if not filename:
        # Filename already stored in database
        filename = instance.avatar.name
        if ext and settings.AVATAR_HASH_FILENAMES:
            # An extension was provided, probably because the thumbnail
            # is in a different format than the file. Use it. Because it's
            # only enabled if AVATAR_HASH_FILENAMES is true, we can trust
            # it won't conflict with another filename
            (root, oldext) = os.path.splitext(filename)
            filename = root + "." + ext
    else:
        # File doesn't exist yet
        if settings.AVATAR_HASH_FILENAMES:
            (root, ext) = os.path.splitext(filename)
            if settings.AVATAR_RANDOMIZE_HASHES:
                filename = binascii.hexlify(os.urandom(16)).decode('ascii')
            else:
                filename = hashlib.md5(force_bytes(filename)).hexdigest()
            filename = filename + ext
    if size:
        tmppath.extend(['resized', str(size)])
    tmppath.append(os.path.basename(filename))
    return os.path.join(*tmppath)
    def get_avatar_url(self, user, size):
        params = {'s': str(size)}
        if settings.AVATAR_GRAVATAR_DEFAULT:
            params['d'] = settings.AVATAR_GRAVATAR_DEFAULT
        if settings.AVATAR_GRAVATAR_FORCEDEFAULT:
            params['f'] = 'y'
        path = "%s/?%s" % (hashlib.md5(force_bytes(getattr(user,
            settings.AVATAR_GRAVATAR_FIELD))).hexdigest(), urlencode(params))

        return urljoin(settings.AVATAR_GRAVATAR_BASE_URL, path)
Beispiel #4
0
    def get_avatar_url(self, user, size):
        params = {'s': str(size)}
        if settings.AVATAR_GRAVATAR_DEFAULT:
            params['d'] = settings.AVATAR_GRAVATAR_DEFAULT
        if settings.AVATAR_GRAVATAR_FORCEDEFAULT:
            params['f'] = 'y'
        path = "%s/?%s" % (hashlib.md5(
            force_bytes(getattr(user,
                                settings.AVATAR_GRAVATAR_FIELD))).hexdigest(),
                           urlencode(params))

        return urljoin(settings.AVATAR_GRAVATAR_BASE_URL, path)
Beispiel #5
0
def avatar_url(user, size=settings.AVATAR_DEFAULT_SIZE):
    avatar = get_primary_avatar(user, size=size)
    if avatar:
        return avatar.avatar_url(size)

    if settings.AVATAR_GRAVATAR_BACKUP:
        params = {'s': str(size)}
        if settings.AVATAR_GRAVATAR_DEFAULT:
            params['d'] = settings.AVATAR_GRAVATAR_DEFAULT
        if settings.AVATAR_GRAVATAR_FORCEDEFAULT:
            params['f'] = 'y'
        path = "%s/?%s" % (hashlib.md5(force_bytes(getattr(user,
            settings.AVATAR_GRAVATAR_FIELD))).hexdigest(), urlencode(params))
        return urljoin(settings.AVATAR_GRAVATAR_BASE_URL, path)

    if settings.AVATAR_FACEBOOK_BACKUP:
        fb_id = get_facebook_id(user)
        if fb_id:
            return 'https://graph.facebook.com/{fb_id}/picture?type=square&width={size}&height={size}'.format(
                fb_id=fb_id, size=size
            )

    return get_default_avatar_url()