paniktags.py 2.95 KB
Newer Older
fred's avatar
fred committed
1
2
import re
import urllib2
3
4
import uuid

fred's avatar
fred committed
5
6
7
8
9
10
from django import template

register = template.Library()

@register.filter(name='zip')
def zip_lists(a, b):
fred's avatar
fred committed
11
    return zip(a, b)
12

13
@register.inclusion_tag('includes/audio.html')
Simon Daron's avatar
Simon Daron committed
14
15
16
17
18
def audio(sound=None):
    return {
        'sound': sound
    }

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@register.inclusion_tag('episodes/resume.html', takes_context=True)
def episode_resume(context, date=None):
    return {
        'episode': context.get('episode'),
        'date': date,
    }

@register.inclusion_tag('episodes/inline.html', takes_context=True)
def episode_inline(context, date=None):
    return {
        'episode': context.get('episode'),
        'date': date,
    }

@register.inclusion_tag('emissions/detail.html', takes_context=True)
def emission_detail(context, date=None):
    return {
        'emission': context.get('emission'),
        'schedules': context.get('schedules'),
    }

40
41
42
@register.inclusion_tag('includes/player.html', takes_context=False)
def player():
    return {'unique': uuid.uuid4()}
43
44
45
46

@register.inclusion_tag('includes/metaNav.html', takes_context=False)
def metanav():
    return {}
47
48
49
50
51
52
53
54

@register.inclusion_tag('news/inline.html', takes_context=False)
def news_inline(content=None, klass=None, logo=None):
    return {
            'content': content,
            'class': klass,
            'logo': logo
    }
fred's avatar
fred committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76


@register.filter
def remove_tag_facet(url, facet):
    facet = urllib2.quote(facet.encode('utf-8'), safe='')
    return re.sub(r'&page=\d+', '', url.replace('&selected_facets=tags_exact:%s' % facet, ''))

@register.filter
def remove_category_facet(url, facet):
    facet = urllib2.quote(facet.encode('utf-8'), safe='')
    return re.sub(r'&page=\d+', '', url.replace('&selected_facets=categories_exact:%s' % facet, ''))


@register.filter
def append_tag_facet(url, facet):
    facet = urllib2.quote(facet.encode('utf-8'), safe='')
    return re.sub(r'&page=\d+', '', url + '&selected_facets=tags_exact:%s' % facet)

@register.filter
def append_category_facet(url, facet):
    facet = urllib2.quote(facet.encode('utf-8'), safe='')
    return re.sub(r'&page=\d+', '', url + '&selected_facets=categories_exact:%s' % facet)
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100


@register.tag
def search_result_template(parser, token):
    try:
        tag_name, result_str = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError("%r tag requires exactly one argument" % token.contents.split()[0])
    return FormatSearchResultNode(result_str)


class FormatSearchResultNode(template.Node):
    def __init__(self, result_str):
        self.result_var = template.Variable(result_str)

    def render(self, context):
        result = self.result_var.resolve(context)
        dir_mapping = {
            'newsitem': 'news',
            'emission': 'emissions',
            'episode': 'episodes'
        }
        t = template.loader.get_template('%s/search_result.html' % dir_mapping.get(result.model_name))
        return t.render(template.context.Context({'result': result}, autoescape=context.autoescape))