~unifield-team/unifield-wm/sync-UF-1.0-2

« back to all changes in this revision

Viewing changes to sync_client/update.py

  • Committer: jf
  • Date: 2014-10-29 11:13:17 UTC
  • mfrom: (502.4.2 sync)
  • Revision ID: jfb@tempo-consulting.fr-20141029111317-qi7vthycalbmzs28
UF-2491 [IMP] Set not run updates
lp:~unifield-team/unifield-wm/sync-odoo-UF-2491

Show diffs side-by-side

added added

removed removed

Lines of Context:
122
122
        'force_recreation' : fields.boolean('Force record recreation', readonly=True),
123
123
        'handle_priority': fields.boolean('Handle Priority'),
124
124
    }
125
 
    
 
125
 
126
126
    _defaults = {
127
127
        'sent' : False,
128
128
        'is_deleted' : False,
141
141
 
142
142
        def create_normal_update(self, rule, context):
143
143
            domain = eval(rule.domain or '[]')
144
 
            export_fields = eval(rule.included_fields or '[]') 
145
 
            if 'id' not in export_fields: 
 
144
            export_fields = eval(rule.included_fields or '[]')
 
145
            if 'id' not in export_fields:
146
146
                export_fields.append('id')
147
147
 
148
148
            ids_to_compute = self.need_to_push(cr, uid,
213
213
    def create_package(self, cr, uid, session_id=None, packet_size=None, context=None):
214
214
        domain = session_id and [('session_id', '=', session_id), ('sent', '=', False)] or [('sent', '=', False)]
215
215
        ids = self.search(cr, uid, domain, limit=packet_size,  order='id asc', context=context)
216
 
        
 
216
 
217
217
        if not ids:
218
218
            return False
219
 
            
 
219
 
220
220
        update_master = self.browse(cr, uid, ids[0], context=context)
221
 
        data = {  
 
221
        data = {
222
222
            'session_id' : update_master.session_id,
223
223
            'model' : update_master.model,
224
224
            'rule_id' : update_master.rule_id.server_id,
270
270
    _sync_field = 'sync_field'
271
271
 
272
272
    _columns = {
273
 
        'source': fields.char('Source Instance', size=128, readonly=True), 
274
 
        'owner': fields.char('Owner Instance', size=128, readonly=True), 
 
273
        'source': fields.char('Source Instance', size=128, readonly=True),
 
274
        'owner': fields.char('Owner Instance', size=128, readonly=True),
275
275
        'model' : fields.char('Model', size=64, readonly=True, select=True),
276
276
        'sdref' : fields.char('SD ref', size=128, readonly=True, required=True),
277
277
        'is_deleted' : fields.boolean('Is deleted?', readonly=True, select=True),
293
293
    }
294
294
 
295
295
    line_error_re = re.compile(r"^Line\s+(\d+)\s*:\s*(.+)", re.S)
296
 
    
 
296
 
297
297
    _logger = logging.getLogger('sync.client')
298
298
 
299
299
    @translate_column('model', 'ir_model', 'model', 'character varying(64)')
450
450
 
451
451
            #3 check for missing field : report missing fields
452
452
            bad_fields = self._check_fields(cr, uid, obj._name, import_fields, context=context)
453
 
            if bad_fields: 
 
453
            if bad_fields:
454
454
                message += "Missing or unauthorized fields found : %s\n" % ", ".join(bad_fields)
455
455
                bad_fields = [import_fields.index(x) for x in bad_fields]
456
456
 
457
457
            # Prepare updates
458
458
            # TODO: skip updates not preparable
459
459
            for update in updates:
460
 
                       
 
460
 
461
461
                row = eval(update.values)
462
462
 
463
463
                #4 check for fallback value : report missing fallback_value
464
464
                self._check_and_replace_missing_id(cr, uid, import_fields, row, fallback, message, context=context)
465
465
 
466
 
                if bad_fields : 
 
466
                if bad_fields :
467
467
                    row = [row[i] for i in range(len(import_fields)) if i not in bad_fields]
468
468
 
469
469
                values.append(row)
547
547
 
548
548
        def group_unlink_update_execution(obj, sdref_update_ids):
549
549
            obj_ids = obj.find_sd_ref(cr, uid, sdref_update_ids.keys(), context=context)
 
550
            done_ids = []
550
551
            for id, update_id in zip(
551
552
                        obj_ids.values(),
552
553
                        sdref_update_ids.values()
570
571
 
571
572
#                    raise
572
573
                else:
 
574
                    done_ids.append(update_id)
573
575
                    self.write(cr, uid, [update_id], {
574
576
                        'editable' : False,
575
577
                        'run' : True,
576
578
                        'log' : '',
577
579
                    }, context=context)
578
580
 
 
581
                sdrefs = [elem['sdref'] for elem in self.read(cr, uid, done_ids, ['sdref'], context=context)]
 
582
                toSetRun_ids = self.search(cr, uid, [('sdref', 'in', sdrefs), ('is_deleted', '=', False)], context=context)
 
583
                if toSetRun_ids:
 
584
                    self.write(cr, uid, toSetRun_ids, {
 
585
                        'editable' : False,
 
586
                        'run' : True,
 
587
                        'log' : 'Manually set to run by the system. Due to a delete',
 
588
                    }, context=context)
 
589
 
579
590
            return
580
591
 
581
592
        error_message = ""
604
615
                'run' : True,
605
616
                'log' : "This update has been ignored because the record is marked as deleted or does not exists.",
606
617
            }, context=context)
 
618
 
 
619
            sdrefs = [elem['sdref'] for elem in self.read(cr, uid, deleted_update_ids, ['sdref'], context=context)]
 
620
            toSetRun_ids = self.search(cr, uid, [('sdref', 'in', sdrefs), ('is_deleted', '=', False), ('run', '=', False)], context=context)
 
621
            if toSetRun_ids:
 
622
                self.write(cr, uid, toSetRun_ids, {
 
623
                    'editable' : False,
 
624
                    'run' : True,
 
625
                    'log' : '',
 
626
                }, context=context)
 
627
 
 
628
 
607
629
            updates = filter(lambda update: update.id not in deleted_update_ids or
608
630
                                            (not do_deletion and force_recreation),
609
631
                             updates)
664
686
            else:
665
687
                error_message += group_import_update_execution(obj, updates)
666
688
                imported += len(updates)
667
 
        
 
689
 
668
690
        return (error_message.strip(), imported, deleted)
669
691
 
670
692
    def _check_fields(self, cr, uid, model, fields, context=None):
679
701
            if '.id' in field:
680
702
                bad_field.append(field)
681
703
                continue
682
 
            
 
704
 
683
705
            part = field.split('/')
684
706
            if len(part) > 2 or (len(part) == 2 and part[1] != 'id') or not fields_ref.get(part[0]):
685
707
                bad_field.append(field)
686
 
        
 
708
 
687
709
        return bad_field
688
 
         
 
710
 
689
711
    def _remove_bad_fields_values(self, fields, values, bad_fields):
690
712
        for bad_field in bad_fields:
691
713
            i = fields.index(bad_field)
692
714
            fields.pop(i)
693
715
            values.pop(i)
694
 
        
 
716
 
695
717
        return (fields, values)
696
 
    
 
718
 
697
719
    def _conflict(self, cr, uid, sdref, next_version, context=None):
698
720
        ir_data = self.pool.get('ir.model.data')
699
721
        data_id = ir_data.find_sd_ref(cr, uid, sdref, context=context)
705
727
                     or (data_rec.last_modification                           # if last_modification exists, try the next
706
728
                         and data_rec.sync_date < data_rec.last_modification) # modification after synchro => conflict
707
729
                     or next_version < data_rec.version))                     # next version is lower than current version
708
 
    
 
730
 
709
731
    def _check_and_replace_missing_id(self, cr, uid, fields, values, fallback, message, context=None):
710
732
        ir_model_data_obj = self.pool.get('ir.model.data')
711
733