def write_page(request): if not request.user.is_authenticated: return redirect('home_page') if request.method == 'POST': title = request.POST.get('title') if not title: title = 'Null' post = Post.objects.create(title=title, body=request.POST.get('body'), date=datetime.date.today(), author=request.user) post.save() context = authenticated(request) context['post'] = post return redirect('post_page', post.id) return render(request, 'write/write_page.html', authenticated(request))
def search_page(request): context = authenticated(request) if 'search' in request.GET: search = request.GET.get('search') context['users'] = User.objects.filter(username__icontains=search) context['postsTitle'] = Post.objects.filter(title__icontains=search) context['postsBody'] = Post.objects.filter(body__icontains=search) return render(request, 'search/search_page.html', context)
def post_page(request, post_id): context = authenticated(request) try: post = Post.objects.get(pk=post_id) context['post'] = post except ObjectDoesNotExist: return redirect('home_page') if request.method == 'POST' and 'comment' in request.POST: if request.POST.get('comment'): comment = Comment.objects.create(text=request.POST.get('comment'), date=datetime.date.today(), author=request.user, post=post) comment.save() if 'deletePost' in request.POST: post.delete() return redirect(request.user.profile) if 'deleteComment' in request.POST: delete_comment(request) if 'plusComment' in request.POST or 'minusComment' in request.POST: rate_comment(request) if 'plus' in request.POST or 'minus' in request.POST: if not request.user.is_authenticated: messages.error(request, 'You must be signed in to like!') elif request.user == post.author: messages.error(request, 'You cannot like yourself!') elif post.voters.filter(user=request.user).exists(): messages.error(request, "You've already voted!") elif 'plus' in request.POST: # like post.likes += 1 post.voters.add(request.user.profile) post.save() else: post.voters.add(request.user.profile) # dislike post.save() context['rating'] = count_rating(post) context['comments'] = Comment.objects.filter(post=post) return render(request, 'post/post_page.html', context)
def profile_page(request, profile_id): context = authenticated(request) user_profile = request.user if request.method == 'POST' and 'save' in request.POST: user_form = UserForm(request.POST, instance=user_profile) profile_form = ProfileForm(request.POST, instance=request.user.profile) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() messages.success(request, 'Your profile was successfully updated!') else: messages.error(request, 'Please correct the error below.') try: profile = Profile.objects.get(pk=profile_id) user_profile = profile.user context['posts'] = Post.objects.filter(author=user_profile) except ObjectDoesNotExist: return redirect('home_page') if 'plus' in request.POST or 'minus' in request.POST: if not request.user.is_authenticated: messages.error(request, 'You must be signed in to like!') elif request.user == user_profile: messages.error(request, 'You cannot like yourself!') elif user_profile.profile.voters.filter(user=request.user).exists(): messages.error(request, "You've already voted!") elif 'plus' in request.POST: # like user_profile.profile.likes += 1 user_profile.profile.voters.add(request.user.profile) user_profile.save() else: user_profile.profile.voters.add(request.user.profile) # dislike user_profile.save() context['user_profile'] = user_profile context['rating'] = count_rating(user_profile) return render(request, 'profile/profile_page.html', context)
def settings_page(request): if not request.user.is_authenticated: return redirect('home_page') return render(request, 'settings/settings_page.html', authenticated(request))