Example #1
0
def main(url, uname, passw, csv_in_file, csv_out_file):
    
    date = datetime.datetime.now()
    sdate = "%02d-%02d-%04d"%(date.month, date.day, date.year)
    try:
        rmsg, rcount, fcount = upsert.main("update", csv_in_file, csv_out_file, url,
            uname, passw)
        if rmsg != "":
            emsg = "Error at update: %s"%rmsg
            print emsg
            return 1
    except sf.SFException, e:
        print e
        return 1
Example #2
0
def main(save_dir, work_dir, src_fname, mail_file):
    global logger
    doesnotwant = "Does Not Want"
    logger = utils.createLogger("fpn_unsubs")
    logger.debug("main started")
    if not os.path.isdir(save_dir):
        r,c = commands.getstatusoutput("mkdir -p "+save_dir)
        if r:
            logger.error(c)
            mail_file_o = open(mail_file, "w")
            mail_file_o.write("ERROR\n")
            mail_file_o.write(c)
            sys.exit(1)
    
    # save_dir has timestamp:email_add
    donefile = os.path.join(save_dir, "donefile.dat")
    grep1 = "grep -n \"%%s\" %s"%donefile
    open(donefile, "a").close() # wtf
    consider = {}
    for p in [l for l in os.listdir(work_dir) if l.find(src_fname) != -1]:
        for line in open(os.path.join(work_dir, p)).read().split("\n"):
            if len(line):
                parts = line.split(delim)
                if len(parts) == 3:
                    addy = parts[1].strip().lower()
                    eds = parts[2]
                    token = parts[0]
                    blk = "%s:%s"%(token, addy)
                    cmd = grep1%blk
#                    logger.debug(cmd)
                    r,c = commands.getstatusoutput(cmd)
                    if r == 0:
                        logger.debug("Already done "+line)
                    else:
                        logger.debug("Will consider "+line)
                        try:
                            tup = consider[addy]
                        except KeyError:
                            tup = ([], [])
                            consider[addy] = tup
                        tup[0].append(token)
                        for ed in eds.split(";"):
                            edx = ed.split(",")[0]
                            if edx not in tup[1]:
                                tup[1].append(edx)
            
    
    if len(consider) == 0: 
        logger.info("Nothing to process")
        return 0


    (sfurl, username, password) = utils.getSFSiteCred(os.path.join(sf_home, "sfrunner.cfg"))


    where = None
    
        
    for key in consider.keys():
        def escapeQ(x): 
            return x.replace("'", "\\'")
        if not where:
            where = "where Email in ('%s'"%escapeQ(key)
        else:
            where = where + ",'%s'"%escapeQ(key)

    where = where + ") and FPN_Subscriptions__c != '' and FPN_Subscriptions__c != '%s' and FPN_Subscriptions__c != 'Not Authorized - see JMC'"%doesnotwant
    
    queryRes = os.path.join(work_dir, "fpnunsubsw.csv")
    if os.path.isfile(queryRes):
        try:
            os.unlink(queryRes)
        except OSError:
            mail_file_o = open(mail_file, "w")
            mail_file_o.write("ERROR")
            msg = "*** cannot delete file "+queryRes
            mail_file_o.write(msg)
            logger.error(msg)
            return 1
            
    p1.main(queryRes, sfurl, username, password, where)
    
    if not os.path.isfile(queryRes):
        msg = "ERROR\n**** query for unsubs did not generate output file\n Query: "+where
        logger.error(msg);
        mail_file_o = open(mail_file, "w")
        mail_file_o.write(msg)
        return 1
    
    # diff what we got from the server and what is currently in sf

    sfmap = {}    
    first = True
    for r in csv.reader(open(queryRes, 'r')):
        if first: first = False
        else:
            key = r[1].strip().lower()
            sfmap[key] = (r[0], r[4].split(";")) 
    

    # create the input file
    csv_file = os.path.join(work_dir, "update.csv")
    logger.debug("LOADER CSV "+csv_file)
    csv_fileo = open(csv_file, 'w')
    csvw = csv.writer(csv_fileo, delimiter=',',
        quotechar='"', quoting=csv.QUOTE_NONNUMERIC)
    csvw.writerow(["ID", "FPN_Subscriptions__c"])
    
    id_to_unsub_map = {}
    
    has_one = False
    for key in consider.keys():
        try:
            insf_tup = sfmap[key]
        except KeyError:
            # if this is the case then it means the contact was deleted from SF
            continue
        to_remove = consider[key][1]

        logger.debug("CONTACT: "+key)
        logger.debug("SF val: "+str(insf_tup))
        logger.debug("toRemove: "+str(to_remove))

        has_one = True
        new_val = ""
        for i in insf_tup[1]:
            i1 = i.split(" ")[0]
            if i1 not in to_remove:
                if new_val == "":
                    new_val = i
                else:
                    new_val = new_val + ";" + i
        if new_val == "": new_val = doesnotwant
        csvw.writerow([insf_tup[0], new_val])
        id_to_unsub_map[insf_tup[0]] = (key, to_remove)
    csv_fileo.close()
    
    if has_one:
        logger.debug("id_to_unsub_map "+str(id_to_unsub_map))
    
        stat_file = os.path.join(work_dir, "fpnunsubs.out")
        logger.debug("STAT FILE "+stat_file)
    
        try:
            rmsg, rcount, fcount = upsert.main("update", csv_file, stat_file, sfurl,
                username, password)
            if rmsg != "":
                emsg = "Error at update: %s"%rmsg
                mail_file_o = open(mail_file, "w")
                mail_file_o.write(emsg)
                return 1
        except sf.SFException, e:
            mail_file_o = open(mail_file, "w")
            mail_file_o.write("ERROR")
            mail_file_o.write(str(e))
            logger.error(str(e))
            return 1
        
        mail_file_o = open(mail_file, "w")
        mail_file_o.write("SF Updated: Records Processed: %d\nFailed: %d\n"%(rcount, fcount))
        
    
        mail_file_o.write("Successful updates\nEmail Address\t\tFPN Unsubscribes\n")    
        stat_file_o = open(stat_file)
    
        first = True
        fail_rec = []
        for r in csv.reader(stat_file_o):
            if first: first = False
            else:
                id = r[0]
                success = r[1]
                create = r[2]
                reason = r[3]
                try:
                    email, unsub = id_to_unsub_map[id]
                    if success.lower() == "true":
                         mail_file_o.write("%s\t\t%s\n"%(email, ";".join(unsub)))
                    else:
                        fail_rec.append((email, ";".join(unsub), reason))
                except KeyError:
                    fail_rec.append(("", "", reason))
        if len(fail_rec):
            mail_file_o.write("Failed updates\nEmail Address\t\tFPN Unsubscribes\tReason\n")    
            for z in fail_rec:
                mail_file_o.write("%s\t\t%s\t%s\n"%(z[0], z[1], z[2]))