Skip to content

Commit 2f1d009

Browse files
refactor: no mutating countries/timezones choices (ietf-tools#9037)
* refactor: don't mutate countries/timezones * refactor: capitalize/make tuples to discourage mutation
1 parent e93a56b commit 2f1d009

2 files changed

Lines changed: 18 additions & 18 deletions

File tree

ietf/meeting/forms.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from ietf.doc.models import Document, State, NewRevisionDocEvent
2323
from ietf.group.models import Group
2424
from ietf.group.utils import groups_managed_by
25-
from ietf.meeting.models import Session, Meeting, Schedule, countries, timezones, TimeSlot, Room
25+
from ietf.meeting.models import Session, Meeting, Schedule, COUNTRIES, TIMEZONES, TimeSlot, Room
2626
from ietf.meeting.helpers import get_next_interim_number, make_materials_directories
2727
from ietf.meeting.helpers import is_interim_meeting_approved, get_next_agenda_name
2828
from ietf.message.models import Message
@@ -38,11 +38,6 @@
3838
from ietf.utils.validators import ( validate_file_size, validate_mime_type,
3939
validate_file_extension, validate_no_html_frame)
4040

41-
# need to insert empty option for use in ChoiceField
42-
# countries.insert(0, ('', '-'*9 ))
43-
countries.insert(0, ('', '-' * 9))
44-
timezones.insert(0, ('', '-' * 9))
45-
4641
# -------------------------------------------------
4742
# Helpers
4843
# -------------------------------------------------
@@ -140,12 +135,12 @@ class InterimMeetingModelForm(forms.ModelForm):
140135
approved = forms.BooleanField(required=False)
141136
city = forms.CharField(max_length=255, required=False)
142137
city.widget.attrs['placeholder'] = "City"
143-
country = forms.ChoiceField(choices=countries, required=False)
138+
country = forms.ChoiceField(choices=COUNTRIES, required=False)
144139
country.widget.attrs['class'] = "select2-field"
145140
country.widget.attrs['data-max-entries'] = 1
146141
country.widget.attrs['data-placeholder'] = "Country"
147142
country.widget.attrs['data-minimum-input-length'] = 0
148-
time_zone = forms.ChoiceField(choices=timezones)
143+
time_zone = forms.ChoiceField(choices=TIMEZONES)
149144
time_zone.widget.attrs['class'] = "select2-field"
150145
time_zone.widget.attrs['data-max-entries'] = 1
151146
time_zone.widget.attrs['data-minimum-input-length'] = 0

ietf/meeting/models.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,20 @@
4949
)
5050
from ietf.utils.fields import MissingOkImageField
5151

52-
countries = list(pytz.country_names.items())
53-
countries.sort(key=lambda x: x[1])
52+
# Set up countries / timezones, including an empty choice for fields
53+
EMPTY_CHOICE = ("", "-" * 9)
54+
COUNTRIES = (EMPTY_CHOICE,) + tuple(
55+
sorted(pytz.country_names.items(), key=lambda x: x[1])
56+
)
5457

55-
timezones = []
56-
for name in pytz.common_timezones:
57-
tzfn = os.path.join(settings.TZDATA_ICS_PATH, name + ".ics")
58-
if not os.path.islink(tzfn):
59-
timezones.append((name, name))
60-
timezones.sort()
58+
_tzdata_ics_path = Path(settings.TZDATA_ICS_PATH)
59+
TIMEZONES = (EMPTY_CHOICE,) + tuple(
60+
sorted(
61+
(name, name)
62+
for name in pytz.common_timezones
63+
if not (_tzdata_ics_path / f"{name}.ics").is_symlink()
64+
)
65+
)
6166

6267

6368
class Meeting(models.Model):
@@ -72,11 +77,11 @@ class Meeting(models.Model):
7277
days = models.IntegerField(default=7, null=False, validators=[MinValueValidator(1)],
7378
help_text="The number of days the meeting lasts")
7479
city = models.CharField(blank=True, max_length=255)
75-
country = models.CharField(blank=True, max_length=2, choices=countries)
80+
country = models.CharField(blank=True, max_length=2, choices=COUNTRIES)
7681
# We can't derive time-zone from country, as there are some that have
7782
# more than one timezone, and the pytz module doesn't provide timezone
7883
# lookup information for all relevant city/country combinations.
79-
time_zone = models.CharField(max_length=255, choices=timezones, default='UTC')
84+
time_zone = models.CharField(max_length=255, choices=TIMEZONES, default='UTC')
8085
idsubmit_cutoff_day_offset_00 = models.IntegerField(blank=True,
8186
default=settings.IDSUBMIT_DEFAULT_CUTOFF_DAY_OFFSET_00,
8287
help_text = "The number of days before the meeting start date when the submission of -00 drafts will be closed.")

0 commit comments

Comments
 (0)