On Fri, Jun 19, 2009 at 8:05 PM, Seth<[email protected]> wrote:
>
> I just found this thread while looking for information on this exact
> subject. I am using TG 2 and have not found good documentation on
> this.
>
> I appreciate frankentux's post, but does someone have a more recent
> example (perhaps with Sprox)? The object I'm getting doesn't seem to
> have a .size attribute.
I think that will be a great tutorial.
It really boils down to learning how to use paste.fileapp it's trivial code.
I'm attaching a dated! class this worked with one of the Beta's I'm
not sure if it works with 2.0.1, also note it uses the really old
use_wsgi_app you shuld be using a WSGIController. Also it won't
compile as I removed references to my project. note it provides both
upload and download but the download isn't efficient, but this was for
a ow traffic site. And yes it uses plain ToscaWidgets.
import pylons
from pylons import config
from tg import expose, validate, redirect, flash
from formencode import validators
from tw.forms.datagrid import DataGrid,Column
from webhelpers.rails.number import number_to_human_size as
_number_to_human_size
from paste.fileapp import FileApp
from repoze.what import authorize
import os,shutil
import logging
log = logging.getLogger(__name__)
def number_to_human_size(obj):
return _number_to_human_size(obj.size)
attachment_table = DataGrid(fields=[
('Name','name'),
('Created On','created'),
('Type','type'),
('Owner Id','owner_id'),
('Description','description'),
('Policy Id','policy_id'),
('File Size',number_to_human_size),
('Local Path','path'),
])
class AttachmentController(BaseController):
""" This controller handles the CRUD of Attachment objects """
require = authorize.not_anonymous()
@expose('projectone.webapp.templates.attachment')
def index(self):
pylons.c.widget = attachment_table
attachments= DBSession.query(Attachment).all()
log.debug(attachments)
return dict(page='All Attachments',objs=attachments)
@expose()
@validate(attachment_form)
def create(self, fileobj,overwrite=False,**kw):
upload_dir = pylons.config.get('attachments.path')
if fileobj is None:
flash('You must select a file to attach!',
status='status_warning')
redirect('/attachment/new?case_id=%s' % kw['case_id'])
local_path = os.path.join(upload_dir,fileobj.filename)
if os.path.exists(local_path):
flash('a file named %s already exists' % fileobj.filename,
status='status_warning')
redirect('/attachment/new?case_id=%s' % kw['case_id'])
local_file = open(local_path,'wb')
shutil.copyfileobj(fileobj.file,local_file)
local_file.close()
case_id = kw['case_id']
kw['name']=fileobj.filename
kw['path']=local_path
kw['size']=os.path.getsize(local_path)
kw['owner']= request.environ.get('repoze.who.identity')['user']
policy = DBSession.query(Policy).filter(Policy.case_id==case_id)[0]
del kw['case_id']
attachment = Attachment(**kw)
attachment.policy = policy
DBSession.add(attachment)
log.info("%s %s.%s" % ('Created Attachment:',
attachment.__tablename__, attachment.id))
log.debug('Params : %s' % kw)
flash("File %s has been uploaded sucessfully" % fileobj.filename)
redirect('/case/view/%s#ag'%case_id)
@expose('projectone.webapp.templates.widget')
def new(self, case_id=None):
log.debug('Method %s got params case_id=%s',request.path,case_id)
if case_id is None:
flash("No case to add attachment", status="status_warning")
redirect("/case")
pylons.c.widget = attachment_form
d = dict(page='New Attachment', value=case_id)
log.debug('Replying with responseDict=%s',d)
return d
@expose('json')
def fetch(self, **kw):
log.debug('Method %s got params %s',request.path,None)
cases = DBSession.query(Attachment).all()
d = dict(results=cases)
log.debug('Replying with responseDict=%s',d)
return d
@expose()
def download(self,attachment_id):
log.debug('Method %s got params %s',request.path,None)
attachment =
DBSession.query(Attachment).filter_by(id=attachment_id).first()
file_path = attachment.path
filename = os.path.basename(file_path)
fapp = FileApp(file_path,
**{"Content-Disposition":"attachment; filename="+filename})
return use_wsgi_app(fapp)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---