# -*- coding: utf-8 -*-
# © 2016 Esther Martín - AvanzOSC
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from openerp import SUPERUSER_ID


def assign_contract_stage(cr, registry):
    hr_obj = registry['hr.contract']
    hr_ids = hr_obj.search(cr, SUPERUSER_ID, [])
    stage_obj = registry['hr.contract.stage']
    stage_ids = stage_obj.search(cr, SUPERUSER_ID, [])
    for hr in hr_obj.browse(cr, SUPERUSER_ID, hr_ids):
        if not hr.contract_stage_id:
            try:
                vals = {'contract_stage_id': stage_ids[0]}
                hr_obj.write(cr, SUPERUSER_ID, hr.id, vals)
            except:
                continue

@@ -197,94 +197,8 @@
 y):%0A
-    hr_obj = registry%5B'hr.contract'%5D%0A    hr_ids = hr_obj.search(cr, SUPERUSER_ID, %5B%5D)%0A
     
@@ -296,100 +296,43 @@
 , %5B%5D
-)%0A    for hr in hr_obj.browse(cr, SUPERUSER_ID, hr_ids):%0A        if not hr.contract_
+, order=%22sequence%22)%0A    if 
 stage_id
 :%0A  
@@ -327,16 +327,17 @@
 stage_id
+s
 :%0A      
@@ -342,16 +342,40 @@
     
-    try:
+cr.execute('UPDATE hr_contract '
 %0A   
@@ -391,17 +391,16 @@
     
-vals = %7B'
+   'SET 
 cont
@@ -416,24 +416,19 @@
 e_id
-': stage_ids%5B0%5D%7D
+ = %5C'%25s%5C' '
 %0A   
@@ -444,93 +444,79 @@
     
-hr_obj.write(cr, SUPERUSER_ID, hr.id, vals)%0A            except:%0A                continue
+   'WHERE contract_stage_id IS NULL;' %25%0A                   (stage_ids%5B0%5D))
 %0A

improve hook