Exemplo n.º 1
0
def commentForm(request, recipe_id):
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            recipe = Recipe.objects.get(id=recipe_id)
            comment = Comment(content_object=recipe)
            comment.comment_text = form.cleaned_data['comment_text']
            comment.creator = request.user
            comment.save()
            return HttpResponse(
                simplejson.dumps({
                    'response': "ok",
                    'result': 'success'
                }))
        else:
            response = {}
            for k in form.errors:
                response[k] = form.errors[k][0]
                return HttpResponse(
                    simplejson.dumps({
                        'response': response,
                        'result': 'error'
                    }))
    else:
        form = CommentForm()
        recipe = Recipe.objects.get(id=recipe_id)
        comment = recipe.comments.last()
        return render(request, 'recipes/comment.html', {
            'form': form,
            'comment': comment
        })
Exemplo n.º 2
0
def edit_comment(request, comment_id):
	comment = get_object_or_404(Comment, pk=comment_id)

	if comment.author != request.user:
		return HttpResponseForbidden()

	if request.method == 'POST':
		form = CommentForm(request.POST, instance=comment)
		if form.is_valid():
			comment = form.save()
			return redirect(comment.recipe)
	else:
		form = CommentForm(instance=comment)

	return render(request, 'recipes/editcomment_form.html', {
		'form': form, 
		'object': comment
	})
Exemplo n.º 3
0
def add_comment(request, slug):
	recipe = get_object_or_404(Recipe, slug=slug)

	if request.method == 'POST':
		form = CommentForm(data=request.POST)
		if form.is_valid():
			comment = form.save(commit=False)
			comment.recipe = recipe
			comment.author = request.user
			comment.save()
			return redirect(recipe)
	else:
		form = CommentForm()

	return render(request, 'recipes/add_comment.html', {
			'form': form,
			'object': recipe
		})
Exemplo n.º 4
0
    def get(self, request, pk) :
        x = Recipe.objects.get(id=pk)
        comments = Comment.objects.filter(recipe=x).order_by('-updated_at')
        ingredients = Ingredient.objects.filter(recipe=x).order_by('-created_at')
        # instructions_list=list()
        # for line in x.instructions:

        comment_form = CommentForm()
        context = { 'recipe' : x, 'ingredients':ingredients, 'comments': comments, 'comment_form': comment_form }
        return render(request, self.template_name, context)
Exemplo n.º 5
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["form"] = CommentForm()
        recipe = context[self.context_object_name]
        recipe.ingredients_list = recipe.ingredients.split(", ")
        context[
            "own_recipe"] = self.request.user.username == recipe.user.user.username
        context["current_user"] = self.request.user.username
        context["admin"] = self.request.user.username == ADMIN_USERNAME

        return context
Exemplo n.º 6
0
def detail(request, recipe_slug):
    try:
        recipe = Recipe.objects.with_annotates().get(slug=recipe_slug)
    except Recipe.DoesNotExist:
        raise Http404("Recipe does not exist")

    comments_list = RecipeComment.objects.all().filter(
        recipe=recipe).order_by('-created_at')

    paginator = Paginator(comments_list, NUMBER_OF_COMMENTS_PER_PAGE)
    page = request.GET.get('page')
    comments = paginator.get_page(page)

    # get current mark of user if exists
    current_mark_score = 0

    # by default mark can be None if user is not connected
    mark = None

    if request.user.is_authenticated:
        mark = recipe.marks.all().filter(user=request.user).first()

    if mark:
        current_mark_score = mark.mark_score

    if request.method == 'POST':

        comment_form = CommentForm(request.POST)

        if comment_form.is_valid():
            obj = comment_form.save(commit=False)
            obj.user = request.user
            obj.recipe = recipe
            obj.save()

            # init new comment form if comment saved
            comment_form = CommentForm()
    else:
        comment_form = CommentForm()

    return render(request,
                  'recipes/detail_recipe.html',
                  context={
                      'recipe': recipe,
                      'comments': comments,
                      'current_mark_score': current_mark_score,
                      'comment_form': comment_form
                  })
Exemplo n.º 7
0
def edit_comment(request, comment_id):
    comment = get_object_or_404(Comment, pk=comment_id)

    if comment.author != request.user:
        return HttpResponseForbidden()

    if request.method == 'POST':
        form = CommentForm(request.POST, instance=comment)
        if form.is_valid():
            comment = form.save()
            return redirect(comment.recipe)
    else:
        form = CommentForm(instance=comment)

    return render(request, 'recipes/editcomment_form.html', {
        'form': form,
        'object': comment
    })
Exemplo n.º 8
0
def add_comment(request, slug):
    recipe = get_object_or_404(Recipe, slug=slug)

    if request.method == 'POST':
        form = CommentForm(data=request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.recipe = recipe
            comment.author = request.user
            comment.save()
            return redirect(recipe)
    else:
        form = CommentForm()

    return render(request, 'recipes/add_comment.html', {
        'form': form,
        'object': recipe
    })