Merge lp:~mhall119/loco-team-portal/fixes-571483 into lp:loco-team-portal

Proposed by Michael Hall
Status: Merged
Merged at revision: not available
Proposed branch: lp:~mhall119/loco-team-portal/fixes-571483
Merge into: lp:loco-team-portal
Diff against target: 57 lines (+32/-1)
1 file modified
loco_directory/events/forms.py (+32/-1)
To merge this branch: bzr merge lp:~mhall119/loco-team-portal/fixes-571483
Reviewer Review Type Date Requested Status
Daniel Holbach (community) Approve
Michael Hall (community) Needs Resubmitting
Review via email: mp+24379@code.launchpad.net

Description of the change

This converts the venue drop-down list on the Event form from a flat unordered list, to a list using optgroups to group venues by country.

To post a comment you must log in.
Revision history for this message
Daniel Holbach (dholbach) wrote :

 - Do you think it'd make sense to move grouped_venue_list() into models.py? It feels a bit more like here's-where-data-comes-from.
 - Are those print statements meant to stay?
 - Maybe it's because it's Ubuntu Release Day today, but I have a bit of a hard time understanding how the list is assembled.

118. By Michael Hall

Removed debugging print statements, added comments instead

Revision history for this message
Michael Hall (mhall119) wrote :

I removed the print statements and added comments.

The list creates something like this:
[
 ('USA', [
  (1, 'venue 1'),
  (2, 'venue 2')
 ]),
 ('France', [
  (3, 'french venue 1'),
  (4, 'french venue 2')
 ])
]

You an see what Django does with that here: http://127.0.0.1:8000/events/team/add/

review: Needs Resubmitting
Revision history for this message
Daniel Holbach (dholbach) wrote :

Do you think it should go into models.py maybe?

Revision history for this message
Daniel Holbach (dholbach) wrote :

Do you think it should go into models.py maybe?

Revision history for this message
Michael Hall (mhall119) wrote :

I don't think so, it's only useful to forms, and this is the only form with a foreign key to venues. Putting it in models.py would only separate it from the only place where it's being used.

Revision history for this message
Daniel Holbach (dholbach) wrote :

Good work!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'loco_directory/events/forms.py'
2--- loco_directory/events/forms.py 2010-01-20 19:37:55 +0000
3+++ loco_directory/events/forms.py 2010-04-29 11:53:26 +0000
4@@ -2,6 +2,7 @@
5
6 from django import forms
7 from models import BaseEvent, GlobalEvent, TeamEvent, Attendee, TeamEventComment
8+from venues.models import Venue
9
10
11 class BaseEventForm(forms.ModelForm):
12@@ -37,7 +38,6 @@
13 raise forms.ValidationError("Events can not end before they start.")
14 return self.cleaned_data
15
16-
17 class TeamEventForm(BaseEventForm):
18 """
19 a form to create/update a TeamEvent
20@@ -46,6 +46,37 @@
21 model = TeamEvent
22 exclude = ('teams', 'date_created')
23
24+ def __init__(self, *args, **kargs):
25+ super(TeamEventForm, self).__init__(*args, **kargs)
26+ self.fields['venue'].choices = self.grouped_venue_list()
27+
28+ def grouped_venue_list(self):
29+ """
30+ Returns a list of venues grouped by country
31+ in a way that Django will convert to OptGroups
32+ """
33+ venue_choices = [('', '---------')]
34+ country_choices = []
35+ current_country = ''
36+ # Ordered by country so we can tell when we're done with one
37+ # and starting another
38+ for venue in Venue.objects.all().order_by('country'):
39+ # If this venue has a different country, start a new group
40+ if venue.country != current_country:
41+ # Add the old group to the list if it's non-empty
42+ if len(country_choices) > 0:
43+ venue_choices.append((current_country, country_choices))
44+ country_choices = []
45+ current_country = venue.country
46+ # Add venue to the current country group
47+ country_choices.append((venue.id, '%s (%s)'%(venue.name, venue.city)))
48+
49+ # Add the last country group to the list if it's non-empty
50+ if len(country_choices) > 0:
51+ venue_choices.append((current_country, country_choices))
52+
53+ return venue_choices
54+
55
56 class GlobalEventForm(BaseEventForm):
57 """

Subscribers

People subscribed via source and target branches