コード例 #1
0
ファイル: listeners.py プロジェクト: jeame/satchmo-openerp
def _delete_for_mapper(mapper, mapping_table=None, parent=None):
    ''' Delete the OE object(s) corresponding to the given mapper '''

    #get model corresponding to specified mapper
    obj_model = mapper.content_type.model_class()

    if mapping_table is None:
        #get root mapping if no mapping is specified
        mapping_table = ModelMapper.get_for_model(obj_model)[mapper.oerp_model]

    res = True

    #copy mapper for future reference
    mapper_del = DeletedObjMapper.objects.get_or_create(
        content_type = mapper.content_type,
        oerp_model = mapper.oerp_model,
        oerp_id = mapper.oerp_id,
        parent = parent)[0]

    #find child mappers
    child_mappers = mapper.__class__.objects.filter(parent = mapper)

    for child in child_mappers:
        #recursively delete child objects
        child.sync_now = mapper.sync_now
        res = res and _delete_for_mapper(
            mapper = child,
            mapping_table = mapping_table[child.oerp_model],
            parent = mapper_del)

    #check whether objects corresponding to this oe model can be deleted
    mapper.auto_del = ModelMapper.check_auto_del(mapping_table)


    try:
        if mapper.sync_now:
            #Automatic synchronization is turned on
            oerp_object = Oerp(mapper.oerp_model, mapper.oerp_id)

            #only try to delete if the object exists and is not
            #deleted automatically with its parent object
            if oerp_object.exists and not mapper.auto_del:
                oerp_object.delete()
            #deleting non-existent objects is OK and is not logged
            mapper_del.is_dirty = False
        else:
            #this object will be deleted later
            mapper_del.is_dirty = True
    except Exception as errmsg:
        #the sync failed
        log.error('Sync failed -- %s' % errmsg)
        mapper_del.is_dirty = True
        res = False
    finally:
        #store mapper in deleted mappers and delete original mapper
        if mapper != mapper_del:
            mapper.delete()
        mapper_del.save()

    return res