Exemplo n.º 1
0
def add_user_order(request):
    new_order_form = UserNewOrderForm(request.POST or None)

    if new_order_form.is_valid():
        order = Order.objects.filter(owner_id=request.user.id,
                                     is_paid=False).first()
        if order is None:
            order = Order.objects.create(owner_id=request.user.id,
                                         is_paid=False)

        product_id = new_order_form.cleaned_data.get('product_id')
        count = new_order_form.cleaned_data.get('count')
        if count < 0:
            count = 1
        product = Product.objects.get_by_id(product_id=product_id)
        order.orderdetail_set.create(product_id=product.id,
                                     price=product.price,
                                     count=count)
        # todo: redirect user to user panel
        # return redirect('/user/orders')

        return redirect(
            f'/products/{product.id}/{product.title.replace(" ", "-")}')

    return redirect('/')
Exemplo n.º 2
0
def add_user_order(request):
    new_order_form = UserNewOrderForm(request.POST or None)

    if new_order_form.is_valid():
        order = Order.objects.filter(owner_id=request.user.id, is_paid=False).first()
        if order is None:
            order = Order.objects.create(owner_id=request.user.id)
        product_id = new_order_form.cleaned_data.get('product_id')
        count = new_order_form.cleaned_data.get('count')
        if count <= 0:
            count = 1
        product = Product.objects.get_product_by_id(productid=product_id)
        order.orderdetail_set.create(product_id=product_id, price=product.price, count=count)

    return redirect('/')
Exemplo n.º 3
0
def product_detail(request, *args, **kwargs):
    selected_product_id = kwargs['productId']

    new_order_form = UserNewOrderForm(
        request.POST or None, initial={'product_id': selected_product_id})

    product: Product = Product.objects.get_by_id(selected_product_id)

    if product is None or not product.active:
        raise Http404('محصول مورد نظر یافت نشد !!!')

    product.visit_count += 1
    product.save()

    related_products = Product.objects.get_queryset().filter(
        categories__product=product).distinct()
    grouped_related_products = my_grouper(3, related_products)

    galleries = ProductGallery.objects.filter(product_id=selected_product_id)
    grouped_galleries = list(my_grouper(3, galleries))

    context = {
        'product': product,
        'galleries': grouped_galleries,
        'related_products': grouped_related_products,
        'new_order_form': new_order_form,
    }

    # tag = Tag.objects.first()
    # print(tag.products.all())

    return render(request, 'products/products_detail.html', context)
Exemplo n.º 4
0
def product_detail(request, *args, **kwargs):
    selected_product_id = kwargs['productid']
    new_order_form = UserNewOrderForm(
        request.POST or None, initial={'product_id': selected_product_id})
    user_favorite_list = Favorite.objects.filter(
        current_user_id__exact=request.user.id,
        favorite_product__exact=selected_product_id).exists()
    # comment part
    user_commented = UserComment.objects.filter(
        active=True, user_id=request.user.id,
        product_id=selected_product_id).__bool__()
    user_comments = UserComment.objects.order_by('-id').filter(
        active=True, product_id=selected_product_id)
    comment_form = CommentForm(request.POST or None)
    if request.user.is_authenticated and not user_commented:
        if comment_form.is_valid():
            UserComment.objects.create(
                user_id=request.user.id,
                product_id=selected_product_id,
                title=comment_form.cleaned_data.get('title'),
                text=comment_form.cleaned_data.get('text'))
    total_comments = UserComment.objects.filter(
        active=True, product_id=selected_product_id).count()
    paginator = Paginator(user_comments, 4)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    # comment part
    # print(user_favorite_list)
    got_product: Product = Product.objects.get_product_by_id(
        selected_product_id)
    got_product.visit_count += 1
    got_product.save()
    # print(got_product.active)
    if got_product is None or not got_product.active:
        raise Http404('محصول مورد نظر یافت نشد.')

    related_product = Product.objects.get_queryset().filter(
        category__product=got_product, active=True).distinct()
    grouped_related_product = list(gallery_grouper(3, related_product))
    # print(grouped_related_product)
    galleries = ProductGallery.objects.filter(product_id=selected_product_id)
    grouped_gallery = list(gallery_grouper(3, galleries))

    context = {
        'object': got_product,
        'gallery': grouped_gallery,
        'related_product': grouped_related_product,
        'new_order_form': new_order_form,
        'favorite_list': user_favorite_list,
        'comment_form': comment_form,
        'user_comments': user_comments,
        'total_comments': total_comments,
        'user_commented': user_commented,
        'paginator': page_obj,
        'paginator_range': paginator.page_range,
        'title': 'جزئیات محصول | فروشگاه آزمایشی '
    }

    return render(request, 'products/product_detail.html', context)
Exemplo n.º 5
0
def product_detail(request, *args, **kwargs):
    product_id = kwargs['productId']  # get considered id
    comment_form = CommentForm(request.POST or None)  # comment form
    product = Product.objects.get_product_by_id(product_id)
    related_products = Product.objects.get_queryset().filter(
        categories__product=product).distinct()
    new_order_form = UserNewOrderForm(request.POST or None,
                                      initial={'product_id':
                                               product_id})  # order form
    grouped_related_products = my_grouper(4, related_products)
    tag = Tag.objects.filter(product=product)
    grouped_related_products = list(zip(grouped_related_products))
    grouped_related_products = [
        i for i in grouped_related_products[0][0] if i != product
    ]
    if product is None:  # if product is not find
        raise Http404('محصول مورد نظر یافت نشد')
    if comment_form.is_valid() and request.method == 'POST':
        if not request.user.is_authenticated:
            messages.warning(request, 'ابتدا باید وارد سایت شوید!')
            return redirect('login')
        full_name = comment_form.cleaned_data.get('full_name')
        email = comment_form.cleaned_data.get('email')
        comment = comment_form.cleaned_data.get('comment')
        Comment.objects.create(owner=request.user,
                               product_id=product_id,
                               full_name=full_name,
                               email=email,
                               comment=comment)
        messages.success(request, 'نظر شما با موفقیت ارسال شد')
        return HttpResponseRedirect(request.path_info)

    comments = Comment.objects.get_queryset().filter(
        product_id=product_id).order_by(
            '-date_added').distinct()  # get comments for current product
    paginator = Paginator(comments, 4)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)

    # get viewer ip adress
    def get_ip(request):
        address = request.META.get('HTTP_X_FORWARDED_FOR')
        if address:
            ip = address.split(',')[-1].strip()
        else:
            ip = request.META.get('REMOTE_ADDR')
        return ip

    # find visited count for each product
    ip = get_ip(request)
    visited_ip_product = Visited_Ip_product.objects.filter(product=product,
                                                           user_ip=ip).all()
    if len(visited_ip_product) == 0:
        Visited_Ip_product.objects.create(product=product, user_ip=ip)
        product.visit_count += 1
        product.save()
    context = {
        'title': 'فروشگاه اینترنتی|مشاهده جزئیات محصول',
        'product': product,
        'comments': comments,
        'comment_form': comment_form,
        'paginator': paginator,
        'page_obj': page_obj,
        'grouped_related_products': grouped_related_products,
        'new_order_form': new_order_form
    }
    return render(request, 'products/product_detail.html', context)