Skip to content

Commit

Permalink
Added next and prev links to the week view
Browse files Browse the repository at this point in the history
  • Loading branch information
alf committed Oct 11, 2011
1 parent cee9410 commit a817e8a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
26 changes: 22 additions & 4 deletions v1.py
@@ -1,10 +1,12 @@
from __future__ import absolute_import

from .auth import requires_auth
from flask import jsonify
from flask import url_for
from flask import g

from .auth import requires_auth
from .weeks import get_next_week, get_prev_week


@requires_auth
def index():
Expand Down Expand Up @@ -40,12 +42,11 @@ def serialize_activities(activities):
if activity.duration <= 0:
continue

date = activity.day.strftime("%Y-%m-%d")
result.append({
'id': activity.project_id,
'comment': activity.comment,
'duration': str(activity.duration),
'day': date
'day': activity.day.strftime("%Y-%m-%d")
})
return result

Expand All @@ -59,12 +60,29 @@ def get_projects():
})


def get_next_week_url(year, week):
y, w = get_next_week(year, week)
return url_for(".week", year=y, week=w)


def get_prev_week_url(year, week):
y, w = get_prev_week(year, week)
return url_for(".week", year=y, week=w)


@requires_auth
def get_week(year, week):
activities = serialize_activities(g.ct.get_week(year, week))

return jsonify({
"activities": activities
"activities": activities,
"links": [{
"rel": "next-week",
"href": get_next_week_url(year, week)
}, {
"rel": "prev-week",
"href": get_prev_week_url(year, week)
}]
})


Expand Down
57 changes: 57 additions & 0 deletions weeks.py
@@ -0,0 +1,57 @@
from __future__ import absolute_import
from datetime import timedelta, datetime, date

import unittest


def tofirstdayinisoweek(year, week):
"""Code taken from http://stackoverflow.com/questions/5882405
%W takes the first Monday to be in week 1 but ISO defines week
1 to contain 4 January. So the result from
datetime.strptime('2011221', '%Y%W%w') is off by one iff the first
Monday and 4 January are in different weeks. The latter is the
case if 4 January is a Friday, Saturday or Sunday. So the
following should work"""

ret = datetime.strptime('%04d-%02d-1' % (year, week), '%Y-%W-%w')
if date(year, 1, 4).isoweekday() > 4:
ret -= timedelta(days=7)
return ret


def get_next_week(year, week):
current = tofirstdayinisoweek(year, week)
d = current + timedelta(days=7)
return d.isocalendar()[:2]


def get_prev_week(year, week):
current = tofirstdayinisoweek(year, week)
d = current - timedelta(days=7)
return d.isocalendar()[:2]


class WeekTestCase(unittest.TestCase):
def test_that_tofirstdayinisoweek_finds_monday_31_dec_2012(self):
d = tofirstdayinisoweek(2013, 1).date()
self.assertEquals(date(2012, 12, 31), d)

def test_that_tofirstdayinisoweek_finds_monday_2_jan_2012(self):
d = tofirstdayinisoweek(2012, 1).date()
self.assertEquals(date(2012, 1, 2), d)

def test_that_next_week_increases_week(self):
self.assertEquals((2011, 10), get_next_week(2011, 9))

def test_that_prev_week_decreases_week(self):
self.assertEquals((2011, 8), get_prev_week(2011, 9))

def test_that_next_week_wraps_year(self):
self.assertEquals((2012, 1), get_next_week(2011, 52))

def test_that_prev_week_wraps_year(self):
self.assertEquals((2011, 52), get_prev_week(2012, 1))

if __name__ == "__main__":
unittest.main()

0 comments on commit a817e8a

Please sign in to comment.