def details_or_comment_pet(request, pk): pet = Pet.objects.get(pk=pk) if request.method == 'GET': context = { 'pet': pet, 'form': CommentForm(), 'can_delete': request.user == pet.user.user, 'can_edit': request.user == pet.user.user, 'can_like': request.user != pet.user.user, 'has_liked': pet.like_set.filter(user_id=request.user.userprofile.id).exists(), 'can_comment': request.user != pet.user.user, 'can_sell': request.user == pet.user.user, 'is_sold': pet.sell_set.filter(user_id=request.user.userprofile.id).exists(), } return render(request, 'pet_detail.html', context) else: form = CommentForm(request.POST) if form.is_valid(): comment = Comment(text=form.cleaned_data['text']) comment.pet = pet comment.user = request.user.userprofile comment.save() return redirect('pet details or comment', pk) context = { 'pet': pet, 'form': form, } return render(request, 'pet_detail.html', context)
def pets_details(request, pk): pet_obj = Pet.objects.get(pk=pk) if request.method == 'GET': context = { 'pet': pet_obj, 'form': CommentForm(), 'permissions': request.user == pet_obj.user.user, 'like_comment_permissions': request.user != pet_obj.user.user, 'has_liked': pet_obj.like_set.filter( user_id=request.user.userprofile.id).exists() } return render(request, 'pet_detail.html', context) form = CommentForm(request.POST) if form.is_valid(): comment = Comment(comment=form.cleaned_data['comment']) comment.pet = pet_obj comment.user = request.user.userprofile comment.save() return redirect('pets details', pet_obj.pk) context = { 'pet': pet_obj, 'form': form, } return render(request, 'pet_detail.html', context)
def pet_details(request, pk): pet = Pet.objects.get(pk=pk) if request.method == 'GET': like = Like(pk) comment_form = CommentForm() context = { 'pet': pet, 'like': like, 'comment_form': comment_form, 'can_change': request.user == pet.user.user, 'can_like': request.user != pet.user.user, 'has_liked': pet.like_set.filter(user_id=request.user.userprofile.id).exists(), 'can_comment': request.user != pet.user.user, } return render(request, 'pet_detail.html', context) else: comment_form = CommentForm(request.POST) if comment_form.is_valid(): comment = Comment(comment=comment_form.cleaned_data['comment']) comment.pet = pet comment.user = request.user.userprofile comment.save() return redirect('pet details', pk) context = { 'pet': pet, 'comment_form': comment_form, } return render(request, 'pet_detail.html', context)
def details_or_comment_pet(request, pk): pet = Pet.objects.get(pk=pk) if request == 'GET': context = { 'pet': pet, 'form': CommentForm(), } return render(request, 'pet_detail.html', context) else: form = CommentForm(request.POST) if form.is_valid(): comment = Comment(text=form.cleaned_data['text']) comment.pet = pet comment.user = request.user.userprofile comment.save() return redirect('pet details or comment', pk) context = { 'pet': pet, 'form': form, } return render(request, 'pet_detail.html', context)