Hi Anthony,

Thanks for your continuous help. I tried like below, i had to specify 
uploadfolder in SQLFORM.factory(), otherwise web2py raised error saying 
user must specify uploadfolder. Then I thought i could override it after 
form.accepted, but it didn't. The uploaded file ended up storing under the 
top-level folder (auth.user.id) instead of (company_id). One thing I don't 
want is to ask the user to fill in company details in 1 form, submit it, 
then redirect to another form and upload the logo image. I wonder if you 
actually meant what I did below. Thanks!

form=SQLFORM.factory( 
                Field('company_name', requires=[IS_NOT_EMPTY(), 
IS_NOT_IN_DB(db, 'company.company_name')]),
                Field('logo', 'upload', 
uploadfolder=os.path.join(request.folder, 'uploads', str(auth.user.id)), 
uploadseparate=True, autodelete=True), # 23x23mm, 300K recommended
                table_name='company',
                formstyle='table3cols').process()   
if form.accepted:
        company_id= db.company.insert(company_name=form.vars.company_name)
        db.company.logo.uploadfolder=os.path.join(request.folder, 
'uploads', str(company_id)) # 23x23mm, 300K recommended
        db.company.chop.uploadseparate=True
        db.company.logo.autodelete=True
        company_row = db.company(company_id)
        company_row.update_record(logo=form.vars.logo)
elif form.errors: ...
else: ...

On Friday, October 13, 2017 at 9:59:34 AM UTC+8, Anthony wrote:
>
> Instead of having SQLFORM automatically insert the new record, you can 
> create custom logic to first insert everything but the file, and then after 
> getting the new record ID, use that to set the uploadfolder and update the 
> record with the file.
>
> Anthony
>
> On Thursday, October 12, 2017 at 8:26:16 PM UTC-4, Rudy wrote:
>>
>> Hi Antony,
>>
>> Thanks so much again for sharing your insights. I have been playing with 
>> uploadfolder, your suggestion below and from another thread 
>> (uploadfolder=os.path.join(request.folder, 'uploads', str(auth.user.id) 
>> if auth.user else '') work. However what I really want to do is to separate 
>> the files being uploaded based on a company_id. Say an user comes to my 
>> site, register as admin user for his company based on group assignment, 
>> then he / she will be instructed to register his/her company details 
>> including the logo image (being uploaded). Now I want to set this top-level 
>> upload folder to be company_id, however I will only get the company_id 
>> after i submit the form, I don't want to set the top-level upload folder 
>> based on auth.user.id because this admin user can create more users for 
>> this company for day-to-day tasks, I want to group all the file system 
>> consumption based on company.
>>
>> I think about computing the next_company_id by doing below, but if i have 
>> 2 users trying to do the same thing, i will have a race condition and get 
>> error. I search in this forum to see how people deal with auto increment 
>> field, Mathieu suggested Field('ref_number', compute=lambda r: r['id'] + 
>> 100), but i don't seem to be able to apply it in uploadfolder attribute, 
>> any suggestion will be much appreciated.
>>
>> company_rows=db().select(db.company.ALL)
>>     if company_rows:
>>         last_company_row=company_rows.last()
>>         next_company_id=last_company_row.id + 1
>>     else:
>>         next_company_id=1
>>
>>
>> On Thursday, September 28, 2017 at 12:32:46 AM UTC+8, Anthony wrote:
>>>
>>> On Wednesday, September 27, 2017 at 5:04:27 AM UTC-4, Rudy wrote:
>>>>
>>>> Dave / Anthony,
>>>>
>>>> Thanks for your input and confirmation. I will look into the code to 
>>>> see how i can use the uuid to separate files for multi-tenant purpose.
>>>>
>>>
>>> If you're going to be accessing the uploaded files based on the 
>>> filenames stored in the database, you don't necessarily need to do anything 
>>> with the filesystem to enable multi-tenant functionality. The database 
>>> records will identify the tenants and therefore enable you to identify 
>>> which files belong to which tenants.
>>>
>>> However, if you need to easily be able to identify files associated with 
>>> a given tenant from the filesystem only (i.e., without reference to the 
>>> database records), then you could create separate top-level upload folders 
>>> for each client. In your code, you would do that by dynamically specifying 
>>> the "uploadfolder" argument of the Field() constructor to set a unique 
>>> folder for each client. For example:
>>>
>>> uploadfolder=os.path.join(request.folder, 'uploads', tenant_id)
>>>
>>> You could still leave uploadseparate=True, and then you'll get folders 
>>> like:
>>>
>>> /uploads/1/company.logo/ab/
>>> /uploads/2/company.logo/dy/
>>>
>>> where 1 and 2 are the id's of different tenants.
>>>
>>> Anthony
>>>
>>

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