from django.urls import reverse_lazy, reverse
from django.utils.decorators import method_decorator
from django.views.generic import ListView, DetailView, CreateView, DeleteView, UpdateView
from .models import BlogPost
from django.contrib.auth.decorators import login_required


class BlogPostHomeView(ListView):
    model = BlogPost
    context_object_name = "posts"


class BlogPostDetailsView(DetailView):
    model = BlogPost
    context_object_name = "post"

@method_decorator(login_required, name='dispatch')
class BlogPostCreateView(CreateView):
    model = BlogPost
    fields = ['title', 'image','author', 'category', 'content']

    def get_success_url(self):
        return reverse('posts:home')

@method_decorator(login_required, name='dispatch')
class BlogPostUpdateView(UpdateView):
    model = BlogPost
    fields = ['title', 'author', 'category', 'content']
    template_name = 'blog/blogpost_update.html'

@method_decorator(login_required, name='dispatch')
class BlogPostDeleteView(DeleteView):
    model = BlogPost
    success_url = reverse_lazy('posts:home')
[((17, 1, 17, 50), 'django.utils.decorators.method_decorator', 'method_decorator', (), '', False, 'from django.utils.decorators import method_decorator\n'), ((25, 1, 25, 50), 'django.utils.decorators.method_decorator', 'method_decorator', (), '', False, 'from django.utils.decorators import method_decorator\n'), ((31, 1, 31, 50), 'django.utils.decorators.method_decorator', 'method_decorator', (), '', False, 'from django.utils.decorators import method_decorator\n'), ((34, 18, 34, 44), 'django.urls.reverse_lazy', 'reverse_lazy', ({(34, 31, 34, 43): '"""posts:home"""'}, {}), "('posts:home')", False, 'from django.urls import reverse_lazy, reverse\n'), ((23, 15, 23, 36), 'django.urls.reverse', 'reverse', ({(23, 23, 23, 35): '"""posts:home"""'}, {}), "('posts:home')", False, 'from django.urls import reverse_lazy, reverse\n')]