Merge lp:~ronnie.vd.c/ubuntu-elections/homepage-improved into lp:ubuntu-elections

Proposed by Ronnie
Status: Merged
Approved by: Sense Egbert Hofstede
Approved revision: 35
Merged at revision: 37
Proposed branch: lp:~ronnie.vd.c/ubuntu-elections/homepage-improved
Merge into: lp:ubuntu-elections
Diff against target: 93 lines (+46/-4)
3 files modified
ubuntu_voting/elections/models.py (+25/-3)
ubuntu_voting/elections/views.py (+20/-0)
ubuntu_voting/urls.py (+1/-1)
To merge this branch: bzr merge lp:~ronnie.vd.c/ubuntu-elections/homepage-improved
Reviewer Review Type Date Requested Status
Sense Egbert Hofstede Approve
Review via email: mp+107561@code.launchpad.net

Description of the change

Improved homepage, shows the newest items that changed status (canidacy, voting, results)

To post a comment you must log in.
Revision history for this message
Sense Egbert Hofstede (sense) wrote :

Ziet er goed uit, kan in trunk. Dit is ook wat live draait?

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ubuntu_voting/elections/models.py'
2--- ubuntu_voting/elections/models.py 2011-11-20 00:05:21 +0000
3+++ ubuntu_voting/elections/models.py 2012-05-27 17:52:18 +0000
4@@ -26,6 +26,17 @@
5 def __unicode__(self):
6 return self.name
7
8+ def timedelta_statuschange(self):
9+ now = datetime.datetime.now()
10+ diff = datetime.timedelta(weeks=-52)
11+ if self.is_candidature():
12+ diff = now - self.candidature_start
13+ if self.is_election_start():
14+ diff = now - self.election_start
15+ if self.is_election_end():
16+ diff = now - self.election_end
17+ return diff
18+
19 def template(self, base_template='election/incl_election_item_'):
20 now = datetime.datetime.now()
21 if self.election_end < now:
22@@ -104,13 +115,24 @@
23 return self.user.get_full_name() or self.user.username
24
25 def relative_percentage(self):
26- return self.votes * 100 / Candidate.objects.filter(election=self.election).order_by('-votes')[0].votes
27+ relative_percentage = 100
28+ max_votes = Candidate.objects.filter(election=self.election).order_by('-votes')[0].votes
29+ if max_votes is not 0:
30+ relative_percentage = self.votes * 100 / max_votes
31+ return relative_percentage
32
33 def percentage(self):
34- return self.votes * 100 / self.election.total_votes()
35+ pc = 100
36+ total_votes = self.election.total_votes()
37+ if total_votes is not 0:
38+ pc = self.votes * 100 / total_votes
39+ return pc
40
41 def chosen(self):
42- last_chosen_votes = self.election.candidate_set.order_by('-votes')[self.election.candidature_amount - 1].votes
43+ try:
44+ last_chosen_votes = self.election.candidate_set.order_by('-votes')[self.election.candidature_amount - 1].votes
45+ except IndexError:
46+ return 1 # If there are not enough candidates, all candidates are chosen
47 return int(self.votes >= last_chosen_votes)
48
49 class Office(models.Model):
50
51=== modified file 'ubuntu_voting/elections/views.py'
52--- ubuntu_voting/elections/views.py 2011-11-19 23:38:02 +0000
53+++ ubuntu_voting/elections/views.py 2012-05-27 17:52:18 +0000
54@@ -22,6 +22,26 @@
55 # TODO: Write this view
56 return direct_to_template(request, 'election/election_new.html', {})
57
58+def home(request):
59+ '''Display max 3 elections on the homepage.
60+ The three that are displayed are the newest in the categories (candidature, voting, results)
61+ '''
62+ now = datetime.datetime.now()
63+ start_active_data = now + datetime.timedelta(days=30)
64+ final_active_date = now - datetime.timedelta(days=30)
65+
66+ # the active elections:
67+ elections = Election.objects.filter(candidature_start__gt=start_active_data,
68+ election_end__gt=final_active_date)
69+
70+ # is there are less than 3 active elections grab all the elections
71+ if elections.count() < 3:
72+ elections = Election.objects.all()
73+
74+ elections = sorted(elections, key=lambda election: election.timedelta_statuschange())
75+
76+ return direct_to_template(request, 'election/election_list.html', {'elections': elections[:3]})
77+
78 def election_queue(request):
79 elections = Election.objects.filter(candidature_start__gt=datetime.datetime.now())
80 return direct_to_template(request, 'election/election_list.html', {'elections': elections})
81
82=== modified file 'ubuntu_voting/urls.py'
83--- ubuntu_voting/urls.py 2011-11-19 21:56:04 +0000
84+++ ubuntu_voting/urls.py 2012-05-27 17:52:18 +0000
85@@ -8,7 +8,7 @@
86 admin.autodiscover()
87
88 urlpatterns = patterns('',
89- url(r'^$', 'elections.views.election_active', name='home'),
90+ url(r'^$', 'elections.views.home', name='home'),
91 url(r'^account/$', 'elections.views.account', name='account'), # to fill in your Full name, because the LP name is not always the right
92
93 url(r'^login/$', 'django_openid_auth.views.login_begin', name='login'),

Subscribers

People subscribed via source and target branches