def test_counts_filtered(self): rows = UserLog.get_counts_filtered() # print_counts(rows) self.assertEquals(len(rows), 11) rows = UserLog.get_counts_filtered(count_filter=None, group_by=('day', )) print "\nday is_correct_yes total percentage" for row in rows: print "{day} {is_correct_yes:<14} {total:<5} {percentage}".format(**row) self.assertEquals(len(rows), 2) rows = UserLog.get_counts_filtered(count_filter={'created__gte': dt.date.today()}, group_by=('day', 'userid')) self.print_counts(rows) self.assertEquals(len(rows), 2) rows = UserLog.get_counts_filtered(count_filter={'userid': 1}, group_by=('day', 'userid')) self.print_counts(rows) self.assertEquals(len(rows), 2) rows = UserLog.get_counts_filtered(count_filter={'userid': 1, 'created__gte': dt.date.today()}, group_by=('day', 'userid')) self.print_counts(rows) self.assertEquals(len(rows), 1)
def get_counts(user): counts = {} if user.is_authenticated(): rows = UserLog.get_counts_filtered({'userid': 1, 'created__gte': dt.date.today()}) if rows: counts['user_today_right'] = rows[0]['is_correct_yes'] counts['user_today_wrong'] = rows[0]['is_correct_no'] else: counts['user_today_right'] = 0 counts['user_today_wrong'] = 0 rows = UserLog.get_counts_filtered({'userid': 1}) if rows: counts['user_alltime_right'] = rows[0]['is_correct_yes'] counts['user_alltime_wrong'] = rows[0]['is_correct_no'] else: counts['user_alltime_right'] = 0 counts['user_alltime_wrong'] = 0 rows = UserLog.get_counts_filtered({'created__gte': dt.date.today()}) if rows: counts['everyone_today_right'] = rows[0]['is_correct_yes'] counts['everyone_today_wrong'] = rows[0]['is_correct_no'] else: counts['everyone_today_right'] = 0 counts['everyone_today_wrong'] = 0 rows = UserLog.get_counts_filtered() if rows: counts['everyone_alltime_right'] = rows[0]['is_correct_yes'] counts['everyone_alltime_wrong'] = rows[0]['is_correct_no'] else: counts['everyone_alltime_right'] = 0 counts['everyone_alltime_wrong'] = 0 # counts['everyone_today_right'] = UserLog.objects.filter( # correct=True, created__gte=dt.date.today()).count() # counts['everyone_today_wrong'] = UserLog.objects.filter( # correct=False, created__gte=dt.date.today()).count() # # counts['everyone_alltime_right'] = UserLog.objects.filter(correct=True).count() # counts['everyone_alltime_wrong'] = UserLog.objects.filter(correct=False).count() # For the newer table. logs = UserLog.objects.values('userid').annotate( is_correct_yes=CountCase('correct', when=True), total=Count('userid')) for row in logs: row['percentage'] = 100 * (float(row['is_correct_yes']) / row['total']) counts['logs'] = logs return counts