Implemented background processing in auto_attach module.

This commit is contained in:
Albert Cervera i Areny 2008-12-23 20:29:08 +01:00
parent 100fc8b3bc
commit 327542e74d
2 changed files with 31 additions and 28 deletions

View File

@ -158,6 +158,7 @@ class nan_document(osv.osv):
return ret
def scan_document_background(self, cr, uid, imageIds):
print "Scan document background"
self.pool.get('ir.cron').create(cr, uid, {
'name': 'Scan document',
'user_id': uid,
@ -167,8 +168,18 @@ class nan_document(osv.osv):
})
cr.commit()
def scan_documents_batch(self, cr, uid, imageIds):
self.scan_document(cr, uid, imageIds)
self.pool.get('res.request').create( cr, uid, {
'act_from': uid,
'act_to': uid,
'name': 'Finished scanning documents',
'body': 'The auto_attach system has finished scanning the documents you requested. You can now go to the Scanned Documents queue to verify and process them.',
})
# If notify=True sends a request/notification to uid
def scan_document(self, cr, uid, imageIds, notify=False):
print "Scan_documentcalled"
# Load templates into 'templates' list
templates = self.pool.get('nan.template').getAllTemplates( cr, uid )
@ -185,6 +196,7 @@ class nan_document(osv.osv):
fp.write( base64.decodestring(document.datas) )
fp.close()
recognizer.recognize( QImage( image ) )
result = recognizer.findMatchingTemplateByOffset( templates )
template = result['template']
doc = result['document']
@ -247,7 +259,7 @@ class nan_document(osv.osv):
fp.close()
recognizer = Recognizer()
recognizer.recognize()
recognizer.recognize( QImage( image ) )
doc = recognizer.extractWithTemplate( image, template )
for box in doc.boxes:
@ -341,29 +353,6 @@ class nan_document(osv.osv):
else:
self.write( cr, uid, [document.id], {'document': False} )
#expression = re.match('(.*)\((.*)\)', function)
#name = expression.group(1)
#parameters = expression.group(2)
#if name not in dir(self):
# print "Function '%s' not found" % (name)
# continue
#parameters = parameters.split(',')
#properties = dict( [(x.name, x.value) for x in document.properties] )
#newParameters = []
#for p in parameters:
# value = p.strip()
# if value.startswith( '#' ):
# print "We'll search '%s' in the properties" % value[1:]
# if value[1:] not in properties:
# continue
# value = properties[ value[1:] ]
# value = "'" + value.replace("'","\\\\'") + "'"
# newParameters.append( value )
#obj = self.pool.get('nan.document')
#reference = eval('obj.%s(cr, uid, %s)' % ( name, ','.join( newParameters ) ) )
def actionAddPartner( self, cr, uid, explain, name ):
if explain:

View File

@ -15,11 +15,13 @@ view_form_start = """<?xml version="1.0"?>
<label align="0.0" string="Note that this operation may take a lot of time, depending on the amount of documents." colspan="4"/>
<label align="0.0" string="The following documents will be scanned:" colspan="4"/>
<field name="documents" nolabel="1" colspan="4"/>
<field name="background"/>
</group>
</form>"""
view_fields_start = {
"documents": {'type':'text', 'string':'Documents', 'readonly':True}
"documents": {'type':'text', 'string':'Documents', 'readonly':True},
"background": {'type':'boolean', 'string':'Execute in the background' }
}
class ScanDocumentQueueWizard(wizard.interface):
@ -31,8 +33,10 @@ class ScanDocumentQueueWizard(wizard.interface):
else:
ids = obj.search(cr, uid, [('state','=','pending')])
values = obj.read(cr, uid, ids, ['name'])
ret = { 'documents': '\n'.join([x['name'] for x in values]) }
return ret
return {
'documents': '\n'.join([x['name'] for x in values]) ,
'background': True
}
def _scan(self, cr, uid, data, context):
pool = pooler.get_pool(cr.dbname)
@ -41,7 +45,17 @@ class ScanDocumentQueueWizard(wizard.interface):
ids = data['ids']
else:
ids = obj.search(cr, uid, [('state','=','pending')])
obj.scan_document(cr, uid, ids)
if data['form']['background']:
pool.get('nan.document').write(cr, uid, ids, {'state': 'scanning'})
pool.get('ir.cron').create(cr, uid, {
'name': 'Scan documents in batch',
'user_id': uid,
'model': 'nan.document',
'function': 'scan_documents_batch',
'args': repr([ ids ])
})
else:
obj.scan_document(cr, uid, ids)
return {}
states = {