Esempio n. 1
0
    def process_item(self, item, spider):

        # / SALE >>>>

        # extra if check up. shouldn't trigger because we already only scraping since th last sale.
        if Sale.objects.filter(sale_id=item['sale_id'],
                               product__sku=item['sku']).exists():
            print('Sale already exists in database')
            return item
        else:
            sale = Sale()
            sale.sale_id = item['sale_id']  # nooks sale id
            sale.date = item['date']
            sale.product = Product.objects.get(
                Q(sku=item['sku']) | Q(sku_oshawa=item['sku']))
            sale.quantity = int(item['quantity'])  # int bc. its string

            # slice bcause first char is $, float bc. its string
            sale.price_per_unit = float(item['price_per_unit'][1:])

            sale.price = sale.price_per_unit * sale.quantity

            # TODO make real model for channels
            sale.channel = 'Nooks'

            # attribute nooks payout period to the sale
            nooks_payout_schedule = NooksPayoutSchedule.objects.get(
                start_date__lte=sale.date, end_date__gte=sale.date)
            sale.nooks_payout_schedule = nooks_payout_schedule

            sale.save()

            # / TRANSACTION >>>>

            # record change in inventory because of sale
            transaction = Transaction()
            transaction.date = item['date']
            transaction.product = sale.product
            transaction.type = TransactionType.objects.get(
                id=1)  # id 1 is Sale

            # minus because it's sale
            transaction.quantity = -int(item['quantity'])
            transaction.location = Location.objects.get(name=item['location'])

            # because each location has different sku need to check which sku to query
            # all because one sale id can be multiple items.
            # TODO: redesign

            if item['location'] == "Oshawa Centre":  # for now only check oshawa
                transaction.sale = Sale.objects.get(
                    sale_id=item['sale_id'], product__sku_oshawa=item['sku'])
            else:
                transaction.sale = Sale.objects.get(sale_id=item['sale_id'],
                                                    product__sku=item['sku'])

            transaction.save()
            return item