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})
def recipe_view(request, pk, share=None): 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('index')) ingredients = RecipeIngredient.objects.filter(recipe=recipe).all() 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!')) 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'] bookmark.save() messages.add_message(request, messages.SUCCESS, _('Bookmark saved!')) comment_form = CommentForm() bookmark_form = RecipeBookEntryForm() 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, 'ingredients': ingredients, 'comments': comments, 'comment_form': comment_form, 'bookmark_form': bookmark_form})
def test_share(self): internal_recipe = Recipe.objects.create(name='Test', internal=True, created_by=auth.get_user( self.user_client_1)) url = reverse('view_recipe', kwargs={'pk': internal_recipe.pk}) r = self.user_client_1.get(url) self.assertEqual(r.status_code, 200) r = self.anonymous_client.get(url) self.assertEqual(r.status_code, 302) url = reverse('new_share_link', kwargs={'pk': internal_recipe.pk}) r = self.user_client_1.get(url) self.assertEqual(r.status_code, 302) share = ShareLink.objects.filter(recipe=internal_recipe).first() self.assertIsNotNone(share) self.assertTrue(share_link_valid(internal_recipe, share.uuid)) url = reverse('view_recipe', kwargs={ 'pk': internal_recipe.pk, 'share': share.uuid }) r = self.anonymous_client.get(url) self.assertEqual(r.status_code, 200) url = reverse('view_recipe', kwargs={ 'pk': (internal_recipe.pk + 1), 'share': share.uuid }) r = self.anonymous_client.get(url) self.assertEqual(r.status_code, 404) url = reverse('view_recipe', kwargs={ 'pk': internal_recipe.pk, 'share': uuid.uuid4() }) r = self.anonymous_client.get(url) self.assertEqual(r.status_code, 302)
def test_share(recipe_1_s1, u1_s1, a_u): with scopes_disabled(): url = reverse('view_recipe', kwargs={'pk': recipe_1_s1.pk}) r = u1_s1.get(url) assert r.status_code == 200 r = a_u.get(url) assert r.status_code == 302 url = reverse('new_share_link', kwargs={'pk': recipe_1_s1.pk}) r = u1_s1.get(url) assert r.status_code == 302 share = ShareLink.objects.filter(recipe=recipe_1_s1).first() assert share assert share_link_valid(recipe_1_s1, share.uuid) url = reverse('view_recipe', kwargs={ 'pk': recipe_1_s1.pk, 'share': share.uuid }) r = a_u.get(url) assert r.status_code == 200 url = reverse('view_recipe', kwargs={ 'pk': (recipe_1_s1.pk + 1), 'share': share.uuid }) r = a_u.get(url) assert r.status_code == 404 url = reverse('view_recipe', kwargs={ 'pk': recipe_1_s1.pk, 'share': uuid.uuid4() }) r = a_u.get(url) assert r.status_code == 302
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})