django-awesome-avatar
django-awesome-avatar copied to clipboard
AvatarWidget Does Not Perform Well When File Not Selected
I happened upon a couple of issues in "awesome_avatar/widgets.py". I setup a form for one to update his/her profile with a new avatar. If one does not select a file, and the submits the form, an error occurs trying to convert a string to a float in the value_from_datadict method. The "data.get(name + '-ratio', 1)" returns an empty string instead of 1 because the dict value for name + '-ration' is a unicode empty string.
Also, if that error is fixed, and you use crispy forms to display your forms, the render method in "awesome_avatar/widgets.py" also has issues as it expects value to be a django.db.models.fields.files.ImageFieldFile, and instead the type is a dict, so accessing the url member of the dict results in an error.
I have fixes for these issues, if you are interested.
My proposed solution for the first problem in my issue description is to replace line 18 in "awesome_avatar/widgets.py":
ratio = float(data.get(name + '-ratio', 1))
with:
ratio_str = data.get(name + '-ratio', 1)
if (ratio_str != ''):
ratio = float(ratio_str)
else:
ratio = 1.0
My fix for the other issue is to replace line 45:
context['avatar_url'] = value.url if value else '/static/awesome_avatar/default.png'
with:
if type(value) != dict:
context['avatar_url'] = value.url if value else '/static/awesome_avatar/default.png'
else:
context['avatar_url'] = '/static/awesome_avatar/default.png'
Thanks for this -- I was going nuts trying to find the source of the error.