profile_views.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:Plamber 作者: OlegKlimenko 项目源码 文件源码
def upload_avatar(request):
    """
    Sets new user's avatar.
    """
    with transaction.atomic():
        profile_user = get_object_or_404(TheUser, auth_token=request.data.get('user_token'))

        try:
            profile_user.user_photo.save('user_{}.png'.format(profile_user.id), request.data.get('file'))
            profile_user.save()
            logger.info("User '{}' changed his avatar.".format(profile_user))

            resize_image(profile_user.user_photo.path, AVATAR_WIDTH)
            logger.info("Image '{}' successfully resized!".format(profile_user.user_photo.path))

            return Response({'status': 200,
                             'detail': 'successful',
                             'data': {'profile_image': profile_user.user_photo.url}})

        except ValidationError:
            logger.info("User '{}' tried to upload not an image as avatar!".format(profile_user))

            return Response({'status': 404,
                             'detail': 'tried to upload not an image',
                             'data': {}})
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号