Exemple #1
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
                  })
Exemple #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
    })
Exemple #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
    })
Exemple #4
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
	})
Exemple #5
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
		})