from gluon import *
import os
client_id=3 #hard coded here for illustration
db.define_table('attachment_model',
Field('attached_file', 'upload', label="Upload new file",
uploadfolder=os.path.join(request.folder, 'uploads', str(client_id), 'attachments'), requires=IS_NOT_EMPTY(), autodelete=True),
Field('filename', type='string', length=150, writable=False),
migrate=True)
def model_based():
db.attachment_model.filename.readable=False
form_attachment=SQLFORM(db.attachment_model,
autodelete=True, labels=None, deletable=True,
fields=['attached_file'], submit_button='Attach file')
if hasattr(request.vars.attached_file, "filename"):
#save original file name
form_attachment.vars.filename=request.vars.attached_file.filename
if form_attachment.process().accepted:
response.flash = 'attachment saved'
elif form_attachment.errors:
response.flash = 'form has errors'
# list existing attachments
rows=db(db.attachment_model.id>0).select()
response.view = 'attachment.html'
return dict(form_attachment=form_attachment,
rows=rows)
{{extend 'layout.html'}}
<h1>
Attached files
</h1>
<table class="table-striped">
<thead>
<tr>
<td>id</td>
<td>filename</td>
</tr>
</thead>
<tbody>
{{for r in rows:}}
<tr>
<td>{{=r.id}}</td>
<td>{{=A(r.filename, _href=URL('download', args=(str(client_id) + '/attachments/' + r['attached_file'])))}}</td>
</tr>
{{pass}}
</tbody>
</table>
<h2>Add new attachment</h2>
<div class="form-inline well">
{{=form_attachment}}
</div>
from gluon import *
import os
class Attachment_module(object):
def __init__(self, db):
self.db = db
def define_tables(self):
db = self.db
client_id=3
if not 'attachment_module' in db.tables:
db.define_table('attachment_module',
Field('attached_file', 'upload', label="Upload new file",
uploadfolder=os.path.join(current.request.folder, 'uploads', str(client_id), 'attachments'),
requires=IS_NOT_EMPTY(), autodelete=True),
Field('filename', type='string', length=150, writable=False),
Field('request_tenant', type='integer', default=client_id,
readable=False, writable=False),
migrate=True)
def module_based():
from mod_attachment import Attachment_module
attachment_module = Attachment_module(db)
attachment_module.define_tables()
db.attachment_module.filename.readable=False
form_attachment=SQLFORM(db.attachment_module,
autodelete=True, labels=None, deletable=True,
fields=['attached_file'], submit_button='Attach file')
if hasattr(request.vars.attached_file, "filename"):
#save original file name
form_attachment.vars.filename=request.vars.attached_file.filename
if form_attachment.process().accepted:
response.flash = 'attachment saved'
elif form_attachment.errors:
response.flash = 'form has errors'
# list existing attachments
rows=db(db.attachment_module.id>0).select()
response.view = 'attachment.html'
return dict(form_attachment=form_attachment,
rows=rows)
URL('download', args=(str(client_id) + '/attachments/' + r['attached_file'])
def download():
import os
file_path = os.path.join(request.folder,'uploads', str(auth.user.client), request.args(0))
return response.stream(open(file_path), attachment=False, chunk_size=4096)
Hi Rudy
Have you tried re-defining the download function to include the path to the
location you have used for storing the uploaded files? For example, in my
case I ended up with the following in my controller:
def download():
import os
file_path = os.path.join(request.folder,'uploads', str(auth.user.client), request.args(0))
return response.stream(open(file_path), attachment=False, chunk_size=4096)
db.define_table('company',
Field('logo', 'upload', label='Company Logo',
uploadfolder=os.path.join(request.folder, 'uploads', auth.user.client if auth.user else ''),
uploadseparate=True, autodelete=True)),
...)