Ochs / news / views.py

import itertools
from django.shortcuts import get_object_or_404
from django.contrib.contenttypes.models import ContentType
from django.template import RequestContext, loader
from django.http import HttpResponse, Http404

from news.models import Byline, Poll, Choice

def check_poll(request, poll):
    if isinstance(poll, basestring):
        poll = Poll.objects.get(pk=poll)
    poll.already_answered = 'poll_answer%d' % poll.id in request.COOKIES
    return poll.already_answered

def poll_vote(request, poll, choice=None):
    if not request.is_ajax() and not request.method == 'POST':
        raise Http404
    poll = get_object_or_404(Poll, pk=poll)
        choice = request.POST['choice']
    except KeyError, e:
        choice = choice
    print poll
    print poll.choice_set.all()
    template = loader.get_template('news/poll.html')
    answered = check_poll(request, poll)       
    if not answered:        
            choice = poll.choice_set.get(pk=choice)
            choice.votes += 1
        except Choice.DoesNotExist, e:
            raise e
        poll.already_answered = True
    choices = poll.choice_set.all()
    total_votes = sum([c.votes for c in choices])
    for c in choices:
        c.percentage = int((c.votes / float(total_votes)) * 100) if total_votes else 0
    context = RequestContext(request, {
        'is_ajax': request.is_ajax(),
        'total_votes': total_votes,
        'poll': poll,
        'choices': choices,
    resp = HttpResponse(template.render(context))
    resp.set_cookie('poll_answer%d' % poll.id, 1)
    return resp

def story_view(request, story):
    template = loader.get_template('news/story.html')
    ctype = ContentType.objects.get_for_model(story)
    bylines = Byline.objects.filter(content_type__pk=ctype.id, object_id=story.id).order_by('position__position')
    groups = []
    for k, g in itertools.groupby(bylines, lambda x: x.position.get_position_display):
        groups.append((k(), list(g)))
    context = RequestContext(request, {
        'story': story,
        'groups': groups,
    return HttpResponse(template.render(context))
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.