#model
db.define_table('table1',
    Field('name','string',requires=IS_NOT_EMPTY()),
    Field('filename','upload',label=T('Input file'),
      requires=IS_NULL_OR(IS_UPLOAD_FILENAME())),
    )

#controller
def input1():
  form = SQLFORM(db.table1)
  if form.accepts(request.vars,session):
    response.flash=form.vars
  return dict(form=form)

# submit without file generates this ticket:
Traceback (most recent call last):
  File "C:\web2py\gluon\restricted.py", line 178, in restricted
    exec ccode in environment
  File "C:/web2py/applications/test1/controllers/default.py", line 92,
in <module>
  File "C:\web2py\gluon\globals.py", line 101, in <lambda>
    self._caller = lambda f: f()
  File "C:/web2py/applications/test1/controllers/default.py", line 45,
in input1    if form.accepts(request.vars,session):
  File "C:\web2py\gluon\sqlhtml.py", line 816, in accepts
    (source_file, original_filename) = (f.file, f.filename)
AttributeError: 'NoneType' object has no attribute 'file'


# submit with file generates this ticket:
Traceback (most recent call last):
  File "C:\web2py\gluon\restricted.py", line 178, in restricted
    exec ccode in environment
  File "C:/web2py/applications/test1/controllers/default.py", line 92,
in <module>
  File "C:\web2py\gluon\globals.py", line 101, in <lambda>
    self._caller = lambda f: f()
  File "C:/web2py/applications/test1/controllers/default.py", line 45,
in input1    if form.accepts(request.vars,session):
  File "C:\web2py\gluon\sqlhtml.py", line 743, in accepts
    onvalidation,
  File "C:\web2py\gluon\html.py", line 1245, in accepts
    status = self._traverse(status)
  File "C:\web2py\gluon\html.py", line 451, in _traverse
    newstatus = c._traverse(status) and newstatus
  File "C:\web2py\gluon\html.py", line 451, in _traverse
    newstatus = c._traverse(status) and newstatus
  File "C:\web2py\gluon\html.py", line 451, in _traverse
    newstatus = c._traverse(status) and newstatus
  File "C:\web2py\gluon\html.py", line 451, in _traverse
    newstatus = c._traverse(status) and newstatus
  File "C:\web2py\gluon\html.py", line 458, in _traverse
    newstatus = self._validate()
  File "C:\web2py\gluon\html.py", line 1054, in _validate
    (value, errors) = validator(value)
  File "C:\web2py\gluon\validators.py", line 1668, in __call__
    if not value:
  File "C:\Program Files\Python25\Lib\cgi.py", line 633, in __len__
    return len(self.keys())
  File "C:\Program Files\Python25\Lib\cgi.py", line 609, in keys
    raise TypeError, "not indexable"TypeError: not indexable


I think the problem is that IS_UPLOAD_FILENAME() expect there to be a
file, which runs counter to IS_NULL_OR(), which should allow no file.

The problem I'm trying to solve is this. You have a table that with a
file upload field, like table1 above. You want to let the user enter
the file or, cut and paste some text into a text field. If the text
field is used, the controller will save the text to a file. The table
will be as table1 above, so no Field to store text. Therefore,
SQLFORM.factory will be used to build one of the forms with an ersatz
Field:
  text_form = SQLFORM.factory(Field
('text_input','text',requires=IS_NOT_EMPTY(),db.table1)

The other form is:
  file_form = SQLFORM(db.table1)

The problem is that when submitting the text_form, which includes
db.table1, the filename field will not be filled out. The IS_NULL_OR()
clause was supposed to take care of that, but obviously, this is not
the path.

I want to include db.table1 in the text_form because I want to be able
to fill out the name field and have it inserted into the db
automatically by the text_form.accepts() function.

So, should I use the "trick" of making the filename field not readable
or writable during the text_form processing, but readable and writable
during the file_form processing? Or would I be better off just going
with SQLFORM.factory and no db behind it and then manually inserting
fields in the real db table?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py-users" 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/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to