gnocchi-ask / gnocchi / ask /

The default branch has multiple heads

Full commit
from django.views import generic
from django.views.generic.edit import SingleObjectMixin
from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponseRedirect, Http404
from django.contrib import messages
import shlex
from django.db.models import Q
from gnocchi.ask import settings, models, forms

__all__ = [
    'QuestionList', 'QuestionDetail', 'QuestionAsk', 'QuestionRespond',
    'ResponseVote', 'ResponseAccept',
class QuestionList(generic.ListView):
    model = models.Question
    paginate_by = settings.QUESTION_PAGE

    def get_queryset(self):
        qset = super(QuestionList, self).get_queryset()
        search = self.request.GET.get('q', '').encode('utf8')
        for term in shlex.split(search.encode('utf8')):
            qset = qset.filter(
                Q(question__icontains=term) |
                Q(content__icontains=term) |
        return qset

class QuestionDetail(generic.DetailView):
    queryset = models.Question.objects.filter(visible=True)
    def get_context_data(self, **kwargs):
        data = super(QuestionDetail, self).get_context_data(**kwargs)
            'form': forms.AddResponseForm(),
        return data

class QuestionAsk(generic.CreateView):
    model = models.Question
    form_class = forms.AskQuestionForm
    def form_valid(self, form):
        obj =
        obj.user = self.request.user
        return HttpResponseRedirect(obj.get_absolute_url())

class ResponseMixin(SingleObjectMixin):
    '''Operate on Responses of questions, where pk is the Question,
    and response_id is the Response'''
    model = models.Question
    def get_object(self, queryset=None):
        self.question = super(ResponseMixin, self).get_object(queryset)
            obj = self.question.responses.get(pk=self.kwargs['response_id'])
        except ObjectDoesNotExist:
            raise Http404
        return obj

class QuestionRespond(generic.CreateView):
    form_class = forms.AddResponseForm
    model = models.Question	# So that get_object looks for the right thing
    template_name = 'ask/response_form.html'

    def form_valid(self, form):
        obj =
        obj.user = self.request.user
        obj.question = self.get_object()
        return HttpResponseRedirect(obj.question.get_absolute_url())

    def get_context_data(self, **kwargs):
        data = super(QuestionRespond, self).get_context_data(**kwargs)
            'question': self.object,
        return data

    'up': 1,
    'down': -1,

class ResponseVote(ResponseMixin, generic.View):
    def get(self, request, direction, weight=1, *args, **kwargs):
        self.object = self.get_object()
        weight = DIRECTION_WEIGHT[direction] * weight

        vote, created = models.Vote.objects.get_or_create(user=request.user,
            defaults={'weight': weight}

        if not created:
            if vote.weight == weight:
                # Voted the same
            elif abs(vote.weight) == abs(weight):
                # Voted opposite
                vote.weight = weight
        return HttpResponseRedirect(self.question.get_absolute_url())

class ResponseAccept(ResponseMixin, generic.View):
    '''Mark a response as the Accepted correct one.'''
    def get(self, request, *args, **kwargs):
        self.object = self.get_object()

        if self.object.question.user == request.user:
            self.object.question.accepted = self.object
            messages.add_message(request, messages.WARNING,
                "You may not accept a response for a question you didn't ask."
        return HttpResponseRedirect(self.object.question.get_absolute_url())