예제 #1
0
 def _create_offers_with_new_sku(self, offer_list, sku):
     for offer in offer_list:
         r = requests.post(
             url=Config.DEVICE_PRICES,
             data={"processSegmentationCode": offer.segmentation,
                   "deviceStockCode": sku.stock_code,
                   "offerNSICode": offer.offer_code,
                   "tariffPlanCode": offer.tariff_plan_code,
                   "contractConditionCode": offer.contract_condition_code}
         )
         self.request_counter += 1
         if r.json()["devicesPrices"]:
             new_offer = Offer(
                 segmentation=offer.segmentation,
                 market=offer.market,
                 contract_condition_code=offer.contract_condition_code,
                 sku=sku,
                 tariff_plan_code=offer.tariff_plan_code,
                 offer_code=offer.offer_code
             )
             new_offer.priority = offer.priority
             new_offer.set_prices(offer.price)
             new_offer.abo_price = offer.abo_price
             db.session.add(new_offer)
     db.session.commit()
예제 #2
0
    def save_or_update_device(self, device_info, offer_info):
        # =================== Product ======================= #
        product = Product.query.filter_by(
            manufacturer=device_info["brand"],
            model_name=device_info["modelName"]
        ).first()
        if not product:
            product = Product(
                manufacturer=device_info["brand"],
                model_name=device_info["modelName"],
                product_type=device_info["productType"]
            )

        # =================== SKU ======================= #
        sku = SKU.query.filter_by(stock_code=device_info["sku"]).first()
        if not sku:
            sku = SKU(base_product=product, stock_code=device_info["sku"])
        sku.availability = device_info["available"]
        # =================== Photo ======================= #
        for photo in device_info["imagesOnDetails"]:
            device_photo = Photo.query.filter_by(
                url=photo["normalImage"]).first()
            if device_photo is not None:
                device_photo.default = photo["defaultImage"]
            else:
                device_photo = Photo(sku=sku, url=photo["normalImage"],
                                     default=photo["defaultImage"])
            db.session.add(device_photo)

        # =================== Offer ======================= #
        offer = Offer.query.filter_by(
            segmentation=self.segment,
            offer_code=offer_info["offerNSICode"],
            sku=sku,
            tariff_plan_code=offer_info["tariffPlanCode"],
            contract_condition_code=offer_info["contractConditionCode"]
        ).first()
        if not offer:
            offer = Offer(
                segmentation=self.segment,
                market=self.segment.split(".")[0],
                sku=sku,
                offer_code=offer_info["offerNSICode"],
                tariff_plan_code=offer_info["tariffPlanCode"],
                contract_condition_code=offer_info["contractConditionCode"]
            )
        offer.set_prices(
            float(device_info["prices"]["grossPrice"].replace(",", "."))
        )
        offer.abo_price = float(offer_info["monthlyFeeGross"].replace(",", "."))
        offer.priority = device_info["devicePriority"]

        # =================== Saving ======================= #
        db.session.add(product)
        db.session.add(sku)
        offer.ping(self.scrapping_time)
        db.session.commit()