Ejemplo n.º 1
0
Archivo: views.py Proyecto: manqu/mklab
def comment_new(request, pk):
    item = get_object_or_404(Item, pk=pk)
    author = User.objects.filter()
    # author = User.objects.get()

    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comments = form.save(commit=False)
            comments.item = item
            if request.user.is_authenticated():
                comments.author = request.user
            # comments.author = author
            message = form.cleaned_data["message"]
            item.comments.create(author=request.user, message=message)
            comments.save()

            """if request.is_ajax():
                return render(request, 'shop/item_info.html', {'comment': comment,})
            else:"""
            return redirect("shop:item_info", pk)
    else:
        form = CommentForm()

    return render(request, "shop/comment_form.html", {"form": form, "author": request.user})
Ejemplo n.º 2
0
Archivo: views.py Proyecto: manqu/mklab
def comment_edit(request, item_pk, pk):
    item = get_object_or_404(Item, pk=item_pk)
    comment = get_object_or_404(Comment, pk=pk)
    if request.method == "POST":
        form = CommentForm(request.POST, instance=comment)
        if form.is_valid():
            comment.item = item
            comment.save()
            return redirect("shop:item_info", comment.item.pk)
    else:
        form = CommentForm(instance=comment)

    return render(request, "shop/comment_form.html", {"form": form})
Ejemplo n.º 3
0
def product_view(request, pk):
    product = Product.objects.get(pk=pk)
    comments = Comment.objects.filter(product=product)

    if request.method == 'POST' and request.user.is_authenticated:
        form = CommentForm(request.POST)
        if form.is_valid():
            new_comment = Comment(user=request.user,
                                  product=product,
                                  comment=form.cleaned_data['comment'])
            new_comment.save()
        else:
            form = CommentForm()

    product_rating = None
    if request.user.is_authenticated():
        product_rating = product.rating_set.filter(product=product,
                                                   user=request.user)
        if product_rating.count() == 0:
            product_rating = None
        else:
            product_rating = product_rating.first()

    ratings = product.rating_set
    positive_rating_percentage = None
    all_ratings = ratings.all().count()

    if all_ratings > 0:
        positive_ratings = ratings.filter(rating=True).count()

        if positive_ratings == 0:
            positive_rating_percentage = 0
        else:
            positive_rating_percentage = (positive_ratings / all_ratings) * 100

    return render(
        request, 'shop/detail.html', {
            'product': product,
            'product_rating': product_rating,
            'positive_rating_percentage': positive_rating_percentage,
            'comments': comments
        })