Exemplo n.º 1
0
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
Exemplo n.º 2
0
def on_save_obj_mapper(sender, instance, **kwargs):

    # prevent possible recursion
    if sender == ObjMapper:
        return

    #The convention here is to always create an object if it cannot be found,
    #rather than only creating it if it has just been created in Satchmo.

    res = True

    #get root mapping since no mapping is specified
    mapping = ModelMapper.get_for_model(sender)

    for oerp_model in mapping.keys():
        # There is no mapper object yet.
        mapper = ObjMapper.objects.get_or_create(
            content_type = ContentType.objects.get_for_model(sender),
            object_id = instance.id,
            oerp_model = oerp_model,
            parent = None)[0]

        #check whether live sync is active
        if settings.OPENERP_SETTINGS['MODE'] == 'live':
            mapper.sync_now = True

        #sync object
        res = res and _save_for_mapper(mapper)

    log.debug('Sync of \'%s\' finished with result: %s' % (instance, res))
Exemplo n.º 3
0
def _save_for_mapper(mapper, mapping_table=None):

    #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

    try:
        if mapper.sync_now:
            #The object is synced now

            oerp_object = Oerp(mapper.oerp_model, mapper.oerp_id)

            if oerp_object.exists:
                #object has to be updated
                data_dict = ModelMapper.parse_data(
                    mapper.object,
                    mapping_table,
                    mapper.oerp_model,
                    'update')[0]
                oerp_object.update(data_dict)
            else:
                #create object if it doesn't exist
                data_dict = ModelMapper.parse_data(
                    mapper.object,
                    mapping_table,
                    mapper.oerp_model,
                    'create')[0]
                oerp_object.create(data_dict)

                #update mapper
                mapper.oerp_id = oerp_object.id

            #consider sync successful if we get to this point
            mapper.save_state('clean')

        else:
            #This object is synced later. Only change mapper status...
            mapper.save_state('dirty')

    except MappingError as errmsg:
        log.error('Sync failed -- %s' % errmsg)
        mapper.save_state('dirty')
        res = False

    except OerpSyncFailed as errmsg:
        log.error('Sync failed -- %s' % errmsg)
        mapper.save_state('dirty')
        res = False

    else:
        #the sync was successful
        #run this recursively if child mappings are found
        children = ModelMapper.get_children(mapping_table)
        for child_model, child_mapping in children.items():
            #create child mapper
            log.debug('Mapping child model (%s)' % child_model)
            child_mapper = ObjMapper.objects.get_or_create(
                parent = mapper,
                content_type = mapper.content_type,
                object_id = mapper.object_id,
                oerp_model = child_model,)[0]
            child_mapper.sync_now = mapper.sync_now

            res = res and _save_for_mapper(child_mapper, child_mapping)

    return res