コード例 #1
0
ファイル: offer.py プロジェクト: phrac/partflux
    def populate_db(self):
        distributor = Distributor.objects.get(affiliate_identifier=self.distributor)
        """
        Find the manufacturer of the part or create it if it doesn't exist
        """
        manufacturer = None
        try:
            clookup = CompanyAltName.objects.get(name=self.manufacturer)
            manufacturer = clookup.company
        except:
            pass

        if not manufacturer:
            manufacturer, created = Company.objects.get_or_create(name=self.manufacturer)

        """ 
        Get or create the actual manufacturer part
        """
        try:
            part = Part.objects.get(company=manufacturer, number=self.mpn)
            if part.redirect_part:
                try:
                    part = part.redirect_part
                except:
                    print "REDIRECT doesnt exist"
                    part = part
                    part.redirect_part = None
                    p.save()
            if not part.long_description:
                part.long_description = self.long_description
                part.save()
            if not part.image_url:
                part.image_url = self.image_url
                part.save()
            if not part.upc and self.upc:
                part.upc = self.upc
                part.save()

        except ObjectDoesNotExist:
            part = Part(
                number=self.mpn,
                company=manufacturer,
                description=self.description,
                long_description=self.long_description,
            )
            part.save()

        """
        See if there is already a SKU for this distributor/part combo
        """
        try:
            distributor_sku = DistributorSKU.objects.get(distributor=distributor, sku=self.sku)
        except ObjectDoesNotExist:
            distributor_sku = DistributorSKU(sku=self.sku, distributor=distributor, part=part)

        distributor_sku.price = self.price
        distributor_sku.affiliate_url = self.buylink
        distributor_sku.impression_url = self.impression_url

        try:
            distributor_sku.save()
        except:
            connection._rollback()

        """ Clear some memory """
        db.reset_queries()