Ochs / news / templatetags / story_extras.py

from django import template
from django.template import Context, loader

from cms.models import Page

from news.models import Story, Poll
from news.views import check_poll

register = template.Library()

def eyebrow_links():
    links = Story.objects.filter(eyebrow=True).order_by('-publish_date')
    return {'links': links}

def top_categories():
    categories = Page.objects.filter(parent__isnull=True, publisher_public__isnull=False).order_by('tree_id', 'lft')
    categories = [c for c in categories if c.get_title().lower() not in ['about', 'contact']]
    return {'categories': categories}

def footer_categories():
    categories = Page.objects.filter(parent__isnull=True, publisher_public__isnull=False).order_by('tree_id', 'lft')
    return {'categories': categories}

def latest_stories(section=None):
    if section:
        kwargs = {'section': section}
        kwargs = {}
    stories = Story.objects.filter(**kwargs).order_by('-publish_date')
    return {
        'stories': stories

def do_polls(parser, token):
        tag_name, story = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError, "%r tag requires a single argument, the parent story." % token.contents.split()[0]
    return PollNode(story)
class PollNode(template.Node):
    def __init__(self, story):
        self.story = template.Variable(story)
    def render(self, context):
            story = self.story.resolve(context)
            poll_results = []
            t = loader.get_template('news/poll.html')
            print story
            print Poll.objects.filter(story=story)
            for poll in Poll.objects.filter(story=story):
                check_poll(context['request'], poll)
                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
                c = Context({
                    'total_votes': total_votes,
                    'poll': poll,
                    'choices': choices,
            return '\n'.join(poll_results)
        except template.VariableDoesNotExist:
            return ''
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.