Source

gnocchi-ask / gnocchi / ask / views.py

The default branch has multiple heads

from django.views.generic.simple import direct_to_template
from django.views.generic.list_detail import object_list, object_detail
from django.shortcuts import get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import permission_required

from gnocchi.ask import models, forms

QUESTION_PAGE = getattr(settings, 'QUESTION_PAGE', 10)

def question_list(request):
    return object_list(request,
        queryset=models.Question.objects.all(),
        paginate_by=QUESTION_PAGE,
    )

def question_detail(request, question_id):
    return object_detail(request,
        queryset=models.Question.objects.filter(visible=True),
        object_id=question_id,
        extra_context={
            'form': forms.AddResponseForm(),
        }
    )

@permission_required('question.can_add')
def question_ask(request):
    if request.method == 'POST':
        form = forms.AskQuestionForm(request.POST)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.user = request.user
            obj.save()
            form.save_m2m()
            return HttpResponseRedirect(obj.get_absolute_url())
    else:
        form = forms.AskQuestionForm()
    return direct_to_template(request,
        'gnocchi_ask/question_ask.html',
        extra_context={
            'form': form,
        }
    )

@permission_required('response.can_add')
def question_response(request, question_id):
    question = get_object_or_404(models.Question, pk=question_id)

    if request.method == 'POST':
        form = forms.AddResponseForm(request.POST)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.user = request.user
            obj.question = question
            obj.save()
            return HttpResponseRedirect(question.get_absolute_url())
    else:
        form = forms.AddResponseForm()
    # Verify question is still 'open'
    return direct_to_template(request,
        'gnocchi_ask/response.html',
        extra_context={
            'question': question,
            'form': form,
        }
    )

DIRECTION_WEIGHT = {
    'up': 1,
    'down': -1,
}

@permission_required('response.can_vote')
def question_vote(request, question_id, response_id, direction, weight=1):
    response = get_object_or_404(models.Response, pk=response_id,
        question__id=question_id)

    weight = DIRECTION_WEIGHT[direction] * weight

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

    if not created:
        if vote.weight == weight:
            # Voted the same
            pass
        elif abs(vote.weight) == abs(weight):
            # Voted opposite
            vote.delete()
        else:
            vote.weight = weight
            vote.save()

    return HttpResponseRedirect(response.question.get_absolute_url())

def response_accept(request, question_id, response_id):
    response = get_object_or_404(models.Response, pk=response_id,
        question__id=question_id)

    if response.question.user == request.user:
        response.question.accepted = response
        response.question.save()
    else:
        messages.add_message(request, messages.WARNING,
            "You may not accept a response for a question you didn't ask."
        )
    return HttpResponseRedirect(response.question.get_absolute_url())
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.