def iter_last_days num today datetime now replace hour minute second m

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def iter_last_days(num):
today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
start = today - timedelta(days=(num - 1))
for x in xrange(num):
end = start + timedelta(days=1)
yield {
'start': start,
'end': end,
'tag': start.strftime('%Y-%m-%d'),
}
start = end
@login_required
def stats_page(request):
data_reg = []
data_search = []
for day in iter_last_days(7):
data_reg.append({
'day': day['tag'],
'value': User.objects.filter(date_joined__gte=day['start'],
date_joined__lt=day['end']).count(),
})
data_search.append({
'day': day['tag'],
'value': SearchRequest.objects.filter(created__gte=day['start'],
created__lt=day['end']).count(),
})
context = {
'data_reg': json.dumps(data_reg),
'data_search': json.dumps(data_search),
}
return render(request, 'keydb/stats_page.html', context)