paniktags.py 6.27 KB
Newer Older
fred's avatar
fred committed
1
2
import email.utils
import datetime
fred's avatar
fred committed
3
import re
fred's avatar
fred committed
4
import time
fred's avatar
fred committed
5
import urllib2
6
7
import uuid

fred's avatar
fred committed
8
from django import template
9
from django.conf import settings
Simon Daron's avatar
Simon Daron committed
10
11
from django.db.models.query import QuerySet
from django.utils import simplejson
Simon Daron's avatar
Simon Daron committed
12
13
14
from datetime import datetime, timedelta

from emissions.utils import period_program
fred's avatar
fred committed
15

fred's avatar
fred committed
16
17
from panikweb import utils

fred's avatar
fred committed
18
19
20
21
register = template.Library()

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

Simon Daron's avatar
Simon Daron committed
24
25
@register.inclusion_tag('includes/audio.html', takes_context=True)
def audio(context, sound=None):
Simon Daron's avatar
Simon Daron committed
26
    return {
Simon Daron's avatar
Simon Daron committed
27
        'episode': context.get('episode'),
Simon Daron's avatar
Simon Daron committed
28
29
30
        'sound': sound
    }

31
32
33
34
@register.inclusion_tag('news/nav.html', takes_context=True)
def news_nav(context, date=None):
    return {
        'newsitem': context.get('newsitem'),
Simon Daron's avatar
Simon Daron committed
35
        'categories': context.get('categories'),
36
37
38
        'news': context.get('news'),
    }

Simon Daron's avatar
Simon Daron committed
39
@register.inclusion_tag('emissions/nav.html', takes_context=True)
Simon Daron's avatar
Simon Daron committed
40
def emission_nav(context, date=None, klass=None):
Simon Daron's avatar
Simon Daron committed
41
    return {
Simon Daron's avatar
Simon Daron committed
42
43
        'class': klass,
        'categories': context.get('categories'),
44
        'episodes': context.get('episodes'),
Simon Daron's avatar
Simon Daron committed
45
46
47
48
        'emission': context.get('emission'),
        'episode': context.get('episode'),
    }

49
@register.inclusion_tag('episodes/resume.html', takes_context=True)
50
def episode_resume(context, date=None, model=None, klass=None):
51
    return {
52
        'model': model,
53
        'class': klass,
54
55
56
57
        'episode': context.get('episode'),
        'date': date,
    }

58
59
60
61
62
63
64
65
@register.inclusion_tag('episodes/detail.html', takes_context=True)
def episode_detail(context, date=None):
    return {
        'episode': context.get('episode'),
        'emission': context.get('emission'),
        'diffusions': context.get('diffusions'),
    }

66
67
68
69
70
71
72
@register.inclusion_tag('emissions/detail.html', takes_context=True)
def emission_detail(context, date=None):
    return {
        'emission': context.get('emission'),
        'schedules': context.get('schedules'),
    }

Simon Daron's avatar
Simon Daron committed
73
74
75
76
77
78
79
@register.inclusion_tag('emissions/resume.html', takes_context=True)
def emission_resume(context, date=None):
    return {
        'emission': context.get('emission'),
        'schedules': context.get('schedules'),
    }

Simon Daron's avatar
Simon Daron committed
80
81
82
83
84
85
86
@register.inclusion_tag('emissions/inline.html', takes_context=True)
def emission_inline(context, date=None):
    return {
        'emission': context.get('emission'),
        'schedules': context.get('schedules'),
    }

87
88
89
@register.inclusion_tag('includes/player.html', takes_context=False)
def player():
    return {'unique': uuid.uuid4()}
90

91
92
93
94
95
@register.inclusion_tag('includes/metaNav.html', takes_context=True)
def metanav(context, active=None):
    return {
        'sectionName': context.get('sectionName')
	}
96

Simon Daron's avatar
Simon Daron committed
97
@register.inclusion_tag('includes/week.html', takes_context=True)
fred's avatar
fred committed
98
99
100
def weekview(context, year=None, week=None, weekday=None, fragment=None):
    year = year if year else datetime.today().isocalendar()[0]
    week = week if week else datetime.today().isocalendar()[1]
101
    weekday = weekday if weekday else datetime.today().weekday()
fred's avatar
fred committed
102
103

    date = utils.tofirstdayinisoweek(year, week)
Simon Daron's avatar
Simon Daron committed
104
105
106
107
108
109
110
111
112
113
    date = datetime(*date.timetuple()[:3])

    program = period_program(date, date+timedelta(days=7))
    days = []
    for day in range(7):
        days.append({'cells': [x for x in program if x.is_on_weekday(day+1)],
                     'datetime': date+timedelta(days=day)})
    return {
        'fragment': fragment,
        'days': days,
114
        'weekday': weekday,
Simon Daron's avatar
Simon Daron committed
115
116
117
118
        'week': week,
        'year': year,
    }

Simon Daron's avatar
Simon Daron committed
119
120
@register.inclusion_tag('news/inline.html', takes_context=True)
def news_inline(context, klass=None, logo=None):
121
    return {
Simon Daron's avatar
Simon Daron committed
122
            'content': context.get('content'),
123
124
125
            'class': klass,
            'logo': logo
    }
fred's avatar
fred committed
126

Simon Daron's avatar
Simon Daron committed
127
128
129
@register.inclusion_tag('news/roll.html', takes_context=True)
def newsroll(context, nbr=None):
    return {
Simon Daron's avatar
Simon Daron committed
130
        'categories': context.get('categories'),
Simon Daron's avatar
Simon Daron committed
131
132
133
134
135
        'news': context.get('news'),
        'focus': context.get('focus'),
        'nbr': nbr,
    }

fred's avatar
fred committed
136

Simon Daron's avatar
Simon Daron committed
137
138
139
140
141
142
@register.filter
def jsonify(object):
    if isinstance(object, QuerySet):
        return serialize('json', object)
    return simplejson.dumps(object)

Simon Daron's avatar
Simon Daron committed
143
144
145
146
147
148
149
150
151
152
153
154
155
@register.filter
def strreplace ( string, args ): 
    find  = args.split(',')[0]
    replace = args.split(',')[1]
    return string.replace(find,replace)

@register.filter
def replace ( string, args ): 
    search  = args.split(args[0])[1]
    replace = args.split(args[0])[2]

    return re.sub( search, replace, string )

fred's avatar
fred committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
@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)
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199


@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))
200
201
202
203
204


@register.inclusion_tag('includes/piwik.html')
def piwik():
    return {'enabled': settings.ENABLE_PIWIK}
fred's avatar
fred committed
205
206
207
208
209
210
211


@register.filter
def rfc822(datetime):
    if datetime is None:
        return ''
    return email.utils.formatdate(time.mktime(datetime.timetuple()))