Ejemplo n.º 1
0
def detail(request, part_id, company_slug, part_slug):
    p = get_object_or_404(Part, id=part_id)
    
    pricing = DistributorSKU.objects.filter(part=p).aggregate(avg_price=Avg('price'), max_price=Max('price'), min_price=Min('price'))
    distributor_skus = DistributorSKU.objects.filter(part=p).order_by('price')
    current_site = get_current_site(request)
    page_title = "%s %s - %s" % (p.company.name, p.number,
                                    truncatesmart.truncatesmart(title(p.description),
                                    (settings.MAX_PAGE_TITLE_LENGTH
                                     - (len(p.number) +
                                     len(p.company.name)
                                     ))))

    PropertyFormSet = formset_factory(PropertyForm, extra=2, can_delete=True)
    forms = []
    tdict = {}
    for k, v in p.properties.items():
        forms.append({ 'key': k, 'value': v })
    property_formset = PropertyFormSet(initial=forms)

    xrefform = XrefForm(None)
    imageuploadform = ImageUploadForm(None)
    newskuform = DistributorSKUForm(None)
    asinform = ASINForm(None)

    if 'metadata_button' in request.POST:
        update_properties(request, p.id)

        if request.is_ajax():
            return render_to_response('parts/includes/attribute_table.html',
                                          {'part': Part.objects.get(id=p.id),},
                                          context_instance=RequestContext(request))
        else:
            return HttpResponseRedirect(reverse('parts.views.detail',
                                                    args=[part_id, p.company.slug, p.slug]))

    if 'sku_button' in request.POST:
        newskuform = DistributorSKUForm(request.POST)
        if newskuform.is_valid():
            print 'valid'
            sku = newskuform.save(commit=False)
            sku.part = p
            sku.save()
                
            if request.is_ajax():
                return render_to_response('parts/includes/attribute_table.html',
                                          {'part': p,},
                                          context_instance=RequestContext(request))
            else:
                return HttpResponseRedirect(reverse('parts.views.detail',
                                                    args=[part_id, p.company.slug, p.slug]))
        else:
            print 'Not valid'
            print newskuform.errors
    
    if 'description_button' in request.POST:
        description = request.POST.get('description', '')
        p.long_description = description
        p.save()
        if request.is_ajax():
            return render_to_response('parts/includes/long_description.html',
                                        {'part': p,},
                                    context_instance=RequestContext(request))

    if 'asin_button' in request.POST:
        asinform = ASINForm(request.POST)
        if asinform.is_valid():
            asin = asinform.cleaned_data['asin']
            p.asin = asin
            d = Distributor.objects.get(name='Amazon')
            amazon = AmazonAPI(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY, settings.AWS_ASSOCIATE_TAG)
            try:
                product = amazon.lookup(ItemId=asin)
            except:
                pass
            price = product.price_and_currency
            p.upc = product.upc
            p.ean = product.ean
            try:
                attrs = product.get_attributes(['ItemDimensions.Width', 'ItemDimensions.Height', 
                                                'ItemDimensions.Length', 'ItemDimensions.Weight'])
                print attrs
                weight = attrs['ItemDimensions.Weight']
                if not weight:
                    weight = product.get_attribute('Weight')
                p.weight = float(weight)/100.0
                print p.weight
                
            except:
                pass
            
            if not p.image_url or request.POST.get('replace_image', False):
                p.image_url = product.large_image_url
                
            p.save()
            ds = DistributorSKU(distributor=d, part=p, sku=asin, price=price[0],
                                url = product.offer_url)
            ds.save()
            p.save()
            
            if request.is_ajax():
                return render_to_response('parts/includes/attribute_table.html',
                                          {'part': p,},
                                          context_instance=RequestContext(request))
            else:
                return HttpResponseRedirect(reverse('parts.views.detail',
                                                    args=[part_id, p.company.slug, p.slug]))

    	for c in p.cross_references.all():
	    c.categories.add(cat)
            
        if request.is_ajax():
            return render_to_response('parts/includes/category.html',
                                          {'part': p,},
                                          context_instance=RequestContext(request))
        else:
            return HttpResponseRedirect(reverse('parts.views.detail',
                                                args=[part_id, p.company.slug, p.slug]))

    if 'xref_button' in request.POST:
        xrefform = XrefForm(request.POST)
        if xrefform.is_valid:
            status = addxref(request, p.pk)

            if request.is_ajax():
                return render_to_response('parts/includes/xrefs_table.html',
                                          {'part': p,},
                                          context_instance=RequestContext(request))
            else:
                return HttpResponseRedirect(reverse('parts.views.detail',
                                                args=[part_id, p.company.slug, p.slug]))
            
    return render(request, 'parts/detail.html', {'part': p, 
            'property_formset': property_formset, 
            'xref_form' : xrefform,
            'imageuploadform' : imageuploadform,
            'newskuform' : newskuform,
            'page_title': page_title,
            'agg_pricing': pricing,
            'distributor_skus': distributor_skus,
            'asinform': asinform,
           })
Ejemplo n.º 2
0
    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()