Exemplo n.º 1
0
def product_from_post(productslug, formdata):
    product = Product.objects.get_by_site(slug=productslug)
    origproduct = product
    log.debug('found product: %s', product)
    p_types = product.get_subtypes()
    details = []
    zero = Decimal("0.00")

    if 'ConfigurableProduct' in p_types:
        # This happens when productname cannot be updated by javascript.
        cp = product.configurableproduct
        # catching a nasty bug where ConfigurableProducts with no option_groups can't be ordered
        if cp.option_group.count() > 0:
            chosenOptions = optionids_from_post(cp, formdata)
            optproduct = cp.get_product_from_options(chosenOptions)
            if not optproduct:
                log.debug('Could not fully configure a ConfigurableProduct [%s] with [%s]', product, chosenOptions)
                raise Product.DoesNotExist()
            else:
                product = optproduct

    if 'CustomProduct' in p_types:
        try:
            cp = product.customproduct
        except ObjectDoesNotExist:
            # maybe we've already looked up the subtype product above, try the original
            cp = origproduct.customproduct
        for customfield in cp.custom_text_fields.all():
            if customfield.price_change is not None:
                price_change = customfield.price_change
            else:
                price_change = zero
            data = { 'name' : customfield.translated_name(),
                     'value' : formdata.get("custom_%s" % customfield.slug, ''),
                     'sort_order': customfield.sort_order,
                     'price_change': price_change }
            details.append(data)
            data = {}
        chosenOptions = optionids_from_post(cp, formdata)
        manager = OptionManager()
        for choice in chosenOptions:
            result = manager.from_unique_id(choice)
            if result.price_change is not None:
                price_change = result.price_change
            else:
                price_change = zero
            data = { 'name': six.text_type(result.option_group),
                      'value': six.text_type(result.translated_name()),
                      'sort_order': result.sort_order,
                      'price_change': price_change
            }
            details.append(data)
            data = {}

    if 'GiftCertificateProduct' in p_types:
        ix = 0
        for field in ('email', 'message'):
            data = {
                'name' : field,
                'value' : formdata.get("custom_%s" % field, ""),
                'sort_order' : ix,
                'price_change' : zero,
            }
            ix += 1
            details.append(data)
        log.debug("Gift Certificate details: %s", details)
        data = {}

    return product, details