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

int & files in a form #81

Open
FrancoisConstant opened this issue Aug 15, 2013 · 5 comments
Open

int & files in a form #81

FrancoisConstant opened this issue Aug 15, 2013 · 5 comments

Comments

@FrancoisConstant
Copy link

The integer on a field don't seem to work when a file is uploaded. I was using django-webtest but the crash occurs in webtest and it seems to be the source of that small bug:

copied from here: django-webtest/django-webtest#18

I used to have a form like this with the test passing:
form = response.form
form['structure-total_number_of_employees'] = 50
...
response = form.submit()

Then when I've added a file to that and updated my code:
form = response.form
form['logo'] = ...
form['structure-total_number_of_employees'] = 50
...
response = form.submit()

It crashed with the following error:
virtual_env/dev_env/lib/python2.7/site-packages/webtest/app.py", line 382, in encode_multipart
body = b'\r\n'.join(lines)
TypeError: sequence item 12: expected string, int found

To get it to work, I've replaced 50 by "50":
form = response.form
form['logo'] = ...
form['structure-total_number_of_employees'] = "50"
...
response = form.submit()

I reckon that the encode multipart should be updated to handle integers.

Thanks !

@gawel
Copy link
Member

gawel commented Aug 15, 2013

Form values are always string. It should not work with an integer. Same with other python types (eg: datetime, float, etc.).

@FrancoisConstant
Copy link
Author

Good point - it could be improved with some checking and clear exception for incorrect type (anything but string). Especially since it's working if no file is uploaded while it shouldn't.

@kmike
Copy link
Contributor

kmike commented Aug 17, 2013

API-wise, what's wrong with passing integers to Webtest?

Not supporting datetimes can prevent errors because there are many ways to convert datetime to string, and it is better to be explicit here. Same with floats (e.g. decimal point vs comma). But I don't see how not supporting integer values could prevent errors. If user sets 50 as a form field value, she wants 50 in the HTML for sure, this is not ambiguous. I don't think anybody sane would expect e.g. inserting thousand separators for integers in such cases.

So +1 for auto-converting integers to strings, because this use case is quite common.

@gawel
Copy link
Member

gawel commented Aug 19, 2013

I'm +0. Because if you expect that form.field = 3 work you can expect that form.field == 3 work too. But this will require much more magic.

@gawel
Copy link
Member

gawel commented Apr 16, 2014

Anyone want to provide a pull request ? If not I'll close this issue.

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

3 participants