Exemplo n.º 1
0
def recipe_view(request, pk, share=None):
    with scopes_disabled():
        recipe = get_object_or_404(Recipe, pk=pk)

        if not request.user.is_authenticated and not share_link_valid(recipe, share):
            messages.add_message(request, messages.ERROR,
                                 _('You do not have the required permissions to view this page!'))
            return HttpResponseRedirect(reverse('account_login') + '?next=' + request.path)

        if not (has_group_permission(request.user,
                                     ('guest',)) and recipe.space == request.space) and not share_link_valid(recipe,
                                                                                                             share):
            messages.add_message(request, messages.ERROR,
                                 _('You do not have the required permissions to view this page!'))
            return HttpResponseRedirect(reverse('index'))

        comments = Comment.objects.filter(recipe__space=request.space, recipe=recipe)

        if request.method == "POST":
            if not request.user.is_authenticated:
                messages.add_message(request, messages.ERROR,
                                     _('You do not have the required permissions to perform this action!'))
                return HttpResponseRedirect(reverse('view_recipe', kwargs={'pk': recipe.pk, 'share': share}))

            comment_form = CommentForm(request.POST, prefix='comment')
            if comment_form.is_valid():
                comment = Comment()
                comment.recipe = recipe
                comment.text = comment_form.cleaned_data['text']
                comment.created_by = request.user
                comment.save()

                messages.add_message(request, messages.SUCCESS, _('Comment saved!'))

        comment_form = CommentForm()

        user_servings = None
        if request.user.is_authenticated:
            user_servings = CookLog.objects.filter(
                recipe=recipe,
                created_by=request.user,
                servings__gt=0,
                space=request.space,
            ).all().aggregate(Avg('servings'))['servings__avg']

        if not user_servings:
            user_servings = 0

        if request.user.is_authenticated:
            if not ViewLog.objects.filter(recipe=recipe, created_by=request.user,
                                          created_at__gt=(timezone.now() - timezone.timedelta(minutes=5)),
                                          space=request.space).exists():
                ViewLog.objects.create(recipe=recipe, created_by=request.user, space=request.space)

        return render(request, 'recipe_view.html',
                      {'recipe': recipe, 'comments': comments, 'comment_form': comment_form, 'share': share,
                       'user_servings': user_servings})
Exemplo n.º 2
0
def view_recipe(request, user, recipe_slug):
    context_dict = {}
    commentForm = CommentForm()
    if request.method == 'POST':
        commentForm = CommentForm(request.POST)
        if commentForm.is_valid():
            comment = commentForm.save(commit=False)
            comment.user = request.user
            recipe = Recipe.objects.get(user=User.objects.get(username=user),
                                        slug=recipe_slug)
            comment.recipe = recipe
            comment.save()
            recipe.no_of_comments += 1
            recipe.save()

            # Use Http Get to show the recipe
            return HttpResponseRedirect(
                reverse('cookbook:view_recipe', args=[user, recipe_slug]))

    try:
        # get the recipe
        user = User.objects.get(username=user)
        recipe = Recipe.objects.get(user=user, slug=recipe_slug)
        if request.user != user:
            recipe.views += 1
            recipe.save()

        # get the recipes comments
        comments = Comment.objects.filter(
            recipe=recipe).order_by('-upload_date')

        if request.user.is_authenticated():
            saved = recipe in Recipe.objects.filter(saved_by=request.user)
            if request.user != recipe.user:
                my_rating = Rating.objects.filter(user=request.user,
                                                  recipe=recipe)
                if len(my_rating) > 0:
                    rating = my_rating[0].value
                else:
                    rating = None
            else:
                rating = None
        else:
            saved = None
            rating = None

        context_dict['recipe'] = recipe
        context_dict['comments'] = comments
        context_dict['saved'] = saved
        context_dict['commentForm'] = CommentForm()
        context_dict['rating'] = rating

    except (User.DoesNotExist, Recipe.DoesNotExist):
        context_dict['recipe'] = None
        context_dict['comments'] = None
        context_dict['saved'] = None
        context_dict['commentForm'] = None
        context_dict['rating'] = None

    return render(request, 'cookbook/view_recipe.html', context_dict)
Exemplo n.º 3
0
def recipe_view(request, pk, share=None):
    recipe = get_object_or_404(Recipe, pk=pk)

    if not has_group_permission(request.user, ('guest',)) and not share_link_valid(recipe, share):
        messages.add_message(
            request,
            messages.ERROR,
            _('You do not have the required permissions to view this page!')
        )
        return HttpResponseRedirect(reverse('view_no_group') + '?next=' + request.path)

    comments = Comment.objects.filter(recipe=recipe)

    if request.method == "POST":
        if not request.user.is_authenticated:
            messages.add_message(
                request,
                messages.ERROR,
                _('You do not have the required permissions to perform this action!')  # noqa: E501
            )
            return HttpResponseRedirect(
                reverse(
                    'view_recipe',
                    kwargs={'pk': recipe.pk, 'share': share}
                )
            )

        comment_form = CommentForm(request.POST, prefix='comment')
        if comment_form.is_valid():
            comment = Comment()
            comment.recipe = recipe
            comment.text = comment_form.cleaned_data['text']
            comment.created_by = request.user

            comment.save()

            messages.add_message(
                request, messages.SUCCESS, _('Comment saved!')
            )

        bookmark_form = RecipeBookEntryForm(request.POST, prefix='bookmark')
        if bookmark_form.is_valid():
            bookmark = RecipeBookEntry()
            bookmark.recipe = recipe
            bookmark.book = bookmark_form.cleaned_data['book']

            try:
                bookmark.save()
            except IntegrityError as e:
                if 'UNIQUE constraint' in str(e.args):
                    messages.add_message(
                        request,
                        messages.ERROR,
                        _('This recipe is already linked to the book!')
                    )
            else:
                messages.add_message(
                    request,
                    messages.SUCCESS,
                    _('Bookmark saved!')
                )

    comment_form = CommentForm()

    user_servings = None
    if request.user.is_authenticated:
        user_servings = CookLog.objects.filter(
            recipe=recipe,
            created_by=request.user,
            servings__gt=0
        ).all().aggregate(Avg('servings'))['servings__avg']

    if not user_servings:
        user_servings = 0

    if request.user.is_authenticated:
        if not ViewLog.objects \
                .filter(recipe=recipe) \
                .filter(created_by=request.user) \
                .filter(created_at__gt=(
                timezone.now() - timezone.timedelta(minutes=5))) \
                .exists():
            ViewLog.objects.create(recipe=recipe, created_by=request.user)

    return render(request, 'recipe_view.html', {'recipe': recipe, 'comments': comments, 'comment_form': comment_form, 'share': share, 'user_servings': user_servings})