~openerp-dev/openobject-server/trunk-temporal-db

« back to all changes in this revision

Viewing changes to openerp/addons/base/ir/ir_model.py

[MERGE] ir_model_data unlinking does not use _unlink anymore, clears caches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
580
580
    def __init__(self, pool, cr):
581
581
        osv.osv.__init__(self, pool, cr)
582
582
        self.doinit = True
583
 
        self.unlink_mark = {}
584
583
 
585
584
        # also stored in pool to avoid being discarded along with this osv instance
586
585
        if getattr(pool, 'model_data_reference_ids', None) is None:
629
628
            id = False
630
629
        return id
631
630
 
 
631
    def unlink(self, cr, uid, ids, context=None):
 
632
        """ Regular unlink method, but make sure to clear the caches. """
 
633
        ref_ids = self.browse(cr, uid, ids, context=context)
 
634
        for ref_id in ref_ids:
 
635
            self._get_id.clear_cache(cr.dbname, uid, ref_id.module, ref_id.name)
 
636
            self.get_object_reference.clear_cache(cr.dbname, uid, ref_id.module, ref_id.name)
 
637
        return super(ir_model_data,self).unlink(cr, uid, ids, context=context)
 
638
 
632
639
    def _update(self,cr, uid, model, module, values, xml_id=False, store=True, noupdate=False, mode='init', res_id=False, context=None):
633
640
        model_obj = self.pool.get(model)
634
641
        if not context:
719
726
                                table.replace('.', '_'))] = (table, inherit_id)
720
727
        return res_id
721
728
 
722
 
    def _unlink(self, cr, uid, model, res_ids):
723
 
        for res_id in res_ids:
724
 
            self.unlink_mark[(model, res_id)] = False
725
 
            cr.execute('delete from ir_model_data where res_id=%s and model=%s', (res_id, model))
726
 
        return True
727
 
 
728
729
    def ir_set(self, cr, uid, key, key2, name, models, value, replace=True, isobject=False, meta=None, xml_id=False):
729
730
        if type(models[0])==type([]) or type(models[0])==type(()):
730
731
            model,res_id = models[0]
752
753
        return True
753
754
 
754
755
    def _process_end(self, cr, uid, modules):
 
756
        """ Clear records removed from updated module data.
 
757
 
 
758
        This method is called at the end of the module loading process.
 
759
        It is meant to removed records that are no longer present in the
 
760
        updated data. Such records are recognised as the one with an xml id
 
761
        and a module in ir_model_data and noupdate set to false, but not
 
762
        present in self.loads.
 
763
 
 
764
        """
755
765
        if not modules:
756
766
            return True
757
767
        modules = list(modules)
758
768
        module_in = ",".join(["%s"] * len(modules))
759
769
        cr.execute('select id,name,model,res_id,module from ir_model_data where module IN (' + module_in + ') and noupdate=%s', modules + [False])
760
770
        wkf_todo = []
 
771
        to_unlink = []
761
772
        for (id, name, model, res_id,module) in cr.fetchall():
762
773
            if (module,name) not in self.loads:
763
 
                self.unlink_mark[(model,res_id)] = id
 
774
                to_unlink.append((model,res_id))
764
775
                if model=='workflow.activity':
765
776
                    cr.execute('select res_type,res_id from wkf_instance where id IN (select inst_id from wkf_workitem where act_id=%s)', (res_id,))
766
777
                    wkf_todo.extend(cr.fetchall())
773
784
 
774
785
        cr.commit()
775
786
        if not config.get('import_partial'):
776
 
            for (model, res_id) in self.unlink_mark.keys():
 
787
            for (model, res_id) in to_unlink:
777
788
                if self.pool.get(model):
778
789
                    self.__logger.info('Deleting %s@%s', res_id, model)
779
790
                    try:
780
791
                        self.pool.get(model).unlink(cr, uid, [res_id])
781
 
                        if id:
782
 
                            ids = self.search(cr, uid, [('res_id','=',res_id),
783
 
                                                        ('model','=',model)])
784
 
                            self.__logger.debug('=> Deleting %s: %s',
785
 
                                                self._name, ids)
786
 
                            if len(ids) > 1 and \
787
 
                               self.__logger.isEnabledFor(logging.WARNING):
788
 
                                self.__logger.warn(
789
 
                                    'Got %d %s for (%s, %d): %s',
790
 
                                    len(ids), self._name, model, res_id,
791
 
                                    map(itemgetter('module','name'),
792
 
                                        self.read(cr, uid, ids,
793
 
                                                  ['name', 'module'])))
794
 
                            self.unlink(cr, uid, ids)
795
 
                            cr.execute(
796
 
                                'DELETE FROM ir_values WHERE value=%s',
797
 
                                ('%s,%s'%(model, res_id),))
798
792
                        cr.commit()
799
793
                    except Exception:
800
794
                        cr.rollback()
801
795
                        self.__logger.warn(
802
 
                            'Could not delete id: %d of model %s\nThere '
803
 
                            'should be some relation that points to this '
804
 
                            'resource\nYou should manually fix this and '
805
 
                            'restart with --update=module', res_id, model)
 
796
                            'Could not delete obsolete record with id: %d of model %s\n'
 
797
                            'There should be some relation that points to this resource\n'
 
798
                            'You should manually fix this and restart with --update=module',
 
799
                            res_id, model)
806
800
        return True
807
801
ir_model_data()
808
802