8 from django import template
9 from django.conf import settings
10 from django.db.models.query import QuerySet
11 from django.utils import simplejson
12 from datetime import datetime, timedelta
14 from emissions.utils import period_program
16 from panikweb import utils
18 register = template.Library()
20 @register.filter(name='zip')
24 @register.inclusion_tag('includes/audio.html', takes_context=True)
25 def audio(context, sound=None):
27 'episode': context.get('episode'),
31 @register.inclusion_tag('news/nav.html', takes_context=True)
32 def news_nav(context, date=None):
34 'newsitem': context.get('newsitem'),
35 'categories': context.get('categories'),
36 'news': context.get('news'),
39 @register.inclusion_tag('emissions/nav.html', takes_context=True)
40 def emission_nav(context, date=None, klass=None):
43 'categories': context.get('categories'),
44 'episodes': context.get('episodes'),
45 'emission': context.get('emission'),
46 '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):
54 'episode': context.get('episode'),
58 @register.inclusion_tag('episodes/detail.html', takes_context=True)
59 def episode_detail(context, date=None):
61 'episode': context.get('episode'),
62 'emission': context.get('emission'),
63 'diffusions': context.get('diffusions'),
66 @register.inclusion_tag('emissions/detail.html', takes_context=True)
67 def emission_detail(context, date=None):
69 'emission': context.get('emission'),
70 'schedules': context.get('schedules'),
73 @register.inclusion_tag('emissions/resume.html', takes_context=True)
74 def emission_resume(context, date=None):
76 'emission': context.get('emission'),
77 'schedules': context.get('schedules'),
80 @register.inclusion_tag('emissions/inline.html', takes_context=True)
81 def emission_inline(context, date=None):
83 'emission': context.get('emission'),
84 'schedules': context.get('schedules'),
87 @register.inclusion_tag('includes/player.html', takes_context=False)
89 return {'unique': uuid.uuid4()}
91 @register.inclusion_tag('includes/metaNav.html', takes_context=True)
92 def metanav(context, active=None):
94 'sectionName': context.get('sectionName')
97 @register.inclusion_tag('includes/week.html', takes_context=True)
98 def weekview(context, year=None, week=None, weekday=None, fragment=None):
99 year = year if year else datetime.today().isocalendar()[0]
100 week = week if week else datetime.today().isocalendar()[1]
101 weekday = weekday if weekday else datetime.today().weekday()
103 date = utils.tofirstdayinisoweek(year, week)
104 date = datetime(*date.timetuple()[:3])
106 program = period_program(date, date+timedelta(days=7))
109 days.append({'cells': [x for x in program if x.is_on_weekday(day+1)],
110 'datetime': date+timedelta(days=day)})
112 'fragment': fragment,
119 @register.inclusion_tag('news/inline.html', takes_context=True)
120 def news_inline(context, klass=None, logo=None):
122 'content': context.get('content'),
127 @register.inclusion_tag('news/roll.html', takes_context=True)
128 def newsroll(context, nbr=None):
130 'categories': context.get('categories'),
131 'news': context.get('news'),
132 'focus': context.get('focus'),
139 if isinstance(object, QuerySet):
140 return serialize('json', object)
141 return simplejson.dumps(object)
144 def strreplace ( string, args ):
145 find = args.split(',')[0]
146 replace = args.split(',')[1]
147 return string.replace(find,replace)
150 def replace ( string, args ):
151 search = args.split(args[0])[1]
152 replace = args.split(args[0])[2]
154 return re.sub( search, replace, string )
157 def remove_tag_facet(url, facet):
158 facet = urllib2.quote(facet.encode('utf-8'), safe='')
159 return re.sub(r'&page=\d+', '', url.replace('&selected_facets=tags_exact:%s' % facet, ''))
162 def remove_category_facet(url, facet):
163 facet = urllib2.quote(facet.encode('utf-8'), safe='')
164 return re.sub(r'&page=\d+', '', url.replace('&selected_facets=categories_exact:%s' % facet, ''))
168 def append_tag_facet(url, facet):
169 facet = urllib2.quote(facet.encode('utf-8'), safe='')
170 return re.sub(r'&page=\d+', '', url + '&selected_facets=tags_exact:%s' % facet)
173 def append_category_facet(url, facet):
174 facet = urllib2.quote(facet.encode('utf-8'), safe='')
175 return re.sub(r'&page=\d+', '', url + '&selected_facets=categories_exact:%s' % facet)
179 def search_result_template(parser, token):
181 tag_name, result_str = token.split_contents()
183 raise template.TemplateSyntaxError("%r tag requires exactly one argument" % token.contents.split()[0])
184 return FormatSearchResultNode(result_str)
187 class FormatSearchResultNode(template.Node):
188 def __init__(self, result_str):
189 self.result_var = template.Variable(result_str)
191 def render(self, context):
192 result = self.result_var.resolve(context)
195 'emission': 'emissions',
196 'episode': 'episodes'
198 t = template.loader.get_template('%s/search_result.html' % dir_mapping.get(result.model_name))
199 return t.render(template.context.Context({'result': result}, autoescape=context.autoescape))
202 @register.inclusion_tag('includes/piwik.html')
204 return {'enabled': settings.ENABLE_PIWIK}
208 def rfc822(datetime):
211 return email.utils.formatdate(time.mktime(datetime.timetuple()))