<commit_before>from django.views.generic import UpdateView, ListView, FormView
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_protect
from django.http import HttpResponseRedirect
from django.core.exceptions import ImproperlyConfigured
from django.template.defaultfilters import slugify

from .models import Post
from .forms import PostCreateForm

class PublishedPostMixin(object):

    def get_queryset(self):
        queryset = super(PublishedPostMixin, self).get_queryset()
        return queryset.filter(published=True)

class PostListView(PublishedPostMixin, ListView):

    model = Post

class PostDetailView(PublishedPostMixin, UpdateView):

    model = Post
    form_class = PostCreateForm
    template_name_update = 'blog/post_create_update.html'
    template_name_detail = 'blog/post_detail.html'
    success_url = '/blog/'

    @method_decorator(csrf_protect)
    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        return super(PostDetailView, self).dispatch(request, *args, **kwargs)

    def form_valid(self, form):
        self.object = form.save()
        return HttpResponseRedirect(self.get_success_url())

    def get(self, request, *args, **kwargs):

        if request.user.is_staff:
            self.object = self.get_object()
            form_class = self.get_form_class()
            form = self.get_form(form_class)
            return self.render_to_response(self.get_context_data(object=self.object, form=form))
        else:
            self.object= self.get_object()
            context = self.get_context_data(object=self.object)
            return self.render_to_response(context)

    def get_template_names(self):

        if self.request.user.is_staff:
            if self.template_name_update is None:
                raise ImproperlyConfigured("TemplateResponseMixin requires either a definition of "
                    "'template_name' or an implementation of 'get_template_names()'")
            else:
                return [self.template_name_update]
        else:
            if self.template_name_detail is None:
                raise ImproperlyConfigured("TemplateResponseMixin requires either a definition of "
                    "'template_name' or an implementation of 'get_template_names()'")
            else:
                return [self.template_name_detail]

class PostCreateView(FormView):

    form_class = PostCreateForm
    template_name = 'blog/post_create_update.html'
    success_url = '/blog/'

    @method_decorator(csrf_protect)
    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        return super(PostCreateView, self).dispatch(request, *args, **kwargs)

    def form_valid(self, form):
        form.fields['slug'] = slugify(form.fields['title'])
        self.object = form.save()
        return HttpResponseRedirect(self.get_success_url())

class PostEditView(PublishedPostMixin, UpdateView):

    model = Post<commit_msg>Added blog content to the main site<commit_after>from django.views.generic import UpdateView, ListView, FormView, DetailView
from django.views.generic.edit import FormMixin
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_protect
from django.http import HttpResponseRedirect
from django.core.exceptions import ImproperlyConfigured
from django.template.defaultfilters import slugify

from .models import Post
from .forms import PostCreateForm, PostViewForm

class PublishedPostMixin(object):

    def get_queryset(self):
        queryset = super(PublishedPostMixin, self).get_queryset()
        return queryset.filter(published=True)

class PostListView(PublishedPostMixin, ListView):

    model = Post

class PostDetailView(PublishedPostMixin, DetailView, FormMixin):

    model = Post
    form_class = PostViewForm
    template_name = 'blog/post_detail.html'
    success_url = '/blog/'

    @method_decorator(csrf_protect)
    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        return super(PostDetailView, self).dispatch(request, *args, **kwargs)

    def form_valid(self, form):
        self.object = form.save()
        return HttpResponseRedirect(self.get_success_url())

class PostCreateView(FormView):

    form_class = PostCreateForm
    template_name = 'blog/post_create_update.html'
    success_url = '/blog/'

    @method_decorator(csrf_protect)
    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        return super(PostCreateView, self).dispatch(request, *args, **kwargs)

    def form_valid(self, form):
        form.fields['slug'] = slugify(form.fields['title'])
        self.object = form.save()
        return HttpResponseRedirect(self.get_success_url())

class PostEditView(PublishedPostMixin, UpdateView):

    model = Post
    template_name = 'blog/post_create_update.html'
    form_class = PostCreateForm
    success_url = "/blog/"

    @method_decorator(csrf_protect)
    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        return super(PostEditView, self).dispatch(request, *args, **kwargs)

    def form_valid(self, form):
        self.object = form.save()
        return HttpResponseRedirect(self.get_success_url())


{'fraction_non_alphanumeric': 0.09184423218221896, 'fraction_numerical': 0.0, 'mean_word_length': 3.767950963222417, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1943850', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1526, 'n_words': 339}