Mercurial > public > sg101
comparison polls/views.py @ 1090:71685387dd11
Reduce database usage on poll index page.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Mon, 09 May 2016 19:48:51 -0500 |
parents | ee87ea74d46b |
children | f7554fb88727 |
comparison
equal
deleted
inserted
replaced
1089:64fb0d6933a4 | 1090:71685387dd11 |
---|---|
34 return user_choice | 34 return user_choice |
35 | 35 |
36 ####################################################################### | 36 ####################################################################### |
37 | 37 |
38 def poll_index(request): | 38 def poll_index(request): |
39 current_polls = Poll.objects.get_current_polls() | 39 |
40 old_polls = Poll.objects.get_old_polls() | 40 # Do some stuff manually to avoid cascading hits to the database |
41 now = datetime.datetime.now() | |
42 qs = Poll.objects.filter(is_enabled=True) | |
43 poll_dict = {} | |
44 current_polls = [] | |
45 old_polls = [] | |
46 for poll in qs: | |
47 poll.total_votes_ = 0 | |
48 poll_dict[poll.pk] = poll | |
49 if (poll.start_date <= now and | |
50 (poll.end_date is None or poll.end_date >= now)): | |
51 current_polls.append(poll) | |
52 elif (poll.start_date < now and | |
53 (poll.end_date is not None and poll.end_date < now)): | |
54 old_polls.append(poll) | |
55 | |
56 for choice in Choice.objects.iterator(): | |
57 poll = poll_dict.get(choice.poll_id) | |
58 if poll: | |
59 poll.total_votes_ += choice.votes | |
60 | |
41 return render(request, 'polls/index.html', { | 61 return render(request, 'polls/index.html', { |
42 'current_polls': current_polls, | 62 'current_polls': current_polls, |
43 'old_polls': old_polls, | 63 'old_polls': old_polls, |
44 }) | 64 }) |
45 | 65 |