text: # Create your views here.
  from django.http import HttpResponseRedirect
  from django.shortcuts import render
  from django.urls import reverse
  from django.views.generic import ListView, View
  
  from .forms import CommentForm
  from .models import Post
  
  all_posts = Post.objects.all().order_by("-date")
  
  
  class StartingPageView(ListView):
      template_name = "blog/index.html"
      model = Post
      context_object_name = "posts"
      ordering = ["-date"]
  
      def get_queryset(self):
          queryset = super().get_queryset()
          data = queryset[:3]
          return data
  
      # def get_queryset(self):
      #     base_query = super().get_queryset()
      #     data = base_query.order_by("-date")[:3]
      #     return data
  
  
  # def starting_page(request):
  #     latest_post = Post.objects.all().order_by("-date")[:3]
  #
  #     return render(request, "blog/index.html", {
  #         "posts": latest_post
  #     })
  
  
  class AllPostsView(ListView):
      template_name = "blog/all-posts.html"
      model = Post
      context_object_name = "all_posts"
      ordering = ["-date"]
  
  
  # def posts(request):
  #     return render(request, "blog/all-posts.html", {
  #         "all_posts": all_posts
  #     })
  
  # class PostDetailView(DetailView):
  #     template_name = "blog/post-detail.html"
  #     model = Post
  #
  #     def get_context_data(self, **kwargs):
  #         context = super().get_context_data(**kwargs)
  #         context["post_tags"] = self.object.tags.all()
  #         context["comment_form"] = CommentForm()
  #         return context
  
  class PostDetailView(View):
  
      def is_stored_post(self, request, post_id):
          stored_posts = request.session.get("stored_posts")
          if stored_posts is not None:
              is_saved_for_later = post_id in stored_posts
          else:
              is_saved_for_later = False
  
          return is_saved_for_later
  
      def get(self, request, slug):
          post = Post.objects.get(slug=slug)
  
          context = {
              "post": post,
              "post_tags": post.tags.all(),
              "comment_form": CommentForm(),
              "comments": post.comments.all().order_by("-id"),
              "saved_for_later": self.is_stored_post(request, post.id)
          }
          return render(request, "blog/post-detail.html", context)
  
      def post(self, request, slug):
          comment_form = CommentForm(request.POST)
          post = Post.objects.get(slug=slug)
  
          if comment_form.is_valid():
              comment = comment_form.save(commit=False)
              comment.post = post
              comment.save()
  
              return HttpResponseRedirect(reverse("posts_detail_page", args=[slug]))
  
          context = {
              "post": post,
              "post_tags": post.tags.all(),
              "comment_form": comment_form,
              "comments": post.comments.all().order_by("-id"),
              "saved_for_later": self.is_stored_post(request, post.id)
          }
          return render(request, "blog/post-detail.html", context)
  
  
  # def post_detail(request, slug):
  #     identified_post = get_object_or_404(all_posts, slug=slug)
  #
  #     return render(request, "blog/post-detail.html", {
  #         "post": identified_post,
  #         "post_tags": identified_post.tags.all()
  #     })
  
  class ReadLaterView(View):
      def get(self, request):
          stored_posts = request.session.get("stored_posts")
  
          context = {}
          if stored_posts is None or len(stored_posts) == 0:
              context["posts"] = []
              context["has_posts"] = False
          else:
              posts = Post.objects.filter(id__in=stored_posts)  # fetch the posts in which ids are part of that list
              context["posts"] = posts
              context["has_posts"] = True
          return render(request, "blog/stored-posts.html", context)
  
      def post(self, request):
          stored_posts = request.session.get("stored_posts")
  
          if stored_posts is None:
              stored_posts = []
  
          post_id = int(request.POST["post_id"])
          if post_id not in stored_posts:
              stored_posts.append(post_id)
  
          else:
              stored_posts.remove(post_id)
  
          request.session["stored_posts"] = stored_posts
  
          return HttpResponseRedirect("/")
  