compute field and uploading file

51 views
Skip to first unread message

aetag...@gmail.com

unread,
Mar 24, 2016, 5:08:38 PM3/24/16
to web2py-users
I've been struggling with trying to figure out how this can be achieved ...not sure if it can be.


In db1.py I have a table defined..example:

db.define_table('form1',
Field('field1', 'string'),
Field('field2', 'string'),
Field('field3', 'integer'),
Field('form1_file', 'upload', compute=form1compute))

Then within the same models file, I have the form1compute function with the parameter row being passed to it.
form1compute generates a file with the data from the form1 and uploads it.

This works fine...

The problem is that I have this form generating dynamically, so a user may have a session where 2 instances of the form fields are generated.

What is supposed to happen is these form values should be passed into the compute function, and the file that is uploaded will contain all values from that session.

Because I have the upload field included in this table definition, it is uploading a separate file for each instance of the form even though it is all from one session. 

I think I need to make a separate table which references this form1 and has an upload field, but I am unsure of what to do after... Maybe make an array that gets filled with the row values in the session, and then call on a separate function after the array has been filled with all values from the single session and then have this function insert the data to the file and then somehow call on the table with the upload field???? Not sure if this way even works .. any advice will be very helpful please and thank you!

aetag...@gmail.com

unread,
Mar 24, 2016, 5:40:44 PM3/24/16
to web2py-users
Or maybe would I have to construct a class within the models file so that it is called in form1compute each time, and the values from row are passed to it and saved in an array within that? I'm not that advanced with python but I think I need the class in order to actually continue to append all the form values to it because I don't think a function alone can save that each time it is called??

aetag...@gmail.com

unread,
Mar 25, 2016, 11:01:20 AM3/25/16
to web2py-users
Instead of removing the upload/compute field and making a separate table for that.. Is this something a virtual field can be used for?

I'm still trying to read more on virtual fields and gain an understanding but essentially the compute field will call onto a class which is being passed the rows, and initializing an array so that each instance of the form fields are appended for that session ..I would have to make some kind of check for when the array is filled for the session and then I'd be able to do my file insert portion... Then and only then will there be an upload. This makes me feel like there definitely needs to be a separate table for the uploads so then I question the virtual field idea....


If any of this makes sense of what I'm trying to accomplish, any advice would help!

aetag...@gmail.com

unread,
Mar 25, 2016, 2:59:12 PM3/25/16
to web2py-users
Given the table :
db.define_table('form1',
    Field('field1, 'string'),

    Field('field2', 'string'),
    Field('field3', 'integer'),
    Field('form1_file', 'upload', compute=fpost))

If a user is starting a form session and enter the value 2, they submit values for:
field1_0, field2_0, field3_0
field1_1, field2_1, field3_1

and the function fpost within models looks like:

def fpost(row):
    print row.values()

This will print the entered values like this:
['value1', 'value2', 'value3']
['value4', 'value5', 'value6']


These are seperate..so I need to create a dynamic array which is filled based on the session so that the row values results in: ['value1', 'value2', 'value3', 'value4', 'value5', 'value6']

Therefore I try making a class, but I'll either get an attribute error or something of the sort and cannot access anything using keyword row within it.. Here is what the models looks like with this implementation:


db1.py

class fpost(object):


    form_list=[]
    def __init__(self):
        fpost.form_list.append(self)


def disp_arr():
    for i in fpost.form_list:
        print i
       
disp_arr()

db.define_table('form1',
    Field('field1, 'string'),

    Field('field2', 'string'),
    Field('field3', 'integer'),
    Field('form1_file', 'upload', compute=fpost()))

Leonel Câmara

unread,
Mar 25, 2016, 3:49:54 PM3/25/16
to web...@googlegroups.com
Can you tell me exactly what you're trying to do? I see how you're trying to do it and it seems complicated, so I'd like to know what you're trying to accomplish first.

Dave S

unread,
Mar 25, 2016, 9:45:16 PM3/25/16
to web2py-users
On Friday, March 25, 2016 at 12:49:54 PM UTC-7, Leonel Câmara wrote:
Can you tell me exactly what you're trying to do? I see how you're trying to do it and it seems complicated, so I'd like to know what you're trying to accomplish first.

It looks like he's doing 2 things:   adding multiple rows to the table during one submit, and creating a file that logs what those changes are.

(Does this file get mailed to someone to be reviewed?)

If he has kept the id[s] of the rows submitted, he might be able to make his report just by retrieving that/those row[s].  This might suggest an onupdate callback.

/dps

Reply all
Reply to author
Forward
0 new messages