<commit_before># Copyright (c) 2014, Fortylines LLC
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import logging

from django.contrib import messages
from django.shortcuts import redirect
from django.utils.translation import ugettext as _
from django.views.generic import CreateView, ListView, TemplateView, View
from django.views.generic.detail import SingleObjectMixin, DetailView
from haystack.query import SearchQuerySet
from voting.views import vote_on_object

from answers.models import Follow, Question
from answers.forms import QuestionCreateForm
from answers import signals

LOGGER = logging.getLogger(__name__)


class QuestionDetailView(DetailView):
    """
    Generic view for a single Question.
    """

    model = Question

    def get_context_data(self, **kwargs):
        context = super(QuestionDetailView, self).get_context_data(**kwargs)
        if self.request.user.is_authenticated:
            context['is_following'] = Follow.objects.get_followers(
                kwargs['object']).filter(pk=self.request.user.id).exists()
        return context


class QuestionListView(ListView):
    """
    Generic view of a list of Questions.
    """

    model = Question

    def get_context_data(self, **kwargs):
        context = super(QuestionListView, self).get_context_data(**kwargs)
        return context


class QuestionSearchView(TemplateView):
    """
    Search for Questions matching 'q'.
    """
    limit = 10
    template_name = 'answers/question_search.html'

    def get_context_data(self, **kwargs):
        context = super(QuestionSearchView, self).get_context_data(**kwargs)
        query = self.request.GET.get('q', None)
        results = None
        if query:
            # XXX search broken until new release of haystack compatible with
            # Django 1.6:
            #   https://github.com/toastdriven/django-haystack/issues/908
            results = SearchQuerySet().models(Question).filter_or(content=query)
            if self.limit:
                results = results[:int(self.limit)]
        context.update({'results': results, 'query': query})
        return context


class QuestionCreateView(CreateView):
    """
    Create a new question.
    """

    model = Question
    form_class = QuestionCreateForm
    template_name = 'answers/question_new.html'
    prefix = 'question'

    def form_valid(self, form):
        result = super(QuestionCreateView, self).form_valid(form)
        messages.success(self.request, _("Thank you for your question !"))
        # Send notification to the staff that a Question was created.
        signals.question_new.send(
            sender=__name__, question=self.object, request=self.request)
        Follow.objects.subscribe(self.request.user, self.object)
        return result

    def get_initial(self):
        kwargs = super(QuestionCreateView, self).get_initial()
        kwargs.update({'referer': self.request.GET.get("referer", None),
            'user': self.request.user})
        return kwargs


class QuestionVoteView(SingleObjectMixin, View):
    """
    Vote a Question up/down
    """
    model = Question

    def post(self, request, *args, **kwargs): #pylint: disable=unused-argument
        # Auto subscribe User when he/she upvoted a question.
        self.object = self.get_object()
        direction = kwargs.get('direction', 'up')
        if direction == 'up':
            Follow.objects.subscribe(request.user, self.object)
            messages.success(request,
                _('You will now receive an e-mail for new comments on "%s".'
                  % self.object.title))

        return vote_on_object(
            request, model=Question,
            direction=direction,
            object_id=self.object.pk,
            template_object_name='vote',
            template_name='answers/link_confirm_vote.html',
            allow_xmlhttprequest=True)


class QuestionFollowView(SingleObjectMixin, View):
    """
    Start following comments on a Question
    """
    model = Question

    def post(self, request, *args, **kwargs): #pylint: disable=unused-argument
        self.object = self.get_object()
        Follow.objects.subscribe(request.user, self.object)
        messages.success(request,
            _('You will now receive an e-mail for new comments on "%s".'
              % self.object.title))
        return redirect(self.object)


class QuestionUnfollowView(SingleObjectMixin, View):
    """
    Stop following comments on a Question
    """
    model = Question

    def post(self, request, *args, **kwargs): #pylint: disable=unused-argument
        self.object = self.get_object()
        Follow.objects.unsubscribe(request.user, self.object)
        messages.success(request,
        _('You will no longer receive e-mails for additional comments on "%s".'
          % self.object.title))
        return redirect(self.object)
<commit_msg>derive from RedirectView to enable override of redirect url<commit_after># Copyright (c) 2014, Fortylines LLC
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import logging

