def test_get_week_range(self): known_start_of_week = datetime.datetime(2009, 11, 30) dtime = datetime.datetime(2009,12,1) week_range = get_week_range(dtime) week_range = [date for date in week_range] self.assertEqual(7, len(week_range)) self.assertEqual(known_start_of_week.date(), week_range[0]) self.assertEqual((known_start_of_week+datetime.timedelta(days=6)).date(), week_range[-1])
def chart_view(request, year=None, month=None, week=None, what=None, rotation=False): if what is None: what = 'artist' if None in (year, month, week): when = datetime.datetime.now() year = when.year month = when.month week = when.day/7+1 week = int(week) if week < 0 or week > 6: raise Http404() month_start = datetime.datetime(*[int(i) for i in (year, month, 1)]) month_start = month_start - datetime.timedelta(days=month_start.weekday()) start_week = month_start + datetime.timedelta(days=7*week) end_week = start_week + datetime.timedelta(days=7) prev_week = start_week - datetime.timedelta(days=1) prev_week_no = prev_week.day/7+1 next_week = end_week+datetime.timedelta(days=1) next_week_no = next_week.day/7+1 what_model = None prefix = u'' try: if what is None: what = 'album' what_model = {'artist':Artist, 'album':Album}[str(what)] prefix = {'artist':'', 'album':'artist__'}[str(what)] except IndexError: raise Http404() submitted_kwargs = { '%sentry__submitted__lt'%prefix:end_week, '%sentry__submitted__gte'%prefix:start_week, } annotate_kwargs = { 'playcount':Count('%sentry'%prefix), } items = what_model.objects.filter(**submitted_kwargs).annotate(**annotate_kwargs).order_by('-playcount') if rotation: is_rotation_kwargs = { '%sentry__is_rotation'%prefix:True, } items = items.filter(**is_rotation_kwargs) if what_model is Album: items = [{ 'name':'%s - %s' % (album.artist.name, album.name), 'playcount':album.playcount } for album in items] else: items = [{ 'name':artist.name, 'playcount':artist.playcount } for artist in items] ctxt = { 'items':items[:30], 'week':start_week, 'week_no':start_week.day/7+1, 'prev_week':prev_week, 'prev_week_no':prev_week_no, 'next_week':next_week, 'next_week_no':next_week_no, } return render_to_response('radio.logs/charts_view.html', ctxt, context_instance=RequestContext(request))
def test_get_offset_in_seconds(self): dtime = datetime.datetime(2009, 11, 1, 2, 30) expected_seconds = 2*(60**2) + 30*60 self.assertEqual(expected_seconds, get_offset_in_seconds(dtime)) self.assertEqual(0, get_offset_in_seconds(strip_hour_and_minute(dtime))) self.assertEqual(0, get_offset_in_seconds(dtime.date()))
def test_get_day_of_week(self): dtime = datetime.datetime(2009, 12, 1) # <- a tuesday expected = datetime.datetime(2009, 12, 2) # <- a wednesday expected_2 = datetime.datetime(2009, 11, 29) self.assertEqual(expected, get_day_of_week(2, dtime)) self.assertEqual(expected_2, get_day_of_week(-1, dtime)) # get the sunday of the week previous
def test_get_start_of_week(self): known_start_of_week = datetime.datetime(2009, 11, 30) dtime = datetime.datetime(2009,12,1) self.assertEqual(known_start_of_week, get_start_of_week(dtime))