brainstill / brainstill / views.py

# coding: utf-8
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.http import HttpResponseRedirect, HttpResponseBadRequest

from brainstill.models import Project, Vote
from brainstill.forms import IdeaForm


def project_list(request, template_name):
    context = RequestContext(request)
    return render_to_response(template_name,
                              dict(projects=Project.objects.public()),
                              context_instance=context)

def project_detail(request, project_slug, template_name):
    project = get_object_or_404(Project.objects.public(), slug=project_slug)
    context = RequestContext(request)
    vote_ranks = Vote.RANK_CHOICES
    return render_to_response(template_name,
                              dict(project=project, vote_ranks=vote_ranks),
                              context_instance=context)

@login_required
def idea_new(request, project_slug, template_name):
    project = get_object_or_404(Project.objects.public(), slug=project_slug)
    if request.method != 'POST':
        form = IdeaForm()
    else:
        form = IdeaForm(request.POST)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.author = request.user
            obj.project = project
            obj.save()
            return HttpResponseRedirect(obj.get_absolute_url())
    context = RequestContext(request)
    return render_to_response(template_name, dict(form=form, project=project),
                              context_instance=context)


def idea_detail(request, project_slug, idea_id, template_name):
    project = get_object_or_404(Project.objects.public(), slug=project_slug)
    idea = get_object_or_404(project.idea_set, pk=int(idea_id))
    context = RequestContext(request)
    return render_to_response(template_name, dict(idea=idea),
                              context_instance=context)


@login_required
def idea_rank(request, project_slug, idea_id):
    project = get_object_or_404(Project.objects.public(), slug=project_slug)
    idea = get_object_or_404(project.idea_set, pk=int(idea_id))
    existing_votes = list(idea.vote_set.filter(user=request.user))
    if existing_votes:
        [vote] = existing_votes
    else:
        vote = Vote(user=request.user, idea=idea)

    rank = request.GET.get('rank')
    minus = rank.startswith('-')
    if minus:
        rank = rank[1:]
    if not rank.isdigit():
        return HttpResponseBadRequest()
    rank = int(rank)
    if minus:
        rank *= -1
    if not any(rank == choice for choice, text in Vote.RANK_CHOICES):
        return HttpResponseBadRequest()

    vote.rank = rank
    vote.save()
    return HttpResponseRedirect(idea.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.