The fourth argument passed to store_file() is db.table.new_filename, which 
is a DAL Field object. Your code is treating it as a string, and 
"table.new_filename" is the string representation of a Field object. If you 
want to include an actual filename string, then that's what you have to 
pass to the function.

Anthony

On Monday, April 8, 2013 9:34:45 PM UTC-4, James Burke wrote:
>
> Hi Anthony,
>
> I've tried the solution you've posted below:
> Field('file', 'upload',
>     custom_store=lambda file, filename, path: store_file(file, filename,path
> , db.table.new_filename),
>     ...)
>
> But when my additional parameter gets sent to store_file it sends 
> 'table.new_filename'
>
> def store_file(file, filename=None, path=None, new_filename=None):
>  path = "applications/init/uploads/%s" % (new_filename)
>  if not os.path.exists(path):
>  os.makedirs(path)
>  pathfilename = os.path.join(path, filename)
>  dest_file = open(pathfilename, 'wb')
>  try:
>  shutil.copyfileobj(file, dest_file)
>  finally:
>  dest_file.close()
>
>
>  return filename
>
> Results in a new folder in uploads called 'table.new_filename'
>
> Cheers 
> -James
>
> On Wednesday, August 8, 2012 11:51:44 PM UTC+12, Anthony wrote:
>>
>> On Wednesday, August 8, 2012 7:09:37 AM UTC-4, Jim Gregory wrote:
>>>
>>> Can custom_store and custom_retrieve take parameters when they are 
>>> called using Field, e.g.:
>>> db.define_table('table',
>>>     Field('file', 'upload', custom_store=store_file(file, 
>>> db.table.new_filename), 
>>> custom_retrieve=retrieve_file(db.table.new_filename))?
>>>     Field('new_filename','string')
>>> )
>>>
>>
>> These attributes have to be callables, so you can't call them yourself. 
>> To pass additional custom arguments to a callable, just turn it into a 
>> lambda that takes the standard arguments and pass the additional arguments 
>> to your function within the lambda:
>>
>> Field('file', 'upload',
>>     custom_store=lambda file, filename, path: store_file(file, filename,path
>> , db.table.new_filename),
>>     ...)
>>
>> When web2py calls the custom_store callable, it will pass three arguments 
>> (file, filename, and path), so your lambda must take three arguments. 
>> Within the lambda, you can then do whatever you want. You can pass any or 
>> all of the standard three arguments to your custom function, and you can 
>> pass additional arguments. In the example above, I passed the three 
>> standard arguments followed by a fourth custom argument, though it doesn't 
>> have to look like that (presumably you will at least want to pass the file 
>> as an argument).
>>
>> custom_retrieve takes two standard arguments, "name" and "path".
>>
>> Anthony
>>
>>

-- 

--- 
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