On Monday, March 21, 2016 at 9:53:36 PM UTC-4, drew Roberts wrote:
> I have something like this in a controller:
>
>
> @auth.requires_login()
> def list_machines():
> form=crud.create(db.machine)
> form1=SQLFORM(db.machine)
> machines=db(db.machine.id>0).select(orderby=db.machine.name)
> return dict(machines=machines,form=form,form1=form1)
>
> The view is something like this:
>
> <h1>All Machines</h1>
>
> [<a href="#nil" onclick="jQuery('#form').slideToggle();">Add</a>]
> <div id="form">
> <h2>New Machine</h2>
> {{=form}}
> </div>
> [<a href="#nil" onclick="jQuery('#form1').slideToggle();">Multi Edit</a>]
> <div id="form1">
> <h2>Multi Edit Machines</h2>
> {{=form1}}
> </div>
>
>
> <br /><br />
> <table class="smarttable">
> <thead>
> <tr>
> <th>Name</th><th>Curr Camp</th><th>Next Camp</th><th>Next
> Date</th><th>Actions</th>
> </tr>
> </thead>
> <tbody>
> {{for machine in machines:}}
> <tr>
> <td>{{=link_machine(machine)}}</td>
> <td>{{=[machine.cur_camp}}</td>
> <td>{{=machine.next_camp}}</td>
> <td>{{=machine.next_date}}</td>
> <td>
> {{=button('edit','edit_machine',machine.id)}}</td>
> </tr>
> {{pass}}
> </tbody>
> </table>
>
> The purpose of form is to allow new machines to be added and I have that
> working.
>
> What I want from form1 is this:
>
> If I put something in the cur_camp field and submit, all rows "selected"
> are updated with this new value.
>
> Selected is either every row or every row remaining when something is
> searched for.
>
> Basically, I want to be able to do mass edits for certain fields of
> selected records. (If the field is left blank, I want to leave that column
> as is on the rows.
>
> Has anyone ever done something like this in web2py? Any hints?
>
OK, I have a dirty and non general solution in case anyone is interested in
this problem:
Model bits:
db.define_table('camp',
Field('name', unique=True, requires=[IS_NOT_EMPTY(), IS_ALPHANUMERIC()]),
format='%(name)s'
)
db.camp._singular = "Camp"
db.camp._plural = "Camps"
db.define_table('machine',
Field('name', unique=True, requires=[IS_NOT_EMPTY(), IS_ALPHANUMERIC()]),
Field('cur_camp', 'reference camp'),
Field('next_camp', 'reference camp'),
format='%(name)s'
)
db.machine.cur_camp.requires = IS_IN_DB(db, db.camp.id,'%(name)s')
db.machine.next_camp.requires = IS_IN_DB(db, db.camp.id,'%(name)s')
db.machine.cur_camp.represent = lambda id, r: db.camp[id].name
db.machine.next_camp.represent = lambda id, r: db.camp[id].name
db.machine._singular = "Machine"
db.machine._plural = "Machines"
Controller bits:
import os
import errno
import urllib2
import ConfigParser
import re
from shutil import copyfile
from glob import glob
def list_camps():
grid = SQLFORM.grid(db.camp, user_signature=False)
return dict(grid=grid)
def list_machines():
grid = SQLFORM.grid(db.machine, user_signature=False)
return dict(grid=grid)
def edit_machinemulti():
#machine_id=request.args(0)
#machine=db.machine[machine_id] or redirect(error_page)
#session.recent_machines = add(session.recent_machines,machine)
#form=crud.update(db.machine,machine,next=url('list_machines'))
grid = SQLFORM.grid(db.machine, details=False, deletable=False,
editable=True)
form1 = SQLFORM.factory(
Field('cur_camp',requires=IS_EMPTY_OR(IS_IN_DB(db,
db.camp.id,'%(name)s'))),
Field('next_camp',requires=IS_EMPTY_OR(IS_IN_DB(db,
db.camp.id,'%(name)s')))).process()
if form1.accepted:
#what we do if accepted
#response.flash = form.vars.cur_camp, form.vars.next_camp
myrowlist = list()
rownum = 1
for row in grid:
#response.flash = row.type()
if rownum==2:
#print
'=================================================================='
#print type(row)
#print row
#print
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
for c in row.elements('tr td',first_only=True):
#c[0]='z'
#print '-----------------'
myrowid = c.flatten()
#print myrowid
#print '-----------------'
myrowlist.append(myrowid)
#[m.start() for m in re.finditer('edit/machine/?', row)]
rownum = rownum+1
#print 'Records to update: ', myrowlist
mynumrecs = len(myrowlist)
if (mynumrecs > 0):
#print 'We will now update the following number of records: ',
mynumrecs
for i in range(0,mynumrecs):
print 'Updating record: ', i, 'Record id: ', myrowlist[i]
myupdaterec = db(db.machine.id ==
myrowlist[i]).select().first()
if (form1.vars.cur_camp<>None):
print "OK, we need to change the current camp for this
record to: ", form1.vars.cur_camp
myupdaterec.update_record(cur_camp=form1.vars.cur_camp)
else:
print "We are not updating the current camp for this
record."
if (form1.vars.next_camp<>None):
print "OK, we need to change the next camp for this
record to: ", form1.vars.next_camp
myupdaterec.update_record(next_camp=form1.vars.next_camp)
else:
print "We are not updating the next camp for this
record."
redirect(URL('default','edit_machinemulti', args=[request.args(0)]))
elif form1.errors:
# what happens if form errors
response.flash = 'invalid values in form1!'
return dict(grid=grid,form1=form1)
View bits:
list_camps.html:
{{extend 'layout.html'}}
<h1>List Camps</h1>
{{=grid}}
list_machines.html:
{{extend 'layout.html'}}
<h1>List Machines</h1>
{{=grid}}
edit_machinemulti.html:
{{extend 'layout.html'}}
<h1>Edit Mutliple Machines</h1>
{{=grid}}
<h2>Changes</h2>
{{=form1}}
Hopefully this will prove useful to someone. I am working on a demo app
that explores this problem and looks for alternate and more general
solutions.
all the best,
drew
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.