def register_promotion(conn, cursor, promotion): promotion_id = promotion['id'] cursor.execute('select * from promotions where id = %s', [promotion_id]) if not cursor.fetchone(): offer = promotion['offer'] category = offer['category'] category_id = register_named_entity(conn, cursor, category, 'categories', dict(label=category['label'])) advertiser_id = register_named_entity(conn, cursor, offer['advertiser'], 'advertisers') data = { 'id': promotion_id, 'marketplace_status': promotion['status'], 'name': promotion['name'].encode('utf-8'), 'headline': promotion['offer']['headline'].encode('utf-8'), 'start_date': promotion['start_date'], 'end_date': promotion['end_date'], 'category_id': category_id, 'advertiser_id': advertiser_id } dinsert(cursor, 'promotions', data) sites = promotion.get('publisher', {}).get('sites', []) for site in sites: market_id = register_named_entity(conn, cursor, site['market'], 'markets') dinsert(cursor, 'promotion_market', dict(promotion_id=promotion_id, market_id=market_id)) conn.commit()
def update_redemption_data(conn, cursor, g_client, pid): logger.info("======================================") logger.info("UPDATING REDEMPTION DATA FOR %s" % pid) for status in [ 'CREATED', 'PURCHASED', 'REDEEMED', 'REFUND_HOLD', 'REFUNDED', 'CANCELLED' ]: try: redemption_codes = g_client.GetRedemptionCodesWithStatus( pid, status) size = len(redemption_codes.get('offer', {}).get('codes', [])) logger.info("GetRedemptionCodesWithStatus found -> %s / %s / %s" % (pid, status, size)) data = { 'promotion_id': pid, 'size': size, 'status': status, 'last': True, 'last_update': datetime.datetime.now() } cursor.execute( 'update redemption_codes set last=false where promotion_id = %s and status=%s', [pid, status]) dinsert(cursor, 'redemption_codes', data) except Exception, e: logging.exception("UpdatingRedemptionData Exception -> %s " % str(e)) continue
def register_promotion_history(conn, cursor, promotion, g_status): pid = promotion['id'] data = { 'promotion_id':pid, 'status' : g_status, 'last' : True, 'last_update' : datetime.datetime.now() } cursor.execute('update redemption_codes set last=false where promotion_id = %s', [pid]) dinsert(cursor, 'promotion_status_history', data) conn.commit()
def register_promotion_history(conn, cursor, promotion, g_status): pid = promotion['id'] data = { 'promotion_id': pid, 'status': g_status, 'last': True, 'last_update': datetime.datetime.now() } cursor.execute( 'update redemption_codes set last=false where promotion_id = %s', [pid]) dinsert(cursor, 'promotion_status_history', data) conn.commit()
def register_promotion(conn, cursor, promotion): promotion_id = promotion['id'] cursor.execute('select * from promotions where id = %s', [promotion_id]) if not cursor.fetchone(): offer = promotion['offer'] category = offer['category'] category_id = register_named_entity(conn, cursor, category, 'categories', dict(label=category['label'])) advertiser_id = register_named_entity(conn, cursor, offer['advertiser'], 'advertisers') data = { 'id' : promotion_id, 'marketplace_status': promotion['status'], 'name' : promotion['name'].encode('utf-8'), 'headline' : promotion['offer']['headline'].encode('utf-8'), 'start_date' : promotion['start_date'], 'end_date' : promotion['end_date'], 'category_id' : category_id, 'advertiser_id' : advertiser_id } dinsert(cursor, 'promotions', data) sites = promotion.get('publisher', {}).get('sites', []) for site in sites: market_id = register_named_entity(conn, cursor, site['market'], 'markets') dinsert(cursor, 'promotion_market', dict(promotion_id=promotion_id, market_id=market_id)) conn.commit()
def update_redemption_data(conn, cursor, g_client, pid): logger.info("======================================") logger.info("UPDATING REDEMPTION DATA FOR %s" % pid) for status in ['CREATED', 'PURCHASED', 'REDEEMED', 'REFUND_HOLD', 'REFUNDED', 'CANCELLED']: try: redemption_codes = g_client.GetRedemptionCodesWithStatus(pid, status) size = len(redemption_codes.get('offer', {}).get('codes', [])) logger.info("GetRedemptionCodesWithStatus found -> %s / %s / %s" % (pid, status, size)) data = { 'promotion_id': pid, 'size' : size, 'status' : status, 'last' : True, 'last_update' : datetime.datetime.now() } cursor.execute('update redemption_codes set last=false where promotion_id = %s and status=%s', [pid, status]) dinsert(cursor, 'redemption_codes', data) except Exception, e: logging.exception("UpdatingRedemptionData Exception -> %s " % str(e)) continue