<commit_before>import forms

from django.http.response import HttpResponseRedirect
from django.views.generic.base import View, TemplateResponseMixin
from django.views.generic.detail import SingleObjectMixin

from models import Comment


class CommentView(TemplateResponseMixin, SingleObjectMixin, View):
    """
    View for creating and listing comments.
    """

    # set to true if some of your users are anonymous
    anonymous = False
    # enable threading of comments
    threaded = False
    # form variable in templates
    form_name = 'comment_form'
    # enable titles in form
    titles = False

    def initialize_form(self, data=None):
        """
        This might be overridden if default does not accept target_object
        argument
        """
        return self.comment_form(target_object=self.get_object(), data=data)

    def post(self, request, **kwargs):
        form = self.initialize_form(data=request.POST)
        user = self.request.user

        if form.is_valid():
            comment = form.save(commit=False)
            if user.is_authenticated():
                comment.user = user
            comment.save()
            return HttpResponseRedirect('')  # reload the page
        else:
            context = self.get_context_data(**kwargs)
            context[self.form_name] = form
            return self.render_to_response(context)

    def get_context_data(self, **kwargs):
        context = super(CommentView, self).get_context_data(**kwargs)
        context['comments'] = Comment.objects.for_model(self.get_object())
        context[self.form_name] = self.initialize_form()
        return context
<commit_msg>changed comment view name to generic<commit_after>import forms

from django.http.response import HttpResponseRedirect
from django.views.generic.base import View, TemplateResponseMixin
from django.views.generic.detail import SingleObjectMixin

from models import Comment


class CommentViewGeneric(TemplateResponseMixin, SingleObjectMixin, View):
    """
    View for creating and listing comments.
    """

    # set to true if some of your users are anonymous
    anonymous = False
    # enable threading of comments
    threaded = False
    # form variable in templates
    form_name = 'comment_form'
    # enable titles in form
    titles = False

    def initialize_form(self, data=None):
        """
        This might be overridden if default does not accept target_object
        argument
        """
        return self.comment_form(target_object=self.get_object(), data=data)

    def post(self, request, **kwargs):
        form = self.initialize_form(data=request.POST)
        user = self.request.user

        if form.is_valid():
            comment = form.save(commit=False)
            if user.is_authenticated():
                comment.user = user
            comment.save()
            return HttpResponseRedirect('')  # reload the page
        else:
            context = self.get_context_data(**kwargs)
            context[self.form_name] = form
            return self.render_to_response(context)

    def get_context_data(self, **kwargs):
        context = super(CommentViewGeneric, self).get_context_data(**kwargs)
        context['comments'] = Comment.objects.for_model(self.get_object())
        context[self.form_name] = self.initialize_form()
        return context

{'fraction_non_alphanumeric': 0.08425455631909172, 'fraction_numerical': 0.0, 'mean_word_length': 2.795918367346939, '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': '6870360', 'n_tokens_mistral': 884, 'n_tokens_neox': 863, 'n_words': 239}