Example #1
0
def change_avatar():
    if request.method == 'POST' and 'avatar' in request.files:
        avatar_file = request.files['avatar']
        if avatar_file.filename == '':
            flash(lazy_gettext('No selected file'), 'danger')
            return redirect(request.url)
        delete_avatar()
        filename = avatars.save(avatar_file,
                                name='{}.'.format(current_user.id))
        image = os.path.join(current_app.config['UPLOADS_DEFAULT_DEST'],
                             'avatars', filename)
        if image.split('.')[-1].lower() not in ['jpg', 'jpeg', 'gif', 'png']:
            flash(lazy_gettext('Wrong image format.'))
            return redirect(url_for('profile.change_avatar'))
        im = Image.open(image)
        rgb_im = im.convert('RGB')
        rgb_im.thumbnail((64, 64), Image.BICUBIC)
        rgb_im.save(
            os.path.join(current_app.config['UPLOADS_DEFAULT_DEST'], 'avatars',
                         f'{current_user.id}_thumbnail.jpg'), "JPEG")
        current_user.avatar = filename
        current_user.avatar_version = int(time.time())
        db.session.commit()
        flash(lazy_gettext("Avatar saved."))
        return redirect(url_for('profile.change_avatar'))

    avatar_url = avatars.url(
        current_user.avatar) if current_user.avatar else avatars.url(
            'default.png')

    return render_template('profile/change_avatar.html',
                           avatar_url=avatar_url,
                           title=lazy_gettext('Avatar Settings'))
Example #2
0
def change_avatar():
    if request.method == 'POST' and 'avatar' in request.files:
        delete_avatar()
        filename = avatars.save(request.files['avatar'], name='{}.'.format(current_user.id))
        current_user.avatar = filename
        db.session.commit()
        flash(lazy_gettext("Avatar saved."))
        return redirect(url_for('profile.change_avatar'))

    avatar_url = avatars.url(current_user.avatar) if current_user.avatar else avatars.url('default.png')

    return render_template('profile/change_avatar.html', avatar_url=avatar_url, title=lazy_gettext('Avatar Settings'))