Here it is!
Model :
db.define_table('test1',
Field('f1','string'),
Field('f2','integer'),
Field('review','boolean'))
from utils import md5_hash
class v_f_s(object):
def hmd5(self):
return md5_hash('|'.join(str(self.test1[k]) for k in
db.test1.fields()))
Controller:
def __return_md5_hash(table, record): # Can be move into module...
from utils import md5_hash
if record and table:
serialized = '|'.join(str(db[table][record][k]) for k in
db[table].fields())
record_hash = md5_hash(serialized)
return record_hash
def select2():
db.test1.virtualfields.append(v_f_s())
rows = db(db.test1.id>0).select(db.test1.ALL)
rows.colnames.append('test1.hmd5') # from bruno snipts... Only way I
found to append virtual field to SQLTABLE
list_of_row_md5h = []
for r in rows:
list_of_row_md5h.append((r.id,r.hmd5)) #
table = SQLTABLE(rows, columns=['test1.id
','test1.f1','test1.f2','test1.review','test1.hmd5'])
form = SQLFORM.factory(Field('review_records',
'boolean'),hidden=dict(list_md5h=list_of_row_md5h))
if form.process().accepted:
form.vars.list_md5h = request.vars.list_md5h # as says in the book
in case of hidden field vars has to be move manually
true_check = []
if form.vars.list_md5h: # is there a list of SQLTABLE record(s) hash
for i,j in eval(form.vars.list_md5h):
if j == __return_md5_hash('test1', i): # test1 or tablename
db(db.test1.id == i).update(review='TRUE') #
Processing...
else:
true_check.append(True)
if true_check and all(true_check):
session.flash = 'record(s) have changed before you submit
the form, please complet the review process...'
redirect(URL(request.application,request.controller,'select2'))
else:
response.flash = 'bulk review completed'
else:
session.flash = 'notting to review or error happen computing
records hash, contact administrator'
redirect(URL(request.application,c=request.controller,f='index'))
elif form.errors:
response.flash = 'form has errors'
return dict(table=table, form=form)
I spend long time to figure out that form.vars.list_md5h was returned as a
string... :)
Feel free to comment!
Richard
On Wed, Dec 14, 2011 at 2:15 PM, Richard Vézina <[email protected]
> wrote:
> Ok... Thank you so much for your precious time Anthony!
>
> Answer in the text below...
>
> On Wed, Dec 14, 2011 at 2:07 PM, Anthony <[email protected]> wrote:
>
>> Don't have time for a thorough review, but a few points...
>>
>>
>> sum_of_sum = md5_hash('|'.join(str(i) for i in list_of_row_md5h))
>>>
>>
>> I think you only want to hash individual records and check individual
>> records when they are submitted (probably via an ajax submission). There's
>> no reason to reject an update for one record just because another record
>> has changed (which is what will happen if you create a single hash for the
>> entire set of records).
>>
>>
>
> You maybe right... I already have a implementation that works for that. I
> was pushing further, since user will have to double check if all the
> records I think he had reviewed have been effectively reviewed... Maybe
> just returning (redirect) the form in case a records had changed will be
> enough...
>
>
>
>> form._formkey = sum_of_sum* # does notting here...*
>>>
>>
>> Although the input field is named "_formkey", the form attribute is
>> form.formkey, not form._formkey. Also, the _formkey isn't created until you
>> call .accepts or .process, so you have to change the formkey after
>> form.process.
>>
>> form.process(detect_record_**change=True)
>>>
>>
>> I do not think you will be able to use the built-in detect_record_change
>> functionality with SQLFORM.factory. I was suggesting you manually code your
>> own by hashing the records and comparing the hashes upon submission.
>>
>> Anthony
>>
>
>
> Richard
>