text: from django.contrib.auth import get_user_model
  from django.contrib.auth.decorators import login_required
  from django.core.paginator import Paginator
  from django.shortcuts import get_object_or_404, redirect, render
  
  from .forms import PostForm, CommentForm
  from .models import Group, Post, Comment, Follow
  
  User = get_user_model()
  
  
  def index(request):
      paginator = Paginator(Post.objects.all(), 10)
      page_number = request.GET.get("page")
      page = paginator.get_page(page_number)
      return render(
          request,
          "posts/index.html",
          {"paginator": paginator, "page": page}
          )
  
  
  def group_posts(request, slug):
      group = get_object_or_404(Group, slug=slug)
      posts = group.posts.all()
      paginator = Paginator(posts, 10)
      page_number = request.GET.get("page")
      page = paginator.get_page(page_number)
      return render(
          request,
          "posts/group.html",
          {"page": page, "paginator": paginator, "group": group}
          )
  
  
  @login_required
  def new_post(request):
      form = PostForm(request.POST or None, files=request.FILES or None)
      if request.method == "POST":
          if form.is_valid():
              form.instance.author = request.user
              form.save()
              return redirect("index")
      return render(request, "posts/new_post.html", {"form": form})
  
  
  def profile(request, username):
      profile_owner = get_object_or_404(User, username=username)
      profile_posts = profile_owner.posts.all()
      paginator = Paginator(profile_posts, 10)
      page_number = request.GET.get("page")
      page = paginator.get_page(page_number)
      counter_posts = profile_posts.count()
      counter_follower = profile_owner.following.count()
      counter_following = profile_owner.follower.count()
  
      is_followed = False
  
      if request.user.is_authenticated:
          follow_object = Follow.objects.filter(
              author=profile_owner, user=request.user
              )
          if follow_object:
              is_followed = True
  
      context = {
          "profile_owner": profile_owner,
          "page": page,
          "paginator": paginator,
          "counter_posts": counter_posts,
          "counter_followers": counter_follower,
          "counter_following": counter_following,
          "is_followed": is_followed
      }
      return render(request, 'posts/profile.html', context=context)
  
  
  def post_view(request, username, post_id):
      profile_owner = User.objects.get(username=username)
      post = get_object_or_404(profile_owner.posts, pk=post_id)
      counter = profile_owner.posts.count()
      comments = Comment.objects.filter(post=post)
      profile_posts = profile_owner.posts.all()
      form = CommentForm()
      counter_posts = profile_posts.count()
      counter_follower = profile_owner.following.count()
      counter_following = profile_owner.follower.count()
  
      is_followed = False
  
      if request.user.is_authenticated:
          follow_object = Follow.objects.filter(
              author=profile_owner, user=request.user
              )
          if follow_object:
              is_followed = True
              
      context = {
          "post": post,
          "profile_owner": profile_owner,
          "counter_posts": counter_posts,
          "counter_followers": counter_follower,
          "counter_following": counter_following,
          "items": comments,
          "form": form,
          "comments": True
      }
      return render(request, 'posts/post.html', context)
  
  
  @login_required
  def post_edit(request, username, post_id):
      get_object_or_404(User, username=username)
      previous_form_info = Post.objects.get(pk=post_id)
      if request.user.username == username:
          if request.method == "POST":
              form = PostForm(
                  request.POST,
                  files=request.FILES or None,
                  instance=previous_form_info
                  )
              if form.is_valid():
                  form.save()
                  return redirect('post', post_id=post_id, username=username)
              context = {
                  "form": form,
                  "edit": True,
                  "post": previous_form_info
                  }
              return render(request, "posts/new_post.html", context)
          form = PostForm(instance=previous_form_info)
          context = {
              "form": form,
              "edit": True,
              "post": previous_form_info
          }
          return render(request, "posts/new_post.html", context)
      return redirect('post', post_id=post_id, username=username)
  
  
  def page_not_found(request, exception):
      return render(
          request,
          "misc/404.html",
          {"path": request.path},
          status=404
      )
  
  
  def server_error(request):
      return render(request, "misc/500.html", status=500)
  
  
  @login_required
  def add_comment(request, username, post_id):
      if request.method == "POST":
          form = CommentForm(request.POST)
          form.instance.author = request.user
          form.instance.post_id = post_id
          if form.is_valid():
              form.save()
      return redirect(
          "post", post_id=post_id, username=username
          )
  
  
  @login_required
  def follow_index(request):
      posts = Post.objects.filter(
          author__following__user=request.user
          )
      paginator = Paginator(posts, 10)
      page_number = request.GET.get("page")
      page = paginator.get_page(page_number)
      return render(
          request,
          "posts/follow.html",
          {"paginator": paginator, "page": page, "follow": True}
          )
  
  
  @login_required
  def profile_follow(request, username):
      author = get_object_or_404(User, username=username)
      follow_object = Follow.objects.filter(
          author=author, user=request.user
          )
      self_following = (request.user.username == username)
      if follow_object.exists() or self_following:
          return redirect("profile", username=username)
      Follow.objects.create(user=request.user, author=author)
      return redirect("profile", username=username)
  
  
  @login_required
  def profile_unfollow(request, username):
      author = get_object_or_404(User, username=username)
      follow = Follow.objects.get(user=request.user, author=author)
      follow.delete()
      return redirect("profile", username=username)
  