def graph(user, end_day, length, today, type): start_day = end_day - datetime.timedelta(days=(length-1)) if today < start_day: length = (end_day - today).days start_day = today approximated_weight = approximate_user_data(user.userdata_set.filter(), type, extend_to=length, current_date = today) if not approximated_weight: return {'plot_data':None} modifier = 0 if len(approximated_weight) == (today - start_day).days: modifier = 1 today_value = approximated_weight[(today - start_day).days - modifier] data = [] for i in range(len(approximated_weight)): date = start_day+datetime.timedelta(days=i) data.append(["%s-%s-%s" % (date.year,date.month,date.day), approximated_weight[i]]) return {'plot_data':simplejson.dumps( [data, [["%s-%s-%s"%(today.year,today.month,today.day),today_value]]] ), 'type':type}
def test_approximate_user_data(self): ''' Sprawdzamy czy approximate_user_data zwraca +- spodziewane wartosci ''' date_to = datetime.date(day=14,month=1,year=2009) date_from = date_to - datetime.timedelta(days=23) day = datetime.date(2009, 1, 15) udl = [] for i in [1,2,3,4,11,12,13,14,20,21,22,23,24,25,26]: udl.append(UserData( weight=0 if i in [1,2,24,25,26] else 90 - i*0.5, # zero means lack of data date=date_from+datetime.timedelta(days=i)) ) self.assertEqual(26, len(approximate_user_data(udl,extend_to=26))) self.assertEqual(2, len(approximate_user_data(udl, current_date = day)))