Good catch. I changed "if value" into "if value is not None". Does that fix 
the problem?

On Sunday, 28 July 2013 22:50:14 UTC-5, Joe Barnhart wrote:
>
> Guess what??
>
> I've found this bug again -- this time in the DAL module!  Here is the 
> affected code, it's a part of the Table class:
>
>     def _attempt_upload(self, fields):
>         for field in self:
>             if field.type=='upload' and field.name in fields:
>                 value = fields[field.name]
>                 *if value and not isinstance(value,str):*
>                     if hasattr(value,'file') and hasattr(value,'filename'):
>                         new_name = 
> field.store(value.file,filename=value.filename)
>                     elif hasattr(value,'read') and hasattr(value,'name'):
>                         new_name = field.store(value,filename=value.name)
>                     else:
>                         raise RuntimeError("Unable to handle upload")
>                     fields[field.name] = new_name
>
>
> If this function is passed an instance of cgi.FieldStorage for an upload 
> field, the "if" clause resolves to FALSE because cgi.FieldStorage does not 
> support the convention of "has data = true".  It returns False whether it 
> has data or not.  Therefore, you can't use "form.validate()" with your own 
> "insert" to manually process forms with upload values.
>
> I've been pounding on WingIDE to figure out why I couldn't get my code to 
> work as documented, and I found this.  I'm not exactly sure how to code 
> around it, but I'll figure something out.
>
> -- Joe B
>
>
> On Thursday, May 30, 2013 2:06:20 AM UTC-7, Joe Barnhart wrote:
>>
>> I've had the most awful time trying to get the actual filename of an 
>> uploaded file to appear in my database.  I've gone over and over the 
>> advice, but it has never worked for me.  I was beginning to suspect evil 
>> spirits in my code when I ran across the problem...
>>
>> cgi.FieldStorage does NOT behave like other Python variables in that it 
>> doesn't resolve to True if the object contains data.  It resolves to False 
>> ALL THE TIME, whether it has data or not.
>>
>> I was dutifully following advice and had code that looked like:
>>
>>
>> if request.vars.upload:
>>   form.vars.filename = request.vars.upload.filename
>>
>>
>> But guess what?  The code inside "if" NEVER RUNS.
>>
>> For now I've worked around it by using the method "has_key()" on the 
>> cgi.FieldStorage object:
>>
>>
>> if request.vars.has_key('upload'):
>>   form.vars.filename = request.vars.upload.filename
>>
>>
>>
>> And this works.
>>
>> The "proper" fix is probably to modify cgi.FieldStorage so that is 
>> resolves to True if it has data and False if empty, like all other Python 
>> objects.  But I thought I'd pass along my epiphany in case others are 
>> wondering if their code is inhabited by evil spirits.
>
>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to