Hi All,
I'm having a very very weird problem with some upload processing I'm performing in web2py.
Firstly for context here is the code I am using:
Controller:
def validate_test():
form = SQLFORM.factory(
Field('csvfile', 'upload'), table_name="dataset_upload")
if form.validate():
logger.info("form validating")
candidate_file = request.vars.csvfile.file.read()
logger.info("{}".format(candidate_file))
elif form.process().accepted:
response.flash = 'form accepted'
elif form.errors:
response.flash = 'form has errors'
return dict(form=form)
And the View:
{{extend 'layout.html'}}
<div style="background: linear-gradient(240deg, lightgrey, whitesmoke)" class="jumbotron">
<br><br>
{{=form}}
</div>
This is essentially as simple as it can be. The user should upload a csv file, and I wish to run a suite of validators on that csv file before returning to the user whether the validation was passed or where it failed and on what.
Now this is the first step, just checking that the form csv is readable by the server. And here is where I hit the problem:
Sometimes I get out (as expected) the contents of the csv file from logger.info("{}".format(candidate_file))
Most of the time
I just get out a blank string, even with the same input file!
I feel like I'm going mad, doing the same thing twice and getting different results. It smells to me like caching or race conditions but I just have no idea what really is going on or why this might be happening.
Do you have any ideas?
Thanks!
P.S. also more generally is this a good way to perform validation on a csv file without any database IO? I want users to be able to check whether their file fits the canonical format prior to actually attempting to commit it to the database?
It would be nice to be able to use the Web2Py validators to do this validation, but I don't really want to start creating temporary staging tables to perform this validation prior to upload, especially as I have a multi-table structure which the data will be going in to, but it comes in as a flat csv.