示例#1
0
def load_cart(request):
    if request.method == 'GET':
        cartItems = []
        basket = request.basket
        index = 0
        ret = request.POST
        basketlines = get_list_or_empty(Line, basket_id=basket)
        if len(basketlines) > 0:
            for basketline in basketlines:
                currentproduct = get_object_or_404(Product,
                                                   id=basketline.product_id)
                parentproduct = get_object_or_404(Product,
                                                  id=currentproduct.parent_id)
                image = get_list_or_empty(ProductImages,
                                          product_id=parentproduct.id)
                stockrecord = get_object_or_404(
                    StockRecords, product_id=basketline.product_id)
                price_excl_tax = convert_to_currency(basketline.price_excl_tax)
                cartInfo = cartInfoJson(basket, basketline, currentproduct,
                                        parentproduct, stockrecord,
                                        basketline.quantity, image)
                cartItems.append(cartInfo)

        return HttpResponse(json.dumps(
            sorted(cartItems, key=lambda k: k['shop'])),
                            content_type='application/json')
示例#2
0
def get_sort_order(filteredobjects, sortfilter):
    if sortfilter == 'date-asc':
        return filteredobjects.order_by('-date_created')
    elif sortfilter == 'date-dsc':
        return filteredobjects.order_by('date_created')
    elif sortfilter == 'price-asc':
        return sorted(filteredobjects,
                      key=lambda i: i.min_child_price_excl_tax)
    elif sortfilter == 'price-dsc':
        return sorted(filteredobjects,
                      key=lambda i: i.min_child_price_excl_tax,
                      reverse=True)
    elif sortfilter == 'pop-asc':
        return sorted(filteredobjects,
                      key=lambda i: sum([
                          j.stats.score if has_stats(j) else 0
                          for j in get_list_or_empty(Product, parent=i.id)
                      ]))
    elif sortfilter == 'pop-dsc':
        return sorted(filteredobjects,
                      key=lambda i: sum([
                          j.stats.score if has_stats(j) else 0
                          for j in get_list_or_empty(Product, parent=i.id)
                      ]),
                      reverse=True)
    else:
        return filteredobjects.order_by('?')
示例#3
0
def add_item_to_cart(request, shop_slug, item_slug):

    if request.method == 'POST':
        shop = get_object_or_404(Shop, slug__iexact=shop_slug)
        item = get_object_or_404(Product,
                                 slug__iexact=item_slug,
                                 shop_id=shop.id,
                                 parent__isnull=True)
        #this is getting all the variants and need to get the that info from the page
        variants = get_list_or_empty(Product, parent=item.id)
        qty = int(request.POST['qtyFilter'])
        msg = ''
        if request.POST['colorFilter'] == '':
            return HttpResponseBadRequest(json.dumps(
                {'errors': 'Please select a color!'}),
                                          content_type='application/json')
        if request.POST['sizeFilter'] == '':
            return HttpResponseBadRequest(json.dumps(
                {'errors': 'Please select a size!'}),
                                          content_type='application/json')
        # varItem = Product.objects.filter(attribute_values__value_option_id=2)
        currentproduct = get_filtered_variant(item.id, request.POST)
        image = get_list_or_empty(ProductImages, product_id=item.id)
        return addBasket(request, currentproduct.id, qty)
    else:
        return redirect('designer_shop.views.itemdetail', shop_slug, item_slug)
