def update_offers_status(network, offers): """ :param network: Network model object :param offers: list of dictionaries with importing offer data :return: """ from app.models import Offer, OfferEvent from app import db local_offer_ids = set([offer.remote_id for offer in network.get_active_offers()]) remote_offer_ids = set([offer['id'] for offer in offers]) diff = list(local_offer_ids - remote_offer_ids) if diff: # # logger.debug('Paused offers %s' % str(diff)) (Offer.query .filter(Offer.remote_id.in_(diff), Offer.network == network) .update({Offer.status: Offer.STATUS_STOPPED}, synchronize_session=False)) """create offer stopped event""" for offer_id in diff: db_offer = Offer.query.filter_by(network_id=network.id, remote_id=offer_id).first() if db_offer: offer_event = OfferEvent() offer_event.name = OfferEvent.TYPE_STOPPED offer_event.offer = db_offer db.session.add(offer_event)
def update_or_create_offer(network, offer): """ :param network: Network object model :param offer: dict of importing offer data :return: """ from app.models import Offer, OfferEvent from app import db db_offer = Offer.query.filter_by(network_id=network.id, remote_id=offer['id']).first() if db_offer: """update""" db_offer.name = offer['name'][:128] db_offer.preview_url = offer['preview_url'][:512] db_offer.payout = float(offer['payout']) db_offer.status = Offer.STATUS_ACTIVE return db_offer, True else: """insert""" db_offer = Offer() db_offer.remote_id = offer['id'] db_offer.name = offer['name'][:128] db_offer.preview_url = offer['preview_url'][:512] db_offer.payout = offer['payout'] db_offer.status = Offer.STATUS_ACTIVE db_offer.network = network db.session.add(db_offer) """create event""" offer_event = OfferEvent() offer_event.name = OfferEvent.TYPE_NEW offer_event.offer = db_offer db.session.add(offer_event) return db_offer, False