from django.contrib import messages
from django.utils.translation import ugettext as _
from django.views.generic import (CreateView, ListView, RedirectView,
    TemplateView, View)
from django.views.generic.detail import SingleObjectMixin, DetailView
from haystack.query import SearchQuerySet
from voting.views import vote_on_object

from answers.models import Follow, Question
from answers.forms import QuestionCreateForm
from answers import signals

LOGGER = logging.getLogger(__name__)


class QuestionDetailView(DetailView):
    """
    Generic view for a single Question.
    """

    model = Question

    def get_context_data(self, **kwargs):
        context = super(QuestionDetailView, self).get_context_data(**kwargs)
        if self.request.user.is_authenticated:
            context['is_following'] = Follow.objects.get_followers(
                kwargs['object']).filter(pk=self.request.user.id).exists()
        return context


class QuestionListView(ListView):
    """
    Generic view of a list of Questions.
    """

    model = Question

    def get_context_data(self, **kwargs):
        context = super(QuestionListView, self).get_context_data(**kwargs)
        return context


class QuestionSearchView(TemplateView):
    """
    Search for Questions matching 'q'.
    """
    limit = 10
    template_name = 'answers/question_search.html'

    def get_context_data(self, **kwargs):
        context = super(QuestionSearchView, self).get_context_data(**kwargs)
        query = self.request.GET.get('q', None)
        results = None
        if query:
            # XXX search broken until new release of haystack compatible with
            # Django 1.6:
            #   https://github.com/toastdriven/django-haystack/issues/908
            results = SearchQuerySet().models(Question).filter_or(content=query)
            if self.limit:
                results = results[:int(self.limit)]
        context.update({'results': results, 'query': query})
        return context


class QuestionCreateView(CreateView):
    """
    Create a new question.
    """

    model = Question
    form_class = QuestionCreateForm
    template_name = 'answers/question_new.html'
    prefix = 'question'

    def form_valid(self, form):
        result = super(QuestionCreateView, self).form_valid(form)
        messages.success(self.request, _("Thank you for your question !"))
        # Send notification to the staff that a Question was created.
        signals.question_new.send(
            sender=__name__, question=self.object, request=self.request)
        Follow.objects.subscribe(self.request.user, self.object)
        return result

    def get_initial(self):
        kwargs = super(QuestionCreateView, self).get_initial()
        kwargs.update({'referer': self.request.GET.get("referer", None),
            'user': self.request.user})
        return kwargs


class QuestionVoteView(SingleObjectMixin, View):
    """
    Vote a Question up/down
    """
    model = Question

    def post(self, request, *args, **kwargs): #pylint: disable=unused-argument
        # Auto subscribe User when he/she upvoted a question.
        self.object = self.get_object()
        direction = kwargs.get('direction', 'up')
        if direction == 'up':
            Follow.objects.subscribe(request.user, self.object)
            messages.success(request,
                _('You will now receive an e-mail for new comments on "%s".'
                  % self.object.title))

        return vote_on_object(
            request, model=Question,
            direction=direction,
            object_id=self.object.pk,
            template_object_name='vote',
            template_name='answers/link_confirm_vote.html',
            allow_xmlhttprequest=True)


class QuestionFollowView(SingleObjectMixin, RedirectView):
    """
    Start following comments on a Question
    """
    model = Question

    def get_redirect_url(self, *args, **kwargs):
        return self.object.get_absolute_url()

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        Follow.objects.subscribe(request.user, self.object)
        messages.success(request,
            _('You will now receive an e-mail for new comments on "%s".'
              % self.object.title))
        return super(QuestionFollowView, self).post(request, *args, **kwargs)


class QuestionUnfollowView(SingleObjectMixin, RedirectView):
    """
    Stop following comments on a Question
    """
    model = Question

    def get_redirect_url(self, *args, **kwargs):
        return self.object.get_absolute_url()

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        Follow.objects.unsubscribe(request.user, self.object)
        messages.success(request,
        _('You will no longer receive e-mails for additional comments on "%s".'
          % self.object.title))
        return super(QuestionUnfollowView, self).post(request, *args, **kwargs)


{'fraction_non_alphanumeric': 0.09074236504266008, 'fraction_numerical': 0.0020731999043138508, 'mean_word_length': 3.144745538664904, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3574549', 'n_tokens_mistral': 3406, 'n_tokens_neox': 3228, 'n_words': 1061}