Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AvatarWidget Does Not Perform Well When File Not Selected #5

Open
markworden opened this issue Jan 22, 2014 · 2 comments
Open

AvatarWidget Does Not Perform Well When File Not Selected #5

markworden opened this issue Jan 22, 2014 · 2 comments

Comments

@markworden
Copy link

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
Copy link
Author

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'

@domesticatedviking
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants