Example #1
0
def addcomment(request, id):
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comm = form.save(commit=False)
            comm.product = Product.objects.get(id=id)
            form.save()
    return HttpResponseRedirect('/product/%s' % id)
Example #2
0
def newComment(request, idProducto=None):
    if request.user.is_authenticated():
        if idProducto != None:
            if request.method == "POST":
                newComment = NewCommentForm(request.POST)
                if newComment.is_valid():
                    try:
                        data = {
                            'id_product': idProducto,
                            'id_sender': request.user.id,
                            'subject': newComment.cleaned_data['subject'],
                            'text': newComment.cleaned_data['text'],
                            'datetime': datetime.now(),
                        }
                        commentForm = CommentForm(data)
                        if commentForm.is_valid():
                            commentForm.save()
                            user = request.user
                            #                         send_mail('Comentario enviado', 'Usted hizo un nuevo comentario.', settings.EMAIL_HOST_USER , [user.email], fail_silently=False)
                            return HttpResponseRedirect("/products/" +
                                                        str(idProducto))
                        else:
                            return HttpResponse("DATA_ERROR")
                    except Exception as e:
                        print e
                        return HttpResponseRedirect("/products/" +
                                                    str(idProducto))
                else:
                    return HttpResponse("NO_VALID")
            elif request.is_ajax():
                newComment = NewCommentForm()
                product = Product.objects.get(id=idProducto)
                title = "Nuevo comentario para " + product.name
                user = request.user
                message = {
                    "add_comment_data":
                    render_to_response(
                        "new_comment.html", {
                            'form': newComment,
                            'title': title,
                            'user': user,
                            'idproducto': idProducto
                        },
                        context_instance=RequestContext(request)).content
                }
                json = simplejson.dumps(message)
                return HttpResponse(json, mimetype='application/json')
        else:
            return HttpResponseRedirect("/")
    else:
        return HttpResponse("/login")
Example #3
0
    def mutate_and_get_payload(cls, input, context, info):
        user = context.user

        comment_id = input.get('id')
        comment = get_node(comment_id, context, info, Comment)
        if not comment or comment.user != user:
            raise ResponseError("Invalid Comment", code='invalid_comment')

        # update comment
        form = CommentForm(input, instance=comment)
        if form.is_valid():
            form.save()
        else:
            raise FormError(form.errors)

        return UpdateCommentMutation(comment=comment)
Example #4
0
def add_comment_to_post(request, pk):
    productPost = get_object_or_404(Product, pk=pk)
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.productPost = productPost
            comment.save()
            return redirect('products/product.html', pk=productPost.pk)
    else:
        form = CommentForm()
    return render(request, 'products/comments.html', {'form': form})
Example #5
0
def newComment(request, idProducto=None):
    if request.user.is_authenticated():
        if idProducto!=None:
            if request.method == "POST":
                newComment = NewCommentForm(request.POST)
                if newComment.is_valid():
                    try:
                        data = {'id_product':idProducto,
                                'id_sender':request.user.id,
                                'subject':newComment.cleaned_data['subject'],
                                'text':newComment.cleaned_data['text'],
                                'datetime': datetime.now(),
                                }    
                        commentForm = CommentForm(data)
                        if commentForm.is_valid():
                            commentForm.save()
                            user = request.user
   #                         send_mail('Comentario enviado', 'Usted hizo un nuevo comentario.', settings.EMAIL_HOST_USER , [user.email], fail_silently=False)
                            return HttpResponseRedirect("/products/" + str(idProducto))
                        else:
                            return HttpResponse("DATA_ERROR")
                    except Exception as e:
                        print e
                        return HttpResponseRedirect("/products/" + str(idProducto))                            
                else:
                    return HttpResponse("NO_VALID")        
            elif request.is_ajax():
                newComment = NewCommentForm()
                product = Product.objects.get(id=idProducto)
                title = "Nuevo comentario para " + product.name
                user = request.user
                message = {"add_comment_data": render_to_response("new_comment.html", {'form':newComment, 'title':title, 'user':user, 'idproducto':idProducto},context_instance=RequestContext(request)).content}
                json = simplejson.dumps(message)
                return HttpResponse(json, mimetype='application/json')
        else:
            return HttpResponseRedirect("/")
    else:
        return HttpResponse("/login")
Example #6
0
    def mutate_and_get_payload(cls, input, context, info):
        user = context.user

        product_id = input.get('product_id')
        product = get_node(product_id, context, info, Product)
        if not product:
            raise ResponseError("Invalid Product", code='invalid_product')

        # create comment
        form = CommentForm(input)
        if form.is_valid():
            new_comment = form.save(commit=False)
            new_comment.product = product
            new_comment.user = user
            new_comment.save()
        else:
            raise FormError(form.errors)

        return CreateCommentMutation(comment=new_comment)
Example #7
0
 def post(self, request, *args, **kwargs):
     # form = self.get_form()
     form = CommentForm(data=request.POST)
     pk = kwargs.get('pk', None)
     if form.is_valid():
         product = Product.objects.get(id=pk)
         data = form.save(commit=False)
         # comment obj에 위치 정보 저장
         set_object_location(request, data)
         p_latlng = str(product.lat) + ',' + str(product.lng)
         c_latlng = str(data.lat) + ',' + str(data.lng)
         data.product = product
         # product-comment의 거리 측정
         distance = google_distance_matrix(org_coord=p_latlng, des_coord=c_latlng)
         print('distance', distance)
         data.distance = distance[c_latlng]['distance']['text']
         print(data.distance)
         data.save()
         return redirect(product.get_absolute_url())
Example #8
0
def add_comment_to_product(request, id):
    user = request.user
    product = get_object_or_404(Product, pk=id)
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.user = user
            comment.product = product
            comment.approve()
            comment.save()
            return redirect('product_detail', slug=product.slug)
    else:
        form = CommentForm()
    request.session['comments_counter'] = Comment.objects.filter(user_id=user.id).count()
    template = 'products/add_comment_to_product.html'
    context ={
        'form':form,
    }
    return render(request, template, context)
Example #9
0
def product(request, product_slug=None):
    """A detail page for a product."""
    product = get_object_or_404(Product.objects.active(), slug=product_slug)

    if request.method == 'POST':
        comment_form = CommentForm(request.POST)
        if comment_form.is_valid():
            comment = comment_form.save(commit=False)
            comment.product = product
            comment.save()
            comment_form = CommentForm()
    else:
        comment_form = CommentForm()

    comments = product.productcomment_set.active()

    return render_to_response('products/product.html', {
        'comments': comments,
        'comment_form': comment_form,
        'product': product,
        'ratio': float(product.current_quantity) / float(product.max_quantity),
    }, context_instance=RequestContext(request))