Exemple #1
0
def comment(request, group_id, item_id):
    item = get_object_or_404(Item, pk=item_id)
    group = get_object_or_404(WishlistGroup, pk=group_id)
    group_item = GroupItem.objects.get(item=item, group=group)

    print "adding a coment to %s" % item
    print "posted values %s" % request.POST
    form = CommentForm(request.POST)
    if not form.is_valid():
        print "comment form is invalid"
        return render(request, "wishlist_app/item/item.html", {
            "item": item,
            "comments": item.comments.order_by('created'),
            "comment_form": form
        })
    print "saving comment"
    c = form.save(commit=False)
    c.commenter = request.user
    # TODO refactor this to be more DRY
    if c.hide_from_wisher and c.commenter == item.wisher:
        form.add_error("hide_from_wisher", ValidationError("Can't hide comments from yourself"))
        return render(request, "wishlist_app/item/item.html", {
            "item": item,
            "comments": group_item.comments.order_by('created'),
            "comment_form": form
        })
    c.save()
    ic = ItemComment(group_item=group_item, comment=c)
    ic.save()
    print "saved comment %s" % c
    return redirect("group_item_read", group.id,  item.id)