Exemplo n.º 1
0
def migrate_surrogate(session, surrogate, crm_path, path_sep="/"):
    logging.debug('reading files for %s:%s from %s' % (surrogate.get("_type"),surrogate.get("label"), crm_path) )
    file_count = 0
    for info in Control._walk_path_(crm_path, path_sep):
        surrogate_path = info.get("surrogate_path")
        source_path = info.get("source_path")
        fileinfo = info.get("file_info")
    
        try:
            db_file = model.File(**fileinfo)
            db_file.pending = False
            session.add(db_file)
            from genoa.core.model import JsonMixin
            db_object = JsonMixin.from_surrogate(session, surrogate)
            
            if surrogate.get("_type") == "Deal":
                db_rel = model.F2D(deal=db_object, surrogate_path=surrogate_path) 
                db_file.deals.append(db_rel)
            elif surrogate.get("_type") == "Entity":
                db_rel = model.F2E(entity=db_object, surrogate_path=surrogate_path) 
                db_file.entities.append(db_rel)                
            elif surrogate.get("_type") == "Person":
                db_rel = model.F2P(person=db_object, surrogate_path=surrogate_path) 
                db_file.contacts.append(db_rel) 
            else:
                db_file.pending = True
                logging.error("Unsupported file relationship to %s " % surrogate.get("_type"))
            
            session.flush()
            file_count += 1
        except Exception, e:
            logging.exception("Error importing file db_file.name: %s" % e)
        finally: