
from django.views.generic import ListView, DetailView,CreateView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic.edit import DeleteView, UpdateView
from .models import Post, Comment
from django.shortcuts import render, redirect
from django.core.exceptions import PermissionDenied
from .forms import CommentForm
from django.shortcuts import get_object_or_404
# Create your views here.
from .models import Comment
from django import forms
# Create your views here.

class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields=('content',)


class CommentUpdate(LoginRequiredMixin, UpdateView):
    model = Comment
    form_class = CommentForm

    def dispatch(self, request, *args, **kwargs):
        if request.user.is_authenticated and request.user == self.get_object().author:
            return super(CommentUpdate, self).dispatch(request, *args, **kwargs)
        else:
            raise PermissionDenied

def delete_post(request, pk):
    model = Post.objects.get(pk=pk)
    model.delete()
    return redirect('/com_7/')


def delete_comment(request, pk):
    comment = get_object_or_404(Comment, pk=pk)
    post = comment.post
    comment.delete()
    return redirect(post.get_absolute_url())




def new_comment(request,pk):
    if request.user.is_authenticated:
        post = get_object_or_404(Post, pk=pk)


        if request.method =='POST':
            comment_form = CommentForm(request.POST)
            if comment_form.is_valid():
                comment = comment_form.save(commit=False)
                comment.post =post
                comment.author =request.user
                comment.save()
                return redirect(comment.get_absolute_url())
        else:
            return redirect(post.get_absolute_url())
    else:
        raise PermissionDenied


class PostList(ListView):
    model = Post
    ordering = '-pk'
# def index(request):
#     posts = Post.objects.all().order_by('-pk')

#     return render(
#         request, 'com_7/index.html',{'posts':posts,}
#     )
class PostDetail(DetailView):
    model = Post
    def get_context_data(self, **kwargs):
        context = super(PostDetail, self).get_context_data()
        context['comment_form']=CommentForm
        return context
# def single_post_page(request,pk):
#     post = Post.objects.get(pk=pk)

#     return render(request, 'com_7/single_post_page.html', {'post':post,})
class PostCreate(LoginRequiredMixin, CreateView):
    model = Post
    fields = ['title', 'content']

    def form_valid(self,form):
        current_user = self.request.user
        if current_user.is_authenticated:
            form.instance.author =current_user
            return super(PostCreate, self).form_valid(form)
        else:
            return redirect('/com_7/')

class PostUpdate(LoginRequiredMixin, UpdateView):
    model =Post
    fields = ['title', 'content']

    template_name = 'com_7/post_update_form.html'


    def dispatch(self, request, *args, **kwargs):
        if request.user.is_authenticated and request.user == self.get_object().author:
            return super(PostUpdate, self).dispatch(request, *args, **kwargs)
        else:
            raise PermissionDenied

