Thank you for your help Massimo.

My model is defined as follows:

## Repository

db.define_table('repository',
        Field('name', type='string'),
        Field('directory', default='applications/init/uploads'),
        format='%(name)s')

## Workbench - workbenches stored in the repository

db.define_table('workbench',
        Field('repository', 'reference repository'),
        Field('name', compute=lambda r: 
db.workbench.workbench.retrieve(r.workbench)[0]),
        Field('workbench', type='upload', required=True, 
custom_store=store_file, custom_retrieve=retrieve_file ),

        Field('sequence', 'integer', required=True),
        Field('created_date', 'datetime', default=request.now, readable=False, 
writable=False),
        Field('modified_date', 'datetime', update=request.now, readable=False, 
writable=False),
        format='%(name)s')


I have a Repository table which is joined to the Workbench table (1:M - a 
repository can have many workbenches )

In my controller I have a smartgrid for Repository which has Workbenches as 
a linked table. After a Repository has been created, the user can create 
Workbenches linked to the Repository. When creating the Workbench I want 
the workbench file to be uploaded to the directory in the Repository 
location field.

I hope that makes it clear what I'm trying to achieve.

Thank you.

On Monday, December 1, 2014 11:46:57 AM UTC+13, Massimo Di Pierro wrote:
>
> You cannot do this uploadfolder= lambda r:...
> But this is a chicken-egg problem. You want r to be the row but the row is 
> not created until after the file is uploaded therefore you cannot specify 
> the upload folder as function of something that happens after.
>
> Please explain to us in english of the folder should depend on the upload 
> and perhaps we can suggest a way to do it.
>
>
> On Saturday, 29 November 2014 14:08:20 UTC-6, James Burke wrote:
>>
>> How do you create a dynamic uploadfolder for an upload Field?
>>
>> I tried using:
>> uploadfolder= lambda r: db.repository(r.repository_id).directory
>>
>> but i get the error message, I guess it doesn't have the same 
>> functionality as compute: 
>> cannot concatenate 'str' and 'function' objects
>>
>> I'm also using custom_store/retreive to maintain the files original 
>> filename
>>
>> def store_file(file, filename=None, path=None):
>>     path = path # "applications/init/uploads"
>>     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
>>
>>
>> If I can set the uploadfolder dynamically then it's passed to store_file 
>> as path.
>>
>> I also tried:
>> path = db.repository(db.workbench(db.workbench.name==filename).repository
>> ).directory
>>
>> But it can't find a record, I guess because store_file is run before the 
>> record is created in the db.
>>
>> Any suggestions?
>>
>> Thank you!
>>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to