Commit bc0f8e6a authored by fred's avatar fred

standardize week numbers on iso format

parent 9a21ca68
......@@ -10,10 +10,11 @@ from django.conf import settings
from django.db.models.query import QuerySet
from django.utils import simplejson
from datetime import datetime, timedelta
from django.views.generic.dates import _date_from_string
from emissions.utils import period_program
from panikweb import utils
register = template.Library()
@register.filter(name='zip')
......@@ -93,11 +94,12 @@ def metanav(context, active=None):
}
@register.inclusion_tag('includes/week.html', takes_context=True)
def weekview(context, year=None, week=None, weekday=None, fragment= None):
year = year if year else datetime.today().year
week = week if week else datetime.today().isocalendar()[1]-1
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]
weekday = weekday if weekday else datetime.today().weekday()
date = _date_from_string(str(year), '%Y', '1', '%w', str(week), '%W')
date = utils.tofirstdayinisoweek(year, week)
date = datetime(*date.timetuple()[:3])
program = period_program(date, date+timedelta(days=7))
......
from datetime import datetime, timedelta
def tofirstdayinisoweek(year, week):
# from http://stackoverflow.com/questions/5882405/get-date-from-iso-week-number-in-python
ret = datetime.strptime('%04d-%02d-1' % (year, week), '%Y-%W-%w')
if datetime(year, 1, 4).isoweekday() > 4:
ret -= timedelta(days=7)
return ret
......@@ -19,6 +19,8 @@ from emissions.models import Category, Emission, Episode, Diffusion, SoundFile,
from emissions.utils import whatsonair, period_program
from . import utils
class EmissionMixin:
def get_emission_context(self, emission, episode_ids=None):
......@@ -109,19 +111,22 @@ class ProgramView(TemplateView):
def get_context_data(self, year=None, week=None, **kwargs):
context = super(ProgramView, self).get_context_data(**kwargs)
context['sectionName'] = "Emissions"
schedules = Schedule.objects.select_related().order_by('datetime')
days = []
for day in range(7):
days.append({'schedules': [x for x in schedules if x.is_on_weekday(day+1)],
'datetime': datetime(2007, 1, day+1)})
context['days'] = days
context['weekday'] = datetime.today().weekday()
context['week'] = week = int(week) if week is not None else datetime.today().isocalendar()[1]-1
context['year'] = year = int(year) if year is not None else datetime.today().year
context['week_first_day'] = datetime.strptime(str(year)+' '+str(week)+' 1', '%Y %U %w')
context['week_last_day'] = context['week_first_day'] + timedelta(days=+6)
context['week_previous'] = context['week_first_day'] + timedelta(days=-7)
context['week_next'] = context['week_last_day']
context['week'] = week = int(week) if week is not None else datetime.today().isocalendar()[1]
context['year'] = year = int(year) if year is not None else datetime.today().isocalendar()[0]
context['week_first_day'] = utils.tofirstdayinisoweek(year, week)
context['week_last_day'] = context['week_first_day'] + timedelta(days=6)
previous_week = context['week_first_day'] - timedelta(days=7)
context['previous_week_no'] = previous_week.isocalendar()[1]
context['previous_week_year'] = previous_week.isocalendar()[0]
next_week = context['week_first_day'] + timedelta(days=7)
context['next_week_no'] = next_week.isocalendar()[1]
context['next_week_year'] = next_week.isocalendar()[0]
return context
program = ProgramView.as_view()
......
......@@ -8,10 +8,10 @@
<div class="program ">
<nav class="center">
<ul class="custom distributed">
{% if week_previous %}
{% if previous_week_no %}
<li>
<a
href="{% url 'program_week' year=week_previous|date:"Y" week=week_previous|date:"W" %}"
href="{% url 'program_week' year=previous_week_year week=previous_week_no %}"
class="icon-chevron-sign-left"
></a>
</li>
......@@ -19,7 +19,7 @@
<li>Du {{week_first_day|date:"l j N"}} au {{week_last_day|date:"l j N"}}</li>
<li>
<a
href="{% url 'program_week' year=week_last_day|date:"Y" week=week_last_day|date:"W" %}"
href="{% url 'program_week' year=next_week_year week=next_week_no %}"
class="icon-chevron-sign-right"
></a>
</li>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment