django-poll-system / poll / templatetags /

from django import template
from poll.models import Poll, Item, Queue
from django.conf import settings
from django.utils.safestring import SafeUnicode
from django.utils.datetime_safe import datetime
from poll.ajax import authpass

register = template.Library()

@register.inclusion_tag('polls.html', takes_context=True)
def poll(context, poll):
    can_vote = True
    if poll.queue:
        can_vote = authpass(context['user'], poll.queue)
    return {'poll': poll,
            'poll_type': poll.print_polltype(),
            'items': Item.objects.filter(poll=poll),
            'user': context['user'],
            'can_vote': can_vote,
            'request': context['request'],
            'STATIC_URL': settings.STATIC_URL}

@register.inclusion_tag('polls.html', takes_context=True)
def poll_queue(context, queue):
        if isinstance(queue, SafeUnicode):
            tmp_queue = Queue.objects.get(title=queue)
            tmp_queue = Queue.objects.get(queue)
        raise Exception('Queue not found')

    tmp_polls = Poll.publish_manager.filter(queue=tmp_queue,

    if len(tmp_polls) > 0:
        cur_poll = tmp_polls[0]
        cur_poll = None

    return poll(context, cur_poll)

class RenderItemsClass(template.Node):
    def __init__(self, poll, items):
        self.poll = template.Variable(poll)
        self.items = template.Variable(items)

    def render(self, context):
        poll = self.poll.resolve(context)
        items = self.items.resolve(context)
        #'name' =
        pattern1 = '{3}<br /><input name="poll_{0}" ' \
                   'type="{1}" id="{2}" value="" /><br />'
        pattern2 = '<input name="poll_{0}" type="{1}" id="{2}" /> {3}<br />'
        result = ''

        #Choose an input type
        for item in items:
            if item.userbox:
                input_type = 'textbox'
                pattern = pattern1
                poll_type = poll.print_polltype()

                if poll_type == 'Single':
                    input_type = 'radio'
                elif poll_type == 'Multiple':
                    input_type = 'checkbox'
                pattern = pattern2

            result += pattern.format(, input_type,, item.value)

        return result

def render_items(parser, token):
    tag, poll, items = token.split_contents()
    return RenderItemsClass(poll, items)
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
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.