示例#4
0
def addBasket(request, product_id, qty):
    # ToDo figure out this tax stuff
    tax = 1
    stockrecord = get_object_or_404(StockRecords, product_id=product_id)
    if (qty > stockrecord.num_in_stock):
        errorMsg = "Quantity is greater than {0}.".format(
            stockrecord.num_in_stock)
        return HttpResponseBadRequest(json.dumps({'errors': errorMsg}),
                                      content_type='application/json')
    if (qty < 1):
        return HttpResponseBadRequest(json.dumps(
            {'errors': "Quantity cannot be less than 1."}),
                                      content_type='application/json')

    currentproduct = get_object_or_404(Product, id=product_id)
    parentproduct = get_object_or_404(Product, id=currentproduct.parent_id)
    price_excl_tax = convert_to_currency(stockrecord.price_excl_tax * qty)
    price_incl_tax = convert_to_currency(
        (stockrecord.price_excl_tax * qty) * tax)
    image = get_list_or_empty(ProductImages, product_id=parentproduct.id)

    basket = request.basket
    line_quantity = basket.line_quantity(product=currentproduct,
                                         stockrecord=stockrecord)
    line_ref = basket._create_line_reference(product=currentproduct,
                                             stockrecord=stockrecord,
                                             options=None)
    if line_quantity == 0:
        # item not in the basket line
        basket.add_product(currentproduct, qty)
        # Send signal for basket addition
        add_signal.send(sender=None,
                        product=currentproduct,
                        user=request.user,
                        request=request)
        basketline = get_list_or_empty(Line,
                                       line_reference=line_ref,
                                       basket_id=basket.id)[0]

    else:
        # item already in the basket_line but add to the qty
        basketline = Line.objects.get(basket=basket,
                                      product=currentproduct,
                                      line_reference=line_ref)
        basketline.quantity = qty
        basketline.save(update_fields=["quantity"])

    cartInfo = cartInfoJson(basket, basketline, currentproduct, parentproduct,
                            stockrecord, qty, image)

    return HttpResponse(json.dumps(cartInfo, use_decimal=True),
                        content_type='application/json')
示例#5
0
def itemdetail(request, shop_slug, item_slug=None):
    shop = get_object_or_404(Shop, slug__iexact=shop_slug)

    if not (shop.user.is_approved):
        if (request.user.is_active):
            if (not request.user.slug == shop.user.slug
                    and not request.user.is_staff):
                return HttpResponseRedirect(reverse('under_construction'))
        else:
            return HttpResponseRedirect(reverse('under_construction'))

    item = get_object_or_404(Product,
                             slug__iexact=item_slug,
                             shop_id=shop.id,
                             parent__isnull=True)
    variants = get_list_or_empty(Product, parent=item.id)
    images = get_list_or_empty(ProductImage, product_id=item.id)
    colorlist = []
    for variant in variants:
        colorattribute = get_or_none(Attributes,
                                     product_id=variant.id,
                                     attribute_id=5)
        colorlist.append(colorattribute.value_as_text)
    colorset = set(colorlist)

    colorsizequantity = get_variants(item)

    return render(
        request,
        'designer_shop/itemdetail.html',
        {
            'shop': shop,
            'item': item,
            'variants': variants,
            'validcolors': colorset,
            'colorsizequantity': colorsizequantity,
            'images': images,
            # What what in the butt (Tom Bowman) 6-22-14
        })
示例#6
0
def _get_shipping_address_pk_that_is_shop_shipping_address(request):
    addresses = UserAddress._default_manager.filter(user=request.user)
    if request.user.is_staff:
        partners = get_list_or_empty(Partner, users__pk=request.user.pk)
    else:
        partners = get_list_or_404(Partner, users__pk=request.user.pk)

    if partners and partners is not None:
        partner_address = partners[0].primary_address
        if partner_address is not None:
            for address in addresses:
                if partner_address.title == address.title \
                        and partner_address.first_name == address.first_name \
                        and partner_address.last_name == address.last_name \
                        and partner_address.line1 == address.line1 \
                        and partner_address.line2 == address.line2 \
                        and partner_address.line3 == address.line3 \
                        and partner_address.line4 == address.line4 \
                        and partner_address.state == address.state \
                        and partner_address.postcode == address.postcode \
                        and partner_address.country == address.country \
                        and partner_address.search_text == address.search_text:
                    return address.pk
    return None
