django-awesome-avatar icon indicating copy to clipboard operation
django-awesome-avatar copied to clipboard

AvatarWidget Does Not Perform Well When File Not Selected

Open markworden opened this issue 11 years ago • 2 comments

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.

markworden avatar Jan 22 '14 23:01 markworden

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'

markworden avatar Jan 23 '14 00:01 markworden

Thanks for this -- I was going nuts trying to find the source of the error.

domesticatedviking avatar Feb 09 '14 03:02 domesticatedviking