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': {}})
评论列表
文章目录