示例#7
0
def get_variants(item, group=None):
    variants = get_list_or_empty(Product, parent=item.id)
    sizeType = get_sizetype(variants)

    if group is None:
        colorsizequantitydict = []
    else:
        colorsizequantitydict = collections.defaultdict(list)

    for variant in variants:
        color = ""
        sizeSet = ""
        sizeX = ""
        sizeY = ""
        sizeNum = ""
        divider = ""
        oneSize = ""
        quantity = get_or_none(StockRecords,
                               product_id=variant.id).net_stock_level
        price = str(
            get_or_none(StockRecords, product_id=variant.id).price_excl_tax)
        currency = get_or_none(StockRecords,
                               product_id=variant.id).price_currency

        if get_or_none(Attributes, product_id=variant.id,
                       attribute_id=5) != None:
            primary_color = get_or_none(Attributes,
                                        product_id=variant.id,
                                        attribute_id=5).value_as_text
            secondary_color = get_or_none(Attributes,
                                          product_id=variant.id,
                                          attribute_id=7)
            if secondary_color:
                color = str(primary_color).capitalize() + "/" + str(
                    secondary_color.value_as_text).capitalize()
            else:
                color = str(primary_color).capitalize()

        if get_or_none(Attributes, product_id=variant.id,
                       attribute_id=1) != None:
            sizeSetNum = get_or_none(Attributes,
                                     product_id=variant.id,
                                     attribute_id=1).value_option_id
            sizeSet = get_or_none(Attributes,
                                  product_id=variant.id,
                                  attribute_id=1).value_as_text

        if get_or_none(Attributes, product_id=variant.id,
                       attribute_id=2) != None:
            sizeX = get_or_none(Attributes,
                                product_id=variant.id,
                                attribute_id=2).value_as_text

        if get_or_none(Attributes, product_id=variant.id,
                       attribute_id=3) != None:
            sizeY = get_or_none(Attributes,
                                product_id=variant.id,
                                attribute_id=3).value_as_text

        if get_or_none(Attributes, product_id=variant.id,
                       attribute_id=4) != None:
            sizeNum = get_or_none(Attributes,
                                  product_id=variant.id,
                                  attribute_id=4).value_as_text

        if get_or_none(Attributes, product_id=variant.id,
                       attribute_id=6) != None:
            oneSize = "One Size"

        if sizeX != "" and sizeY != "":
            divider = " x "
        variantsize = str(sizeSet) + str(sizeX) + divider + str(sizeY) + str(
            sizeNum) + str(oneSize)
        caseFunc = str.capitalize if sizeType != SIZE_SET else str.upper

        if group is None:
            if sizeType == SIZE_SET:
                quantitysize = {
                    'color': color,
                    'size': caseFunc(variantsize),
                    'quantity': quantity,
                    'price': price,
                    'currency': currency,
                    'sizeorder': sizeSetNum
                }
            else:
                quantitysize = {
                    'color': color,
                    'size': caseFunc(variantsize),
                    'quantity': quantity,
                    'price': price,
                    'currency': currency
                }
            colorsizequantitydict.append(quantitysize)
        else:
            if sizeType == SIZE_SET:
                groupdict = {
                    'color': color,
                    'size': caseFunc(variantsize),
                    'quantity': quantity,
                    'price': price,
                    'currency': currency,
                    'sizeorder': sizeSetNum
                }
            else:
                groupdict = {
                    'color': color,
                    'size': caseFunc(variantsize),
                    'quantity': quantity,
                    'price': price,
                    'currency': currency
                }
            mysort = groupdict[group]
            groupdict.pop(group)
            quantitysize = groupdict
            colorsizequantitydict[mysort].append(quantitysize)

            if str(group) == 'color':
                if sizeType == SIZE_SET:
                    colorsizequantitydict[mysort] = sorted(
                        colorsizequantitydict[mysort],
                        key=itemgetter('sizeorder'))
                elif sizeType == SIZE_NUM:
                    colorsizequantitydict[mysort] = sorted(
                        colorsizequantitydict[mysort],
                        key=lambda x: float(x.get('size')))
                elif sizeType == SIZE_DIM:
                    colorsizequantitydict[mysort] = sorted(
                        colorsizequantitydict[mysort],
                        key=lambda x: (float(x.get('size').split('x')[0]),
                                       float(x.get('size').split('x')[1])))
                else:
                    colorsizequantitydict[mysort] = sorted(
                        colorsizequantitydict[mysort], key=itemgetter('size'))
            elif group == 'size':
                colorsizequantitydict[mysort] = sorted(
                    colorsizequantitydict[mysort], key=itemgetter('color'))

    if sizeType == SIZE_NUM and group == 'size':
        colorsizequantitydict = collections.OrderedDict(
            sorted(colorsizequantitydict.items(), key=lambda x: float(x[0])))
    if sizeType == SIZE_DIM and group == 'size':
        colorsizequantitydict = collections.OrderedDict(
            sorted(colorsizequantitydict.items(),
                   key=lambda x:
                   (float(x[0].split('x')[0]), float(x[0].split('x')[1]))))

    addsizetype = {
        'sizetype': sizeType,
        'variants': colorsizequantitydict,
        'minprice': get_min_price(item)
    }
    return json.dumps(addsizetype)
示例#8
0
def get_total_product_count(user):
    if user.is_staff:
        return Product.objects.count()
    else:
        return len(
            get_list_or_empty(Product, shop__user=user, parent__isnull